[Python-3000] PyUnicodeObject implementation

Eric Smith eric at trueblade.com
Tue Sep 9 01:15:04 CEST 2008


Martin v. Löwis wrote:
>> I wouldn't mind letting Cython special case subtypes of str (or unicode in
>> Py3) *somehow*, as long as this "somewhow" proves to be a viable solution that
>> only applies to exactly those types *and* can be done realiably for subtypes
>> of subtypes. I'm just not aware of such a solution.
> 
> As people have pointed out: add new fields *after* the variable-sized
> members. To access it, you need to compute the length of the base
> object, and then cast the pointer to an extension struct.

How about putting the variable sized data _before_ the struct?  That is, 
make the memory layout:

<string data>
<PyObject fields>
<PyUnicodeObject fields>
<derived object fields>

Admittedly, accessing the string data is now more complex, since you 
have to know where it starts (which we already know, based on the size). 
But that might be simpler than having the offset logic when accessing 
derived object fields, because that would be different from all other C 
objects.

There would be some complications when allocating, because of alignment 
issues, but I don't think it would be impossible to do this. We'd need 
to be careful when deallocating, as well (of course).

Eric.


More information about the Python-3000 mailing list