[Pyrex] defining

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Sep 20 05:24:42 CEST 2005


Fabien Cromières wrote:

> Basically, here is what I want to do (and don't manage to):
> I would like to define a struct object that contains a 1 field that
> contains a reference to a function returning a python object.

If you mean a C function, there's nothing wrong with that.
The following passes the Pyrex compiler just fine:

   cdef struct foo:
     object (*blarg)()

If you mean a Python function, you're out of luck,
because Python functions are themselves Python objects,
and a C struct in Pyrex can't contain members of type
'object'. (The reason for this is that Pyrex wouldn't
be able to keep track of the reference counting
properly.)

Having said that, it is possible to store a reference
to a Python object in a struct if you cast it to some
other type first (such as void * or PyObject *). But
then you're on your own about managing the reference
counts.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+




More information about the Pyrex mailing list