DECREFing and PyArray functions in C Extensions

Christian Tismer tismer at tismer.com
Sun May 28 11:24:09 EDT 2000


Roger Hansen wrote:
...
> OK! Lets say I have a Python program with a NumPy array and a loop,
> and for efficiency reasons I decide to write the loop as an extension,
> like this:
> 
> static PyObject *
> foo(PyObject *self, PyObject* args)
> {
>   PyArrayObject *array;
>   double* a;            /* C ptr to the NumPy array data field */
> 
>   if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &array)) {
>     return NULL;  /* Error indicator */
>   }
...
> I don't need to DECREF array in this
> example since PyArg_ParseTuple does not increase the reference count,
> right?

Right. PyArg_ParseTuple just parsed the tuple and does not
create extra references.
The creator of the args tuple is responsible to decref it
later on, and when it is disposed by that, the extra
references to its elements are removed automatically.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com




More information about the Python-list mailing list