[Pyrex] Re: A "main" function for Pyrex: cimport does not work

len-l at telus.net len-l at telus.net
Thu Apr 14 23:25:16 CEST 2005


On 14 Apr 2005 at 11:33, Alain Pointdexter wrote:

> This is a follow-up for the post from Lenard
> Lindstrom. I experimented with his code and it indeed
> works. However, when i tried to elaborate on it and
> import a Pyrex module from the main function, i get a
> segmentation fault. 

A quick test importing zlib and one of my Pyrex modules
from within main() caused no problems for me ( Pyrex 0.9.3,
VC6 and Python 2.3).

> There seems to be no way to cimport modules except for
> including them verbatim, which of course is unfeasible
> for any serious project.
>
This simple example taken from the Pyrex docs ran for me:

============ dishes.pxd =================
cdef enum otherstuff:
    sausage, eggs, lettuce

cdef struct spamdish:
    int oz_of_spam
    otherstuff filler
============ restaurant.pyx ===============
include "main.pxi"

cimport dishes
from dishes cimport spamdish

cdef void prepare(spamdish *d):
    d.oz_of_spam = 42
    d.filler = dishes.sausage

def serve():
    cdef spamdish d
    prepare(&d)
    print "%d oz spam, filler no. %d." % \
          (d.oz_of_spam, d.filler)

def main(arglist):
    serve()
    return 0
=========================================

dishes.pxd and restaurant.pyx were in the same directory.

> PS: I forgot to mention. The post i am talking about
> is from October 2004. 
>
This is just a guess but how are you defining the C macro
__pymoduleinit__? In the above restaurant.pyx example
__pymoduleinit__ must be defined as initrestaurant, the name
of the module initialization function that Pyrex generates. If
this function is not called then any module level stuff will not
work. The macro can be defined with a command line option
to the C compiler. Of course without it the code would probably
not compile.

I must admit though I did post the code without any real testing
so there could be serious limitations. It was to show that Pyrex
could be more than just a language for writing Python extension
modules. I apologize if this has wasted anyone's time.

Lenard Lindstrom
<len-l at telus.net>

P.S. Sorry Alain for accidently sending the first copy of this
directly to your mailbox. My current e-mail client does not
handle mailing lists as automatically as an earlier one I used.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.copyleft.no/pipermail/pyrex/attachments/20050414/6cca4e17/attachment.html


More information about the Pyrex mailing list