473,769 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Swig and Python

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_D IGIT digits[]) {
....
}

char* GetString(char *inMessage, MY_DIGIT *digit) {
char *results;
...
...
return results;
}
....ok so my interface for swig looks like..

%module MyApp
%{
#include "math.h"
%}

extern void MakeDigits(MY_D IGIT digits[]);
extern char* GetString(char *inMessage, MY_DIGIT *digit);

%include "cpointer.i "
%pointer_functi ons(MY_DIGIT, digit_array);

%include "cmalloc.i"
%malloc(int);
%free(int);

....Ok, so I have a couple questions.
1) How would I call MakeDigits from python? In the C code I would
normally have something like...
MY_DIGIT tmp[10];
MakeDigits(tmp) ;

2) How would I call GetString? Again, in C I would have...
char *ptr;
char msg[100] = "Hello World";
MY_DIGIT x = 98;
ptr = GetString(msg, x);

3) Did I define MY_DIGIT correctly in my SWIG interface file?

4) If I try the following Python code...
x = new_digit_array ()
print x
then Python.exe dies...any idea?

thanks for the help. I am trying to find my answers int he SWIG
docs...either i haven't found it or I didn't understand when I came
across it.

thanks in the mean time.

Oct 4 '05 #1
1 2159
On 4 Oct 2005 05:46:27 -0700, Java and Swing <co*******@gmai l.com> wrote:
...Ok, so I have a couple questions.
1) How would I call MakeDigits from python? In the C code I would
normally have something like...
MY_DIGIT tmp[10];
MakeDigits(tmp) ;
How I usually do this is to write a typemap so that MY_DIGIT arrays
are automagically converted to/from regular python lists. See the
SWIG docs on writing typemaps. When that is done, you can call it
just like this:

MakeDigits(rang e(10))
MakeDigits([1, 3, 5, 7, 9])
# etc.
2) How would I call GetString? Again, in C I would have...
char *ptr;
char msg[100] = "Hello World";
MY_DIGIT x = 98;
ptr = GetString(msg, x);
This should happen for you fairly automatically:

GetString('Hell o World', 98)
3) Did I define MY_DIGIT correctly in my SWIG interface file?
I can't really say. I don't use typedefs all that much. You can
always look at the generated C/C++ code to see if it looks right.
Read the docs on the Python/C API if you haven't already.
4) If I try the following Python code...
x = new_digit_array ()
print x
then Python.exe dies...any idea?
Smells like a segfault from here. Might have to put some printfs in
the generated code to find out exactly what's happening.
thanks for the help. I am trying to find my answers int he SWIG
docs...either i haven't found it or I didn't understand when I came
across it.


The SWIG user community also has a very helpful list. Check out
http://www.swig.org/mail.html for more info. You might find that your
SWIG related issues get addressed more quickly when posting to the
SWIG list. There are lots of Python hackers on the list, too. So
don't be afraid to ask fairly Python-centric questions either.

--
Steve Juranich
Tucson, AZ
USA
Oct 4 '05 #2

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

Similar topics

0
2015
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
1638
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...
0
2506
by: Helmut Zeisel | last post by:
I want to build a static extension of Python using SWIG and VC++ 6.0 as described in http://www.swig.org/Doc1.3/Python.html#n8 for gcc. My file is testerl.i: ========================= %module testerl extern int hz(int i);
3
2098
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) {
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
10
3208
by: Bart Ogryczak | last post by:
Hi, I´m looking for some benchmarks comparing SWIG generated modules with modules made directly with C/Python API. Just how much overhead does SWIG give? Doing profile of my code I see, that it spends quiet some time in functions like _swig_setattr_nondinamic, _swig_setattr, _swig_getattr.
1
3261
by: cody314 | last post by:
Versions: Python 2.5.1 (r251:54863, May 14 2007, 10:50:04) SWIG Version 1.3.20 Hello I have some code that wraps a C++ library so I may use it in python. The code basically just gets some data and puts it in the PyArrayObject* which is returned as a PyObject*. I then call it from python like so: self.c = __f2.fdct2_wrapper(x,self.nbs,self.nba,self.ac)
1
1707
by: Uberman | last post by:
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...
0
1341
by: Basha J P M | last post by:
I am beginner in python. I am working through the tutorial examples from http://www.swig.org/ and have run into some problems. I took the following command instructions from the tutorial on swig.org: http://www.swig.org/tutorial.html I have written example.c and example.i as described in the above tutorial. My first attempt at compiling the libraries was this:
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
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
6673
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
5299
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...
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.