Skip to content

enhancements refnanny

DagSverreSeljebotn edited this page · 10 revisions
Clone this wiki locally

Reference count nanny

The "refnanny" is a system used while testing Cython, to make sure that no refcount errors happen through the execution path the Cython testcases takes. It only tests the correct operation of Cython itself, not user code.

The basics

  • Many calls to Py_INCREF etc. are replaced with __Pyx_INCREF etc. (but not in utility code which is in general trusted and doesn't use the nanny)
  • New calls are added: __Pyx_GOTREF, __Pyx_GIVEREF, __Pyx_XGIVEREF, __Pyx_XGOTREF which are called after getting a reference from another function and before giving away a reference to a function/array/struct field. I.e:
PyList_SET_ITEM(..., obj, ...)


obj = Py_Get...
if (!obj) { ... }

(Also, XGIVEREF is called on __pyx_r prior to returning.)

One can get reference count errors (which are printed to output and/or logged in a module variable, see refnanny.pyx) for two reasons:
  • The required calls to __Pyx_GOTREF and __Pyx_GIVEREF is missing in a given situation. This is also a "breach of contract" and should be fixed, but there's no need to i.e. make new releases if such bugs are discovered, as they don't impact running code.
  • There's a genuine reference counting problem.

Running with the nanny

Default in, can be turned off by --no-refnanny.

If you want to run it on your own project, assuming you have a standard install of Cython (with the refnanny module in Cython.Runtime.refnanny), simply compile with -DCYTHON_REFNANNY.

Something went wrong with that request. Please try again.