473,789 Members | 2,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrapping C with Python

Hi!!
I tried wrapping a simple C code suing SWIG to Python, but am having
problem,
my .c file is,

Step 1:
example.c
--------------
double val=3.0;
int fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}

int mod(int x, int y)
{
return (x%y);
}

Step 2:
I then created interface file as.
example.i
--------------
%module example
%{
%}
extern int fact(int n);
extern int mod(int x,int y);

Step 3:
I compiled the .i file with SWIG,
c:\python25\swi gswig -
python example.i
It creates example.py and example_wrap.c

Step 4:
I compiled example.c and example_wrap.c individually
c:\python25\min gw\bin gcc -c example.c (it create
example.o)
c:\python25\min gw\bin gcc -c example_wrap.c -Ic:
\python25\inclu de (it create example_wrap.o)

Step 5:
building .pyd for windows,
c:\python25\min gw\bin gcc -shared *.o -o _example.pyd -Lc:
\python25\libs -lpython25 (it creates _example.pyd)
Then, i imported example module by palcing it in python25 directory,
it get's imported but i'm not able to use function, when i called
>>>print example.fact(4)
print example.mod(4,2 )
It is nither showing any error nor message...
even i tried simple void fact() by printf inside .c file and is not
showing ...

hope for help...
regard's
Anish
Aug 4 '08 #1
8 2288
Anish Chapagain wrote:
Hi!!
I tried wrapping a simple C code suing SWIG to Python, but am having
problem,
I am not too familiar with SWIG, (I have looked at it), but you may want
to try ctypes in the standard module library. It's very easy to use. I
use it on Windows with gcc but I believe it works fine on Linux too.
Basically you just compile your C code as a regular C code dll. ctypes
then allows you to access the functions in the dll very easily. I was
concerned that performance might be an issue but that has turned out not
to be true. I'm writing a chess program and the C dll handles the move
generation so it is getting called a lot. So far it runs at about
200,000 calls per second.

Patrick
Aug 4 '08 #2
RPM1 wrote:
....
Basically you just compile your C code as a regular C code dll. ctypes
then allows you to access the functions in the dll very easily.
Does that work with C++ code too or just C?
Aug 4 '08 #3
On 4 Aug, 14:14, brad <byte8b...@gmai l.comwrote:
RPM1 wrote:

...
Basically you just compile your C code as a regular C code dll. *ctypes
then allows you to access the functions in the dll very easily.

Does that work with C++ code too or just C?
Hi..
I havenot tried..before with dll, is there any help material and for
me my API in C has almost 20 c files, so will it be feasible

anish
Aug 4 '08 #4
Anish Chapagain wrote:
I tried wrapping a simple C code suing SWIG to Python, but am having
problem,
Try Cython. It's a Python-like language between Python and C that compiles to
C code. It makes it very easy to call into C functions and to hide them behind
a nice Python module.

http://cython.org/

Stefan
Aug 4 '08 #5
brad wrote:
RPM1 wrote:
...
>Basically you just compile your C code as a regular C code dll.
ctypes then allows you to access the functions in the dll very easily.

Does that work with C++ code too or just C?
I believe it does work with C++ although I have not done that. Here's a
simple example:

http://wolfprojects.altervista.org/dllforpyinc.php

I bet if you google around you'll find what you need.

Remember there is documentation for ctypes in the Python documentation
that comes with Python.

Patrick
Aug 4 '08 #6
Anish Chapagain wrote:
On 4 Aug, 14:14, brad <byte8b...@gmai l.comwrote:
>RPM1 wrote:

...
>>Basically you just compile your C code as a regular C code dll. ctypes
then allows you to access the functions in the dll very easily.
Does that work with C++ code too or just C?

Hi..
I havenot tried..before with dll, is there any help material and for
me my API in C has almost 20 c files, so will it be feasible

anish
It is worth looking into it. I found ctypes to be very easy. I had the
example up and running in minutes. I have also figured out how to do
things like return arrays to Python. It isn't that hard. I like the
fact that I don't have to think about reference counting or PyObjects
etc. If you are doing simple function calls ctypes is very easy.

My C code looks like C code. It has no Python stuff at all. My Python
code has just a few calls to the ctypes module. I guess that's one
thing I like about it is that my C code looks like C code and my Python
code looks like Python code, (not much spilling over).

