[Pyrex] referencing the module object (__pyx_m aka module_cname)?

Lenard Lindstrom len-l at telus.net
Sun Nov 7 00:35:34 CET 2004


On Fri, 5 Nov 2004 23:45:36 +0100 Yann Vernier <yann at donkey.dyndns.org> wrote:

> I'm trying to replace a C/Python module called "Text", which is
> statically linked and initialized by a function called Text_Init.
> So far that's pretty simple, but the function needs to return the newly
> created Module Object. I found it in the generated C code, it's called
> __pyx_m, but I can't seem to access it properly.
> 
> This part works, but the "module" name needs a working declaration:
> 
> # initText is the initializer for dynamic loading
> cdef public void initText() except *
> # Text_Init is called when this is linked statically
> cdef public object Text_Init():
>   initText()
>   return module
> 
> I've tried a few combinations:
>   cdef extern object __pyx_m
>     No go, Pyrex says Python Objects can't be extern.
>   cdef object __pyx_m
>     This causes a duplicate declaration, which is fine, and an
>     initialization to None which is not. Segfault due to that
>     overlapping initialization.
>   cdef public object __pyx_m
>     This uses some DL_EXPORT magic, and there will be colliding symbols
>     from other modules in that case. Also initalizes as above.
> 
> How can this be done?
> 
I have compliled the following but did not try to test it.

cdef extern from "python.h":
    ctypedef struct PyObject
    PyObject *__pyx_m  # pretend it is defined in python.h

cdef public void initText() except *

cdef public PyObject *Text_Init():
  initText()
  return __pyx_m


I assume Text_Init returns a borrowed reference. Since __pyx_m is itself a
borrowed reference it should be okay to return it as Text_Init's value
without incrementing the reference first. So __pyx_m and Text_Init are
declared using PyObject * rather than object.

An asside. __pyx_m is likely intended for internal use only so could disappear
in future versions of Pyrex.

Lenard Lindstrom
<len-l at telus.net>





More information about the Pyrex mailing list