Clone this wiki locally
Cython special class methods
(Or some that are not in Pyrex anyway. This documentation should probably be moved somewhere appropriate.)
#!python cdef class MyClass: def __getbuffer__(self, Py_buffer* info, int flags): info.obj = self # do this if you want __releasebuffer__ called on "self" ... def __releasebuffer__(self, Py_buffer* info): # never change info.obj here! ...
These let your class export a buffer as described in [http://www.python.org/dev/peps/pep-3118/ PEP-3118].
- There is some special treatment of
__getbuffer__so that reference counting will work correctly:
- The field
infobeing the second argument) is initialized to
Noneautomatically at the beginning of the function.
info.objis typed as a Python object
- If the function exits with an exception,
info.objis decref-ed and set to
- If the function exits normally, a value of
info.objis converted to a value of
- The field
Note that within
__releasebuffer__, you must not change
info.obj (there's no reason you should either), as this will decref the object. When
__releasebuffer__ is called Python is already on its way to decref
info.obj and so if you reassign it there will be one decref too many (and a segfault may happen later).
All Cython-generated classes have
Py_TPFLAGS_HAVE_NEWBUFFER set under Python 2.6+.