473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1679
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Jon Moldover | last post by:
Hi, I'm using Python in my win32 app by linking to the python23.dll. I'm trying to expose some c++ code in my app to Python so I can make application calls from Python scripts (according to the...
by: Andrew Collier | last post by:
hello, i am not sure whether this problem relates to swig, python or my c++ compiler. +please forgive me if this is inappropriately posted but i am sure that someone +on this list must have some...
by: Ernie | last post by:
Hi, I am learning swig to use C codes from Python. Here is the swig file mvf.i: %module mvf %include "carrays.i" %array_class(double, doubleArray); %typemap(out) double, float "$result =...
by: Kevin Dahlhausen | last post by:
Could anyone post a simple setup.py script that uses a SWIG interface to C++ code and the mingw compiler? I followed some online samples, and am using the setup.cfg file to specify swig-c++=1....
by: It's me | last post by:
I am playing around with SWING building a Python module using the no brainer example in http://www.swig.org/tutorial.html. With that first example, /* File : example.c */ #include <time.h>...
by: Java and Swing | last post by:
I am trying to wrap some C code I have. Currently I have something like... defs.h ----------- typedef unsigned long MY_DIGIT; myapp.c ------------- void MakeDigits(MY_DIGIT digits) {
by: Eric von Horst | last post by:
Hi, we have a third-party product that has a C++ api on HP-UX. I would like be able to use the API in Python (as I remember Python is good at doing this). I have no experience with this so...
by: Stodge | last post by:
Yet another SWIG question (YASQ!). I'm having a problem with using an abstract base class. When generating the Python bindings, SWIG thinks that all the concrete classes that derive from this...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.