[Pyrex] PyStringObject cannot be subclassed in Cython/Pyrex

Stefan Behnel stefan_ml at behnel.de
Fri Jan 11 11:38:48 CET 2008


Hi,

I tried subclassing PyStringObject in Pyrex and noticed that it cannot work
due to the way the type is set up by Python. Its tp_new() uses tp_alloc(t,X)
internally, where X is the length of the string. So it determines the size of
the type struct at runtime. The way Pyrex subclasses types is by appending its
own members to the /compile-time/ struct, which leads to a double use of
memory here. The result is a string value that gets overwritten by Pyrex' own
tp_new().

Here is an example:

--------------------------------
cdef extern from "Python.h":
    ctypedef class __builtin__.str [object PyStringObject]:
        pass

cdef class SubEmpty(str):
    pass

cdef class Sub(str):
    cdef object test

init_value = "test-test-test"

t = SubEmpty(init_value) # this works
print init_value, t

t = Sub(init_value)      # this initialises t with a broken string
print init_value, t
--------------------------------

PyUnicodeObject works different, so that leaves me a chance to work around it.
Still, it's a general problem that conflicts with the way type setup in Python
works (or can work). Luckily, this is rarely used by builtin types, but it is
nevertheless in use and I imagine that it's pretty hard to work around for Pyrex.

Stefan




More information about the Pyrex mailing list