473,782 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(BaseCl ass):
def __init__(self):
BaseClass.__ini t__(self)

my_peter = Peter(0)
self.peters.app end(my_peter)
Then, in my embedded code, after creating an instance of the Python SubClass:

...
PyObject* key = PyString_FromSt ring("peters");
PyObject* py_peters = PyObject_GetAtt r(py_SubClass_i nst,key);
Py_XDECREF(key) ;

if(!py_peters || !PyList_Check(p y_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_GetAtt rString(py_pete r, "get_type") ;
if(!py_peter_me thod || !PyCallable_Che ck(py_peter_met hod))
continue;

PyThreadState* old_thread_stat e = PyThreadState_S wap(my_interpre ter);

PyObject* pargs = Py_BuildValue(" ( )");
PyObject* py_peter_type = PyEval_CallObje ct(py_peter_met hod, pargs);
...

So, right here in PyEval_CallObje ct(), 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 1707
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?
Sep 5 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2016
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 Python windows FAQ I shouldn't have to make a seperate dll, see link below). I create a MyModule.i file for swig which contains a simple test c++ class. I run swig on it with the following options: swig -python -c++ -includeall -shadow MyModule.i,...
0
1640
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 good ideas. i have also sent i to the swig users list a couple of times but have not got any response so i am casting my net a little wider... i have been struggling with the following problem for a couple of days now. +specifically, i have a...
1
3788
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 = PyFloat_FromDouble($1);" %include mvf.h
3
2816
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. Trying to build using mingw out of the cygwin environment but the final call to link the extension calls directly to 'cc' which I think should really be g++ at that point.
3
2100
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> double My_variable = 3.0; int fact(int n) {
1
2161
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) {
0
1726
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 I Googled and tried to find some info on what I had to do.
1
5344
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 abstract class are abstract too and won't create the correct constructor. Abstract class: class CORE_API Shape
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.