[Pyrex] Passing C attribs between extension objects

David McNab david at rebirthing.co.nz
Thu Sep 13 22:58:22 CEST 2007


Hi,

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

But the compiler complains that it's not 'python compatible'.

In the past, I've taken the addresses of C attributes, casted them to
int and saved them as 'cdef int pSomeStruct' attributes, then casted
them back before use, like '<SomeStruct *>obj.pSomeStruct'. But as
architectures and compiler models get more diverse, I'm worried about
target scenarios where pointers can be bigger than ints.

I need to find an efficient means of access, because with the way I've
designed my extension classes, there are low level C API calls that
require attributes of 2 or more different extension objects. I'd prefer
not to change my extension classes to suit the API, because they're
providing some very useful abstractions.

So how can I safely and efficiently expose non-python-compatible, C
attributes of extension objects?

Cheers
David






More information about the Pyrex mailing list