Wrapping C with Python

Discussion in 'Python' started by Anish Chapagain, Aug 4, 2008.

  1. 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\swig> swig -
    python example.i
    It creates example.py and example_wrap.c

    Step 4:
    I compiled example.c and example_wrap.c individually
    c:\python25\mingw\bin> gcc -c example.c (it create
    example.o)
    c:\python25\mingw\bin> gcc -c example_wrap.c -Ic:
    \python25\include (it create example_wrap.o)

    Step 5:
    building .pyd for windows,
    c:\python25\mingw\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
     
    Anish Chapagain, Aug 4, 2008
    #1
    1. Advertising

  2. Anish Chapagain

    RPM1 Guest

    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
     
    RPM1, Aug 4, 2008
    #2
    1. Advertising

  3. Anish Chapagain

    brad Guest

    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?
     
    brad, Aug 4, 2008
    #3
  4. On 4 Aug, 14:14, 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?


    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
     
    Anish Chapagain, Aug 4, 2008
    #4
  5. 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
     
    Stefan Behnel, Aug 4, 2008
    #5
  6. Anish Chapagain

    RPM1 Guest

    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
     
    RPM1, Aug 4, 2008
    #6
  7. Anish Chapagain

    RPM1 Guest

    Anish Chapagain wrote:
    > On 4 Aug, 14:14, 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?

    >
    > 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
     
    RPM1, Aug 4, 2008
    #7
  8. Anish Chapagain

    Terry Reedy Guest

    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.
     
    Terry Reedy, Aug 5, 2008
    #8
  9. Anish Chapagain

    Uwe Schmitt Guest

    On 4 Aug., 15:14, 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?


    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_and_NumPy?action=show

    Greetings, Uwe

    Greetings, Uwe
     
    Uwe Schmitt, Aug 5, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. USCode

    Wrapping Python ?

    USCode, Sep 25, 2004, in forum: Python
    Replies:
    6
    Views:
    402
    Roger Binns
    Sep 26, 2004
  2. Roy Smith

    Wrapping a C library in Python

    Roy Smith, Nov 19, 2004, in forum: Python
    Replies:
    13
    Views:
    750
    Mark Asbach
    Nov 22, 2004
  3. Andrew Wilkinson

    Wrapping C++ Class Heirachy in Python

    Andrew Wilkinson, Apr 18, 2005, in forum: Python
    Replies:
    6
    Views:
    473
    Andrew Wilkinson
    Apr 19, 2005
  4. Martin P. Hellwig
    Replies:
    3
    Views:
    503
    Martin P. Hellwig
    Jul 15, 2006
  5. Paul Anton Letnes

    Re: wrapping C functions in python

    Paul Anton Letnes, Apr 10, 2008, in forum: Python
    Replies:
    2
    Views:
    319
    Ivan Illarionov
    Apr 10, 2008
Loading...

Share This Page