I'm sure it's not perfect, but it seems a lot easier than SWIG to me.

Patrick
Aug 4 '08 #7


brad wrote:
RPM1 wrote:
...
>Basically you just compile your C code as a regular C code dll.
ctypes then allows you to access the functions in the dll very easily.

Does that work with C++ code too or just C?
On Windows, You can apparently works either with stdcall or cdecl functions.
"ctypes tries to protect you from calling functions with the wrong
number of arguments or the wrong calling convention. Unfortunately this
only works on Windows. It does this by examining the stack after the
function returns, so although an error is raised the function has been
called:" "To find out the correct calling convention you have to look
into the C header file or the documentation for the function you want to
call." I do not know if these are only C-isms or also C++-isms.
Aug 5 '08 #8
On 4 Aug., 15:14, brad <byte8b...@gmai l.comwrote:
RPM1 wrote:

...
Basically you just compile your C code as a regular C code dll. *ctypes
then allows you to access the functions in the dll very easily.

Does that work with C++ code too or just C?
It works if the interface of the DLL is C-style, that is you declare
your
functions with 'extern "C" { .... }' around them.
Inside your modules implementation you can use C++ without any
problems.

What works fine too, is f2py from numpy. It targets at wrapping
Fortran
code, but is able to wrap C code too, see

http://www.scipy.org/Cookbook/f2py_a...Py?action=show

Greetings, Uwe

Greetings, Uwe
Aug 5 '08 #9

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

Similar topics

6
2427
by: USCode | last post by:
Does Python have facilities for wrapping up the interpreter, any necessary modules, GUI library, python scripts, image files, etc. up into a single executable like exists for Tcl/Tk? e.g. http://freewrap.sourceforge.net/ http://www.equi4.com/starkit.html I've seen py2exe but it's not quite the same thing and doesn't appear to be as comprehensive as the above 2 are for Tcl/Tk. Thanks!
0
1340
by: Terry Hancock | last post by:
Hi all, Suppose you have found a really nice module that does what you want (or will do, it still being in development), and you don't want to waste a lot of duplicated effort, but this module is in Perl rather than Python (and yes, the author is not open to conversion -- already tried that ;-) ). If it were written in C, I'd have a couple of answers, such
13
3862
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
6
1864
by: Andrew Wilkinson | last post by:
Hi, I'm trying to wrap a C++ class hierarchy with Python types and I'd like to maintain the hierarchy in the types. I'm fairly sure this is possible, isn't it? Are there any documents explaining how to do this, the standard Python manual doesn't go into enough detail about creating types for this. I've found the tp_base and tp_bases elements and I've set them to the base
4
1803
by: Lawrence Oluyede | last post by:
I've never used metaclasses in real life before and while searching through the online Cookbook I found this gorgeous example: "Wrapping method calls (meta-class example)" http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078 What I want to do in my app is to log all method calls and seems that the metaclass example above do this fine but it fails when it's needed to instantiate the class I'm in in a method. So if I do:
3
2886
by: gabriel.becedillas | last post by:
Hi, I'm having problems wrapping a hierarchy of classes, actually having problems wrapping the base class. I don't need to use the WrapClass mechanism since I don't want to override classes in Python. My code boils down to: class Base { public: virtual ~Base()
3
5611
by: Paul Anton Letnes | last post by:
Hello guys, (related to previous thread on wrapping C/C++ in Python, trying the SWIG approach.) Trying to map a C++ class to python, one method for now. Running the following commands to "compile": -------------------------------------- #!/usr/bin/env bash
2
1259
by: Anish Chapagain | last post by:
Hi!! I'm new to python and have a task for Wrapping up an old program written in C(20.c files), to provide GUI and chart,graph feature in Python. I've tried using SWIG but am not getting well in windows system, wish to receive guidelines for initiating the task...
5
4310
by: Charlie | last post by:
Hi All, I am new to using swig/C++/python. I got some problem with function pointers. I posted in swig-user, but got no response. So I forwarded it here. You help is greatly appreciated. Thanks! ---------- Forwarded message ----------
0
9511
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
10410
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
9020
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
7529
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
6769
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
5418
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
3
2909
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.