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

Phillip J. Eby pje at telecommunity.com
Sun Nov 7 03:26:13 CET 2004


At 01:00 AM 11/7/04 +0100, Yann Vernier wrote:
>On Sat, Nov 06, 2004 at 03:35:34PM -0800, Lenard Lindstrom wrote:
> > 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
>
>This is very close to what I wound up doing:
>
>cdef extern from *:     # These are declared "elsewhere"; actually, in Pyrex.
>   void *module "__pyx_m"
># But this one still needs a prototype.. *sigh*
>cdef public void initText() #except *
># Blender uses these
>cdef public object Text_Init():
>   initText()
>   # Blender does not check for exceptions in init, so I did this to read them
>   if PyErr_Occurred():
>     PyErr_Print()
>   # Note: Unlike the old Blender.Text module, we keep another reference 
> ourselves.
>   return <object>module
>
> > 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.
>
>Yes, I suppose it is a borrowed reference.. though the <object> cast
>makes it a normal reference. This makes no difference as it is
>statically allocated and linked anyway.
>
> > An asside. __pyx_m is likely intended for internal use only so could 
> disappear
> > in future versions of Pyrex.
>
>I understand that. If I convert all the modules to Pyrex I may not even
>need it. As Pyrex object basically equals C PyObject* with reference
>counting properly done, I think it really should be possible to declare
>them extern. Perhaps a qualifier for borrowed makes sense. Then I would
>not need that unseemly cast.

Why can't you just use 'import' to get the module object?  Wouldn't that be 
simpler than all this stuff?




More information about the Pyrex mailing list