472,119 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Happy fun time with SWIG

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.
Sep 4 '08 #1
1 1618
Nobody has any help for me on this one?

Let me see if I can make it clearer by using something simpler:

-------| Peter |-------
| +-------+ |
| |
+-------------+ +-------------+
| Python | | Application |
| Environment | | Environment |
+-------------+ +-------------+
^ ^
| |
---<--<--< ? >-->-->---

Both environments use the C++ class Peter in their environments (Application
directly, and Python via SWIG). How do I exchange instances of Peter between
them? How do I get Python-environment instances of Peter with its
Python-specific settings into the Application's environment (and vice versa)?


Does anybody know of a a SWIG-specific mailing list or forum where discussion
of this type of problem would be more appropriate?
Sep 5 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andrew Collier | last post: by
1 post views Thread by Ernie | last post: by
3 posts views Thread by Kevin Dahlhausen | last post: by
3 posts views Thread by It's me | last post: by
1 post views Thread by Java and Swing | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.