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


Groups > comp.lang.python > #91538

Re: Building an extension module with SWIG

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: Building an extension module with SWIG
Date 2015-05-30 21:05 +0200
References <mkco9p$gf8$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.229.1433012748.5151.python-list@python.org> (permalink)

Show all headers | View raw


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/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web