Embedded Python and SWIG shadow classes

Helmut Zeisel helmut.zeisel at aon.at
Fri Mar 8 08:28:56 EST 2002


I used SWIG to create some Python shadow classes for my C++ library,
say

MyClass
{
...
};

MyClass myFct();

No I want to call embedded Python code from a C++ program,
i.e. in C++, I call

PyObject* pmod  = PyImport_ImportModule("myclass");
PyObject* pdict = PyModule_GetDict(pmod);
PyObject* p = PyRun_String("myFct()", Py_eval_input, pdict, pdict);

How do I extract the MyClass mc from the PyObject* p?

>From what I can see from the generated myclass_wrap.cxx,
I have to use something like

MyClass* mc;

if ((SWIG_ConvertPtr(p,(void **) &mc, SWIGTYPE_p_MyClass,1)) == -1)
{
 cout << "Not converted" << endl;
}
else
{
 cout << "Converted" << endl;
}

First problems were, however, that SWIG_ConvertPtr and SWIGTYPE_p_MyClass
are unresolved symbols.
So I comnpiled myclass_wrap.cxx with -DSWIG_GLOBAL
and replace SWIGTYPE_p_MyClass by 

_swigt__p_MyClass,

and added some struct definitions and

static swig_type_info _swigt__p_MyClass[] = 
{{"_p_MyClass", 0, "MyClass *"},{"_p_MyClass"},{0}};

to the C++ program calling PyRun_String.

No my code compiles and links, but the output is "Not converted"

What is the usual way to solve this problem?

Helmut



More information about the Python-list mailing list