I am trying to pass a C++ object to Python function. This Python
function then calls another C++ function which then uses this C++
object to call methods of that object's class.
I tried something like this, but it did not work, gave core dump.
class myclass {
public:
myclass(){};
~myclass(){};
void printmyname() { printf("I am myclass, num=%d\n",num); };
};
main(){
myclass myobj
char funcbody[]=\
"def pyfunction(t167 ,classobj):\n\
onemorefunc(t16 7,\"NONAMe\")\n \
return 10\n\
\n\n";
// compile this Python function using PyRun_String & get its pointer
by PyObject_GetAtt rString.
// Later call it as below:
myclass myobj(23);
PyObject *pTuple = 0;
pTuple = PyTuple_New(2);
PyTuple_SetItem (pTuple,0,PyStr ing_FromString( "NAME")); // for t167
parameter
PyObject *inputarg = Py_BuildValue(" OO&",pTuple,myo bj); // for
classobj parameter
result = PyObject_CallOb ject(pPyEvalFun ction,inputarg) ;
}
How can I pass this class object to Python function?
Is it possible to set it in tuple using PyTuple_SetItem , because I may
have varying number of arguments for my Python functions & that's why
I can't use Py_BuildValue.
function then calls another C++ function which then uses this C++
object to call methods of that object's class.
I tried something like this, but it did not work, gave core dump.
class myclass {
public:
myclass(){};
~myclass(){};
void printmyname() { printf("I am myclass, num=%d\n",num); };
};
main(){
myclass myobj
char funcbody[]=\
"def pyfunction(t167 ,classobj):\n\
onemorefunc(t16 7,\"NONAMe\")\n \
return 10\n\
\n\n";
// compile this Python function using PyRun_String & get its pointer
by PyObject_GetAtt rString.
// Later call it as below:
myclass myobj(23);
PyObject *pTuple = 0;
pTuple = PyTuple_New(2);
PyTuple_SetItem (pTuple,0,PyStr ing_FromString( "NAME")); // for t167
parameter
PyObject *inputarg = Py_BuildValue(" OO&",pTuple,myo bj); // for
classobj parameter
result = PyObject_CallOb ject(pPyEvalFun ction,inputarg) ;
}
How can I pass this class object to Python function?
Is it possible to set it in tuple using PyTuple_SetItem , because I may
have varying number of arguments for my Python functions & that's why
I can't use Py_BuildValue.
Comment