You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 10, 2022. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 10, 2022. It is now read-only.
How might I pass in a http.ResponseWriter object as a *PyObject into PyObject_Call? #26
running this results in:
cannot use w (type http.ResponseWriter) as type *python3.PyObject in argument to python3.PyTuple_SetItem
Describe what you expected:
I did not expect this to work, but would be interested in getting this to work so that http.ResponseWriter.Write could be called within the Python code. Is this possible?
It's possible but this will require some/a lot of wrapping. You need to extend python using CPython and there call any Go function exposed to C. You can't just pass a Go pointer and use it in Python.
In the datadog-agent we don't use this repo (because we want to embed both Python2 and Python3 at the same time, so we need to open shared library dynamically). But you can look into it to get some idea on how to do this.
As you can see embedding python is easy in Go, extending it requires some works.
In you example, I think, the simplest solution would be to create a Python Callable class, that internally store a C pointer and call a C method with that pointer when being called. Then in C you will actually call the Go function.
Activity
hush-hush commentedon Feb 28, 2020
Hi @lightbaseio,
It's possible but this will require some/a lot of wrapping. You need to extend python using CPython and there call any Go function exposed to C. You can't just pass a Go pointer and use it in Python.
In the datadog-agent we don't use this repo (because we want to embed both Python2 and Python3 at the same time, so we need to open shared library dynamically). But you can look into it to get some idea on how to do this.
rtloaderis the C bridge between Go and Python: it embed AND extend Python. rtloader/common is where we extend python. For example the python functiondatadog_agent.get_versionis created here https://github.com/DataDog/datadog-agent/blob/master/rtloader/common/builtins/datadog_agent.c#L42. We then call a callback here, that callback is actually a Go function exported to C (the agent at startup init all the callbacks).We then export Go function to C here for example and init the C callback here.
As you can see embedding python is easy in Go, extending it requires some works.
In you example, I think, the simplest solution would be to create a Python Callable class, that internally store a C pointer and call a C method with that pointer when being called. Then in C you will actually call the Go function.
lightbaseio commentedon Feb 29, 2020
Thanks for the explanation. My C is rusty but the code makes sense. Will give it a go when I have time!