[Python-3000] PyUnicodeObject implementation

Antoine Pitrou solipsis at pitrou.net
Mon Sep 8 12:19:54 CEST 2008


Stefan Behnel <stefan_ml <at> behnel.de> writes:
> 
>     cdef class MyListSubType(PyListObject):
>         cdef int some_additional_int_field
>         cdef my_struct* some_struct
> 
>         def __init__(self):
>             self.some_struct = get_the_struct_pointer(...)
>             self.some_additional_int_field = 1

In your example, you could wrap the additional fields (additional_int_field and
some_struct) in a dedicated struct, and define a macro which gives a pointer to
this struct when given the address of the object. Once you have the pointer to
the struct, accessing additional fields is as simple as in the non-PyVarObject
case.

Something like (pseudocode):

#define MyStrSubType_FIELDS_ADDR(op) \
  ((struct MyStrSubType_subfields*) &((void*)op + PyString_Type->tp_basicsize \
      + op->size * PyString_Type->tp_itemsize))

It's not as trivially cheap as a straight field access, but much less expensive
than a dictionary lookup.

(perhaps this needs to be a bit more complicated if you want a specific
alignment for your fields)





More information about the Python-3000 mailing list