return a ctypes object to C

Eryk Sun eryksun at gmail.com
Thu Oct 31 18:24:44 EDT 2019


On 10/30/19, Arnaud Loonstra <arnaud at sphaero.org> wrote:
>
> I have a binding to access and create the C methods and structures so in
> Python I can call the Zmsg() constructor. I now need to return this.
>
> My python test method is simply:
>
> def actor_test( *args, **kwargs):
>      print("test")
>      msg = Zmsg()
>      frame = Zframe(b"Hello", 5)
>      msg.prepend(frame)
>      return msg
>
> the method is called from C as follows:
>
> PyObject *pReturn = PyObject_CallObject(pFunc, NULL);
>
> This correctly calls the method. However the returned object is of
> course a PyObject*. The debugger says it's
>
> "<czmq._czmq_ctypes.Zmsg object at 0x7ffff5f18e50>"	PyObject
> 			[class]	"<class 'czmq._czmq_ctypes.Zmsg'>"	
> 			[super class]	"<class 'object'>"	
> 			[meta type]	"<class 'type'>"	
> 			ob_refcnt	1	Py_ssize_t
>
> However how I can I get it back to the original C type (zmsg_t *)

I don't know what this Zmsg type is. It doesn't appear to be a ctypes
type such as a Structure. Either way, I'd wager that it supports the
buffer protocol.

https://docs.python.org/3/c-api/buffer.html

Call PyObject_GetBuffer to get a simple view, and try casting the
`buf` pointer to a zmsg_t pointer.


More information about the Python-list mailing list