[Tutor] Refcount in C extensions

Izz ad-Din Ruhulessin izzaddin.ruhulessin at gmail.com
Fri Jan 14 17:52:56 CET 2011


Hello,

I am writing a Python C extension and I have some trouble understanding how
reference counting works exactly. Though I think I understand the practice
on simple operations (see my question at stackoverflow:
http://stackoverflow.com/questions/4657764/py-incref-decref-when), but on
more "complex" operations I cannot quite grasp it yet.

For example, in the following helper function, do I need to DECREF or are
all the references automatically destroyed when the function returns?

double Py_GetAttr_DoubleFromFloat(PyObject *obj, const char *attr)
{
if ((PyObject_GetAttrString(obj, attr) == False) ||
(PyObject_HasAttrString(obj, attr) == False)) {
return -9999.0;
}

return PyFloat_AsDouble(PyNumber_Float(PyObject_GetAttrString(obj, attr)));
}

Please share your thoughts, thanks in advance, kind regards,

Izz ad-Din
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110114/6e431294/attachment.html>


More information about the Tutor mailing list