how to pass C++ object to another C++ function via Python function

grbgooglefan ganeshborse at gmail.com
Mon Apr 21 09:24:15 EDT 2008


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(t167,\"NONAMe\")\n\
     return 10\n\
\n\n";

// compile this Python function using PyRun_String & get its pointer
by PyObject_GetAttrString.
// Later call it as below:
   myclass myobj(23);
   PyObject *pTuple = 0;
   pTuple = PyTuple_New(2);
   PyTuple_SetItem(pTuple,0,PyString_FromString("NAME")); // for t167
parameter
   PyObject *inputarg = Py_BuildValue("OO&",pTuple,myobj); // for
classobj parameter

   result = PyObject_CallObject(pPyEvalFunction,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.



More information about the Python-list mailing list