[Cython] [cython] Fix __Pyx_ImportType if #Py_LIMITED_API is defined. (#171)

Stefan Behnel stefan_ml at behnel.de
Thu Dec 20 07:24:57 CET 2012

Hi Bradley,

Bradley M. Froehle, 19.12.2012 21:31:
> I was recently trying to use mpi4py in a hand-written Python 3 extension
> module with `#define Py_LIMITED_API`.

Work on getting Cython play nicely with Py_LIMITED_API is certainly
appreciated. In most cases, it should be as simple as adding another #ifdef
to utility functions that are already written in plain C anyway. A lot of
the optimisations will need this.

> One side effect of the limited
> API is that `PyTypeObject` becomes an opaque type.
> This means that our `__Pyx_ImportType` cannot just introspect 
> `tp_basicsize`.
> I've updated the code to extract the `tp_basicsize` value from the 
> `__basicsize__` attribute which should be available instead.
> I'd appreciate comments on the code style here... I'm not sure how to 
> write these sorts of calls most efficiently yet.

I think it's ok. The only issue is that PyObject_GetAttrString() creates an
intermediate string object from the char* you pass. Py3.3 (or 3.2+?) has a
mechanism for avoiding this. We could start using that if available, or
maybe even copy it over into Cython to use it in older versions as well.

Anyway, this is code that runs at import time - I don't think it's any
performance critical (and if a whole import works, the memory allocated for
a tiny string can't be an issue either...).


More information about the cython-devel mailing list