Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 10:45 PM
Uberman
Guest
 
Posts: n/a
Default 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):
BaseClass.__init__(self)

my_peter = Peter(0)
self.peters.append(my_peter)


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);
Py_XDECREF(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))
continue;

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.
  #2  
Old September 5th, 2008, 09:15 PM
Uberman
Guest
 
Posts: n/a
Default Re: Happy fun time with SWIG

Nobody has any help for me on this one?

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

+-------+
-------| Peter |-------
| +-------+ |
| |
V V
+-------------+ +-------------+
| 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)?

OR...

Does anybody know of a a SWIG-specific mailing list or forum where discussion
of this type of problem would be more appropriate?
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles