sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.53k stars 138 forks source link

Exposing C pointer inside python.PyObject outside the package #51

Closed hush-hush closed 7 years ago

hush-hush commented 7 years ago

Hi,

First let me explain my usage of go-python.

We are calling Python functions/classes from Go. For that go-python is great ! But we also would like to extend python with the features from our Go project, so core features are usable in Python.

For now we have some C that exposes our Go code to Python. This C code basically do what go-python does internally (calling the CPython API, translating a few types, ...). At first it was okay, but the number of Go features exposed to Python is growing pretty fast.

We have written a certain amount of helpers around go-python that we would like to use in our C binding. The problem is that we need to return to C the internal C pointer inside the Go type PyObject (this one https://github.com/sbinet/go-python/blob/master/object.go#L15). And their is no way to access it outside the package. Therefore this issue to expose it (or create a getter to it). This would allow us to do everything in Go and limit our C code to the minimal.

I also saw that go-python offers Py_InitModule method. If we could create our python module from Go, delegating the C part to go-python, it would solve our problem. But from what I read from the code, we must give pointers to C function in PyMethodDef (https://github.com/sbinet/go-python/blob/master/heap.go#L27). Right ?

This comment let me think that this feature may not be ready yet: https://github.com/sbinet/go-python/blob/master/heap.go#L16 Is it the case ?

In any case thank you for the answer.

sbinet commented 7 years ago

yeah, the whole mess of the PyMethodDef and its impedance mismatch with sbinet/go-python prompted me to create gopy instead.

back to sbinet/go-python: initially I wanted to completely insulate users from the C/cgo world. I guess it was a bit over optimistic. File a PR with a method returning the C handle (or directly exporting the C handle? I haven't thought too much about this...) and we'll review it.

hush-hush commented 7 years ago

Thank you for the answer, I've open a PR.