[Numpy-discussion] Reference counting question

Sasha ndarray at mac.com
Thu Feb 16 13:42:06 CST 2006


I was looking at the code implementing array_new in arrayobject.c and
for a while I could not convince myself that it handles ref. counts
correctly.  The cleanup code (at the "fail:" label contains 
Py_XDECREF(descr), meaning that descr is unreferenced on failure
unless it is NULL.  This makes sense because descr is created inside
array_new  by PyArray_DescrConverter,  but  if the failure is detected
in PyArg_ParseTupleAndKeywords, descr may be NULL.    What was
puzzling to me,  failures of  PyArray_NewFromDescr are handled by "if
(ret == NULL) {descr=NULL;goto fail;}" that sets descr to NULL before
jumping to cleanup.  As I investigated further, I've discovered the
following helpful comment preceding PyArray_NewFromDescr : /* steals a
reference to descr (even on failure) */ that explains  why descr=NULL
is necessary.

I wonder what was the motivation for this design choice.  I don't
think this is a natural behavior for python C-API functions.  I am not
proposing to make any changes, just curious about the design.




More information about the Numpy-discussion mailing list