Can I run a c++ object in Python program?

Fredrik Lundh effbot at telia.com
Thu Feb 24 10:05:42 EST 2000


Bing Chen <BC at prism.co.nz> wrote:
> I pass a object from c++ to Python as follow:
>   PyObject *pargs = Py_BuildValue("Oli",m_curr,field.first,field.second);
>   PyObject *pdict, *pval;
>   pdict = PyDict_New();
>   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
>
>   PyDict_SetItemString(pdict, "Y", pargs);   /* dict['Y'] = pargs   */
>   PyRun_String("from BaseModule import *",  file_input, pdict, pdict);
>   PyRun_String("X = BaseModelLink(Y)", file_input, pdict, pdict);
>
> the "m_curr" is an instance of class in my application, when I  run some
> method in the Python program as follow: /.../ it crash.

probably because that instance isn't really PyObject*
compatible...

if I were you, I'd wrap the C++ classes using SWIG:

    http://www.swig.org

</F>





More information about the Python-list mailing list