[Cython] CEP1000: Native dispatch through callables

Nathaniel Smith njs at pobox.com
Fri Apr 13 14:19:38 CEST 2012

On Fri, Apr 13, 2012 at 9:52 AM, Dag Sverre Seljebotn
<d.s.seljebotn at astro.uio.no> wrote:
> On 04/13/2012 01:38 AM, Robert Bradshaw wrote:
>> Also, I'm not sure the type registration will scale, especially if
>> every callable type wanted to get registered. (E.g. currently closures
>> and generators are new types...) Where to draw the line? (Perhaps
>> things could get registered lazily on the first __nativecall__ lookup,
>> as they're likely to be looked up again?)
> Right... if we do some work to synchronize the types for Cython modules
> generated by the same version of Cython, we're left with 3-4 types for
> Cython, right? Then a couple for numba and one for f2py; so on the order of
> 10?
> An alternative is do something funny in the type object to get across the
> offset-in-object information (abusing the docstring, or introduce our own
> flag which means that the type object has an additional non-standard field
> at the end).

In Python 2.7, it looks like there may be a few TP_FLAG bits free --
15 and 16 are labeled "reserved for stackless python", and 2, 11, 22
don't have anything defined.

There may also be an unused ssize_t field ob_size at the beginning of
the type object -- for some reason PyTypeObject is declared as
variable size (using PyObject_VAR_HEAD), but I don't see any
variable-size fields in it, the docs claim that the ob_size field is a
"historical artifact that is maintained for binary
compatibility...Always set this field to zero", and Include/object.h
has a definition for a PyHeapTypeObject which has a PyTypeObject as
its first member, which would not work if PyTypeObject had variable
size. Grep says that the only place where ob_type->ob_size is accessed
is in Objects/typeobject.c:object_sizeof(), which at first glance
appears to be a bug, and anyway I don't think anyone cares whether
__sizeof__ on C-callable objects is exactly correct. One could use
this for an offset, or even a pointer.

One could also add a field easily by just subclassing PyTypeObject.

The Signature thing seems like a distraction to me. Signature is
intended as just a nice convenient format for looking up stuff that's
otherwise stored in more obscure ways -- the API equivalent of
pretty-printing. The important thing here is getting the C-level
dispatch right.

-- Nathaniel

More information about the cython-devel mailing list