How to transfer data structure or class from Python to C/C++?
Aaron "Castironpi" Brady
castironpi at gmail.com
Thu Oct 16 17:07:35 EDT 2008
On Oct 16, 9:10 am, Hongtian <hongtian.i... at gmail.com> wrote:
> Not exactly.
>
> In my C/C++ application, I have following function or flow:
>
> void func1(....)
> {
> call PyFunc(struct Tdemo, struct &Tdemo1);
>
> }
>
> I mean I want to invoke Python function 'PyFunc' and transfer a data
> structure 'Tdemo' to this function. After some process in Python, I
> want it return 'Tdemo1' back to the C/C++ application.
>
> I research boost.python and think it is not a reasonable solution
> because it make the C/C++ application too complex.
>
> Thanks.
I am stumped. Here's what I have.
/C file:
typedef struct {
int a;
float b;
} TypeA;
static PyObject *
methA(PyObject *self, PyObject *args) {
TypeA a;
TypeA b;
PyObject* fun;
PyObject* res;
TypeA* pa;
TypeA* pb;
PyArg_ParseTuple( args, "O", &fun );
a.a= 10;
a.b= 20.5;
b.a= 30;
b.b= 40.5;
printf( "%p %p\n", &a, &b );
pa= &a;
pb= &b;
res= PyObject_CallFunction( fun, "II", &pa, &pb );
Py_DECREF( res );
return PyInt_FromLong( 0 );
}
/Py file:
import ng27ext
import ctypes as c
class TypeA( c.Structure ):
_fields_= [
( 'a', c.c_int ),
( 'b', c.c_float )
]
def exposed( obj1, obj2 ):
print 'in exposed'
print hex( obj1 ), hex( obj2 )
a= c.POINTER( TypeA ).from_address( obj1 )
print a
print a.contents
print ng27ext.methA( exposed )
/Output:
0021FD48 0021FD40
in exposed
0x21fd48 0x21fd40
<ctypes.LP_TypeA object at 0x009FF350>
<__main__.TypeA object at 0x009FF4E0>
0
Which is unexpected. The address on line 4 should be the contents of
'obj1', 0x21fd48, which it's not. I must not be using 'from_address'
properly.
More information about the Python-list
mailing list