[Cython] Add support for the offsetof() C macro

Nikita Nemkin nikita at nemkin.ru
Wed Apr 3 12:53:50 CEST 2013


Hi,

> offsetof() is not supported by current Cython, and I have not found any
> workaround (except hardcoding offsets for a specific architecture and
> compiler, but this is obviously wrong).

offsetof() would certainly be very useful, but in the meantime
offsetof(Struct, field) can be replaced with:

     <Py_ssize_t>&(<Struct*>NULL).field

It's not ANSI C, but is portable enough.

Another option (for extern or public structs only) is to abuse
renaming:

     enum:
         Struct_offsetof_field1 "offsetof(Struct, field1)"

This way a symbolic name can be given to any expression
to be literally pasted into the generated code.


Best regards,
Nikita Nemkin


More information about the cython-devel mailing list