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

Yann Vernier yann at donkey.dyndns.org
Sun Nov 7 01:00:38 CET 2004


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.

-- 
PGP fingerprint = 9242 DC15 2502 FEAB E15F  84C6 D538 EC09 5380 5746
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20041107/1742de80/attachment.bin


More information about the Pyrex mailing list