(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].
__getbuffer__so that reference counting will work correctly:
infobeing the second argument) is initialized to
Noneautomatically at the beginning of the function.
info.objis typed as a Python object
info.objis decref-ed and set to
info.objis converted to a value of
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+.