473,394 Members | 2,063 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,394 software developers and data experts.

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_DIGIT 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_DIGIT digits[]);
extern char* GetString(char *inMessage, MY_DIGIT *digit);

%include "cpointer.i"
%pointer_functions(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 2125
On 4 Oct 2005 05:46:27 -0700, Java and Swing <co*******@gmail.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(range(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('Hello 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
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...
0
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...
0
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...
3
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>...
2
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...
10
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...
1
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...
1
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.