[Pyrex] [Cython-dev] PyStringObject cannot be subclassed in Cython/Pyrex

Robert Bradshaw robertwb at math.washington.edu
Fri Jan 11 18:48:26 CET 2008


I would just add a note that PyStringObject can't be (cdef)  
subclassed. Trying to provide a workaround for this specific case  
would be (IMHO) more work than it's worth, especially since one can  
use PyUnicodeObject (hopefully) if one really needs to.

- Robert

On Jan 11, 2008, at 2:38 AM, Stefan Behnel wrote:

> 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
>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at lists.berlios.de
> https://lists.berlios.de/mailman/listinfo/cython-dev




More information about the Pyrex mailing list