Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91536 > unrolled thread

Building an extension module with SWIG

Started by"garyr" <garyr@fidalgo.net>
First post2015-05-30 09:22 -0700
Last post2015-05-30 18:54 -0700
Articles 6 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Building an extension module with SWIG "garyr" <garyr@fidalgo.net> - 2015-05-30 09:22 -0700
    Re: Building an extension module with SWIG Stefan Behnel <stefan_ml@behnel.de> - 2015-05-30 21:05 +0200
      Re: Building an extension module with SWIG "garyr" <garyr@fidalgo.net> - 2015-05-30 13:48 -0700
        Re: Building an extension module with SWIG "garyr" <garyr@fidalgo.net> - 2015-05-30 15:55 -0700
        Re: Building an extension module with SWIG Stefan Behnel <stefan_ml@behnel.de> - 2015-05-31 07:28 +0200
    Re: Building an extension module with SWIG "garyr" <garyr@fidalgo.net> - 2015-05-30 18:54 -0700

#91536 — Building an extension module with SWIG

From"garyr" <garyr@fidalgo.net>
Date2015-05-30 09:22 -0700
SubjectBuilding an extension module with SWIG
Message-ID<mkco9p$gf8$1@speranza.aioe.org>
I'm trying to create an extension module using SWIG. I've
succeeded in generating a pyd file but when I import the module I get the
error message: "SystemError: dynamic module not initialized properly." I
added an initfoo() function but that didn't solve the problem. Below are the
various files, a slightly modified version of a SWIG exmaple.
I'm using Python 2.7

What am I missing?

//foo.c:
#include "foo.h"
double Foo;
void initfoo()
{
    Foo = 3.0;
}
int gcd(int x, int y) {
  int g;
  g = y;
  while (x > 0) {
    g = x;
    x = y % x;
    y = g;
  }
  return g;
}

#foo.h:
extern void initfoo();
extern double Foo;
extern int gcd(int x, int y);

#foo.i:
%module example
%inline %{
extern int    gcd(int x, int y);
extern double Foo;
%}

#setup.py
from setuptools import setup, Extension
setup(name='foo',
    version='0.1',
    ext_modules=[Extension('foo', ['foo.c', 'foo.i'],
                    include_dirs=['.'],
                    depends=['foo.h', 'foo.i'],
                    swig_opts=['-modern', '-I../include'],
                    )],
    )


[toc] | [next] | [standalone]


#91538

FromStefan Behnel <stefan_ml@behnel.de>
Date2015-05-30 21:05 +0200
Message-ID<mailman.229.1433012748.5151.python-list@python.org>
In reply to#91536
garyr schrieb am 30.05.2015 um 18:22:
> I'm trying to create an extension module using SWIG. I've
> succeeded in generating a pyd file but when I import the module I get the
> error message: "SystemError: dynamic module not initialized properly." I
> added an initfoo() function but that didn't solve the problem. Below are the
> various files, a slightly modified version of a SWIG exmaple.
> I'm using Python 2.7
> 
> What am I missing?
> 
> //foo.c:
> #include "foo.h"
> double Foo;
> void initfoo()
> {
>     Foo = 3.0;
> }

This is wrong and you also won't need that.


> int gcd(int x, int y) {
>   int g;
>   g = y;
>   while (x > 0) {
>     g = x;
>     x = y % x;
>     y = g;
>   }
>   return g;
> }
> [...]

Just in case you're not bound to SWIG yet, here's a Cython [1] version of
your code:

    # put this in a file called "foo.pyx"

    def gcd(int x, int y):
        while x > 0:
            y, x = x, y % x
        return y

Compile it ("cythonize -b foo.pyx") and you'll get an extension module that
executes faster than what SWIG would give you and keeps everything in one
file to improve readability.

Stefan


[1] http://cython.org/

[toc] | [prev] | [next] | [standalone]


#91542

From"garyr" <garyr@fidalgo.net>
Date2015-05-30 13:48 -0700
Message-ID<mkd7nk$isi$1@speranza.aioe.org>
In reply to#91538
*snip*

> Compile it ("cythonize -b foo.pyx") and you'll get an extension module
> that
> executes faster than what SWIG would give you and keeps everything in one
> file to improve readability.
>
> Stefan
>
>
> [1] http://cython.org/
>
>
Thanks for your reply. My interest is not in computing the gcd but to learn
how build an extension module. I have some much more complicated C code I
wish to use.


[toc] | [prev] | [next] | [standalone]


#91548

From"garyr" <garyr@fidalgo.net>
Date2015-05-30 15:55 -0700
Message-ID<mkdf53$2fh$1@speranza.aioe.org>
In reply to#91542
"garyr" <garyr@fidalgo.net> wrote in message
news:mkd7nk$isi$1@speranza.aioe.org...
> *snip*
>
>> Compile it ("cythonize -b foo.pyx") and you'll get an extension module
>> that
>> executes faster than what SWIG would give you and keeps everything in one
>> file to improve readability.
>>
>> Stefan
>>
>>
>> [1] http://cython.org/
>>
>>
> Thanks for your reply. My interest is not in computing the gcd but to
> learn
> how build an extension module. I have some much more complicated C code I
> wish to use.
>
>
>
Thanks for your reply. I'm using the compiler that came with Anaconda Python
2.7. I too used SWIG a bunch of years ago but it has changed a lot since
then; e.g., it is now included in the Python distribution.


[toc] | [prev] | [next] | [standalone]


#91564

FromStefan Behnel <stefan_ml@behnel.de>
Date2015-05-31 07:28 +0200
Message-ID<mailman.239.1433050134.5151.python-list@python.org>
In reply to#91542
garyr schrieb am 30.05.2015 um 22:48:
> *snip*
> 
>> Compile it ("cythonize -b foo.pyx") and you'll get an extension module
>> that
>> executes faster than what SWIG would give you and keeps everything in one
>> file to improve readability.
>>
>> [1] http://cython.org/
> 
> Thanks for your reply. My interest is not in computing the gcd but to learn
> how build an extension module. I have some much more complicated C code I
> wish to use.

You can do that with Cython, too.

http://docs.cython.org/src/tutorial/external.html

http://docs.cython.org/src/tutorial/clibraries.html

I might be a bit biased as a core developer, but if the parts of you C
library's API for which you have an immediate use are not so tremendously
huge that it's entirely infeasible for you to write a nicely usable Python
API for them, I'd always recommend using Cython over a wrapper generator
like SWIG. Once you get to the points where it becomes interesting, you'll
always end up having more fun writing a Cython based integration layer than
fighting your up-hill battle against the way the wrapper generator wants
you to design it.

Stefan

[toc] | [prev] | [next] | [standalone]


#91557

From"garyr" <garyr@fidalgo.net>
Date2015-05-30 18:54 -0700
Message-ID<mkdpne$k16$1@speranza.aioe.org>
In reply to#91536
"garyr" <garyr@fidalgo.net> wrote in message 
news:mkco9p$gf8$1@speranza.aioe.org...
> I'm trying to create an extension module using SWIG. I've
> succeeded in generating a pyd file but when I import the module I get the
> error message: "SystemError: dynamic module not initialized properly." I
> added an initfoo() function but that didn't solve the problem. Below are 
> the
> various files, a slightly modified version of a SWIG exmaple.
> I'm using Python 2.7
>
> What am I missing?
>
> //foo.c:
> #include "foo.h"
> double Foo;
> void initfoo()
> {
>    Foo = 3.0;
> }
> int gcd(int x, int y) {
>  int g;
>  g = y;
>  while (x > 0) {
>    g = x;
>    x = y % x;
>    y = g;
>  }
>  return g;
> }
>
> #foo.h:
> extern void initfoo();
> extern double Foo;
> extern int gcd(int x, int y);
>
> #foo.i:
> %module example
> %inline %{
> extern int    gcd(int x, int y);
> extern double Foo;
> %}
>
> #setup.py
> from setuptools import setup, Extension
> setup(name='foo',
>    version='0.1',
>    ext_modules=[Extension('foo', ['foo.c', 'foo.i'],
>                    include_dirs=['.'],
>                    depends=['foo.h', 'foo.i'],
>                    swig_opts=['-modern', '-I../include'],
>                    )],
>    )
>
It's working! The first character of the name of the extension must be an 
underscore; e.g.,

>    ext_modules=[Extension('_foo', ['foo.c', 'foo.i'],
and the initfoo() function is not needed.

Thanks to all that replied to my post. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web