[Pyrex] Passing C attribs between extension objects

Stefan Behnel stefan_ml at behnel.de
Fri Sep 14 10:20:07 CEST 2007


David McNab wrote:
> Is there an efficient safe way of making non-python-compatible C
> attributes of extension objects available outside the extension class
> definition?
> 
> 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()'.
> 
> This works, it's pretty safe, but has a hefty overhead - every time I
> access a C attribute of an extension object, it has to be wrapped into a
> new Python object, then unwrapped by the caller. This is hell on
> time-critical code.
> 
> I tried making the C attribute more directly accessible, eg:
> 
>    cdef public y4m_frame_info_t frameinfo
> 
> or
> 
>    cdef public y4m_frame_info_t *frameinfo

Personally, I'd rethink your API and just pass the Python wrappers that
already have this attribute (like the above class), instead of additionally
creating a Python wrapper just for the C type each time you need to pass it on.

If you have more than one class that carries it, think about a common
superclass that holds it and make that the API level object you pass on.

Stefan




More information about the Pyrex mailing list