Happy fun time with SWIG

Uberman bhood37 at hotmail.com
Thu Sep 4 23:33:50 CEST 2008

I have a bit of a odd arrangement here with SWIG, Python, Embedded Python and
C++ classes exported into Python.  Here's the plot:

I have a class defined in a C++ DLL library.  I am wrapping this class (we'll
call it "Peter") with SWIG so some of its base functionality is available in
Python.  For the sake of this example, Peter has a method called get_type(),
that takes no arguments and returns an integer.  This all works, and within my
Python environment I can create instances of Peter, copy them, query their
values -- everything I need to do with them.  I've tested all this from the
Python interpreter with expected results.  Peter exists functionally within
Python's environment.

However, I am also embedding Python into an application, an application that
needs to execute the Python module that makes heavy use of Peter (A-ha!  The
plot thickens!).  Peter's instance within the Python environment contains
singular data, set within the Python environment, that I need to access from
within the embedded Python code.  I also use C++ instances of Peter within my
C++ code.

Here's an example of what I'm looking at:

    # this is the SWIG generated Python import module
    from Peter import *

    class BaseClass:
        def __init__(self):
            self.peters = []

    class SubClass(BaseClass):
        def __init__(self):

            my_peter = Peter(0)

Then, in my embedded code, after creating an instance of the Python SubClass:

    PyObject* key = PyString_FromString("peters");
    PyObject* py_peters = PyObject_GetAttr(py_SubClass_inst,key);

    if(!py_peters || !PyList_Check(py_peters))
        // handle this error

    for(int j = 0;j < PyList_Size(py_peters);j++)
        PyObject* py_peter = PyList_GetItem(py_peters,j);
        PyObject* py_peter_method = PyObject_GetAttrString(py_peter, "get_type");
        if(!py_peter_method || !PyCallable_Check(py_peter_method))

        PyThreadState* old_thread_state = PyThreadState_Swap(my_interpreter);

        PyObject* pargs = Py_BuildValue("( )");
        PyObject* py_peter_type = PyEval_CallObject(py_peter_method, pargs);

So, right here in PyEval_CallObject(), we are invoking the SWIG-generated code
to execute Peter's get_type() C++ method.  However, this call is returning -1
(not a value coded into Peter's get_type() method).  I debugged into the SWIG
code, and I see that I'm getting a PyExc_TypeError error, with a the message
that looks something like:

     in method 'Peter_get_type', argument 1 of type 'Peter const *'

Checking the watch window, the PyObject->obj_type->tp_name of "py_peter" is
"Peter" (not "instance", as I've seen it set to before with other Python
attributes).  This leads me to believe that what I'm dealing with is not
actually an instance of Peter, but rather a class template of Peter.  This
confuses me, because I thought I initialized the peters[] arrays with instances.

Anybody have any helpful insights on this?  Why is SWIG barfing?  Am I perhaps
accessing the Peter instance incorrectly?

Many thanks in advance.

More information about the Python-list mailing list