1

I'm returning object of following structure from a python function

class Bus(Structure):
    _fields_ = [ ("a", c_int), 
                 ("b", c_char), 
                 ("c", c_float), 
                 ("d", c_double), 
                 ("e", c_char_p) 
               ]

def GetBus(  *args, **kwargs ):
    Signal = cast(c_char_p(kwargs['Bus']), POINTER(Bus )).contents
    ## Logic that updates Signal
    return Signal

In my C code, I want update my C struct bus, by parsing the pValue, obtained from :

  Bus bus ;
  python_Bus = Py_BuildValue("s#",  (char* )&bus,  sizeof(Bus)) ;
  PyDict_SetItemString( pDict,"Bus", python_Bus ) ;

  PyObject* pValue = PyObject_Call(pFunc, pArgs,pDict) ;

How to I parse pValue ?

Is there's any Py???_???(pvalue, ???) ; ? or How do I convert it to char* and do a memcpy in my C code (if that's a way) ?


I also tried creating a new Python Type, but looks like it all boiled down to Py_BuildValue in my "getter" function, and I don't want to have several "setters" for each element.

Using Python2.7

P0W
  • 46,614
  • 9
  • 72
  • 119

1 Answers1

2

Assuming you're using ctypes.Structure, you could use the implementation of Buffer interface.

Try something like:

Py_buffer view = { 0 };
PyObject_GetBuffer(pvalue, &view, PyBUF_SIMPLE);
// use view.buf, which is of void*
PyBuffer_Release(&view);

You might want to add some error handling to be sure of getting the proper data.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • Thank you ! Works as expected. Also could you please comment if I'm passing my C `bus` _correctly_ to Python ? It works but I'd like to get comment if I'm doing anything goofy or there's any other good way – P0W Apr 14 '15 at 22:14
  • I would create a [new type](https://docs.python.org/2/extending/newtypes.html) and use it on both sides. Why using ctypes at all? – tynn Apr 14 '15 at 22:32