[Pyrex] Passing C attribs between extension objects

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Sep 14 06:38:41 CEST 2007


David McNab wrote:

> At the moment, I'm making such attributes available as property methods,
> which wrap the attribute via 'PyCObject_FromVoidPtr()', where the
> calling code accessing the attribute then unwraps it via
> 'PyCObject_AsVoidPtr()'.

That's about the best you can do, I think.

> In the past, I've taken the addresses of C attributes, casted them to
> int and saved them as 'cdef int pSomeStruct' attributes,

I don't see how that helps speed-wise, because to
access such an integer from Python, an int object
needs to be created for it, which is likely to be
about as expensive as creating a PyCObject.

You might be able to gain something by keeping a
cache of spare PyCObjects, like Python does with
ints (I think).

 > there are low level C API calls that
> require attributes of 2 or more different extension objects.

Not knowing the details of your API it's hard to
say, but I suspect you would be better off re-thinking
the problem from another angle. For instance, providing
functions that take the extension types directly and
extract the relevant values from them, rather than
leaving this to the calling Python code.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | Carpe post meridiem!          	  |
Christchurch, New Zealand	   | (I'm not a morning person.)          |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Pyrex mailing list