refcounting

S

Simon Dahlbacka

I'm making a C extension where I'm wrapping a C function like follows

void MyFunction(/*[in,out]*/ ArrayType array)

to

a python function taking a list as inparameter and returning a list (for
consistency with other pointer arguments)

Now to the question..

I'm a little confused about the reference counting..

static PyObject* MyFunction(PyObject * /*self*/, PyObject *args) {

PyObject *list;
PyArg_ParseTuple(args, &PyList_Type, &list);

//do stuff with list elements

// XXX do I need Py_INCREF(list); here ???
return list;
}

obviously error checking is omitted for brevity.

PS. are there any documentation explaining the reference counting issues in
more detail than the docs @ python.org ?
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Simon said:
I'm a little confused about the reference counting..

static PyObject* MyFunction(PyObject * /*self*/, PyObject *args) {

PyObject *list;
PyArg_ParseTuple(args, &PyList_Type, &list);

//do stuff with list elements

// XXX do I need Py_INCREF(list); here ???
return list;

Yes. PyArg_ParseTuple returns a borrowed reference to the list; the
reference is originally help by the argument tuple (which is immutable,
so the reference is guaranteed to stay while you hold on to the argument
tuple, which is only decrefed in the caller of MyFunction).

The result must be a new reference, so you must incref.
PS. are there any documentation explaining the reference counting issues in
more detail than the docs @ python.org ?

No. For the specific issue, the documentation says it all:

http://docs.python.org/api/arg-parsing.html

says

"O" (object) [PyObject *] ... The object's reference count is not increased.

http://docs.python.org/api/common-structs.html#l2h-821

says

PyCFunction ... The function must return a new reference.

HTH,
Martin
 
S

Simon Dahlbacka

Martin v. Löwis said:
Simon said:
I'm a little confused about the reference counting..

static PyObject* MyFunction(PyObject * /*self*/, PyObject *args) {

PyObject *list;
PyArg_ParseTuple(args, &PyList_Type, &list);

//do stuff with list elements

// XXX do I need Py_INCREF(list); here ???
return list;

Yes. PyArg_ParseTuple returns a borrowed reference to the list; the
reference is originally help by the argument tuple (which is immutable,
so the reference is guaranteed to stay while you hold on to the argument
tuple, which is only decrefed in the caller of MyFunction).

The result must be a new reference, so you must incref.
PS. are there any documentation explaining the reference counting issues in
more detail than the docs @ python.org ?

No. For the specific issue, the documentation says it all:

http://docs.python.org/api/arg-parsing.html

says

"O" (object) [PyObject *] ... The object's reference count is not increased.

http://docs.python.org/api/common-structs.html#l2h-821

says

PyCFunction ... The function must return a new reference.

thanks for clarifying. Now I think that I understand (a bit more at
least)
Somehow I had managed to miss the common structs page (and especially
the part about "the function must return a new reference")

/Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top