|
From: Sasha <nd...@ma...> - 2006-02-16 21:41:23
|
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=20
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 =3D=3D NULL) {descr=3DNULL;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=3DNULL
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.
|