DataDog / go-python3

Go bindings to the CPython-3 API
MIT License
376 stars 140 forks source link

Hi, How to capture function return when use python3.PyRun_AnyFile #37

Closed bestkillua closed 3 years ago

bestkillua commented 3 years ago

Describe what happened: python file contians only one function return string, but I could't capture its return when use python3.PyRun_AnyFile

Describe what you expected:

Steps to reproduce the issue:

christian-korneck commented 3 years ago

PyRun_AnyFile only returns an error code (0 if successful, a negative number if unsuccessful). It's not possible to return data (like a string) from Python to Go using this function.

To do that you would need to call the function directly using its <python3.PyObject>.CallObject() method, which would return a PyObject that you could "convert" to a Go string object. (I wrote about a simple example here).

bestkillua commented 3 years ago

@christian-korneck Thanks a lot