En Thu, 12 Nov 2009 06:23:54 -0300, lallous <lall...@lgwm.org> escribió:

Everytime I use PyObject_SetAttrString(obj, attr_name, py_val) and I don't need the reference to py_val I should decrement the reference after this call?

If you own a reference to py_val, and you don't need it anymore, you must
decrement it. It doesn't matter if you call PyObject_SetAttrString or
whatever, except when the called function says it "steals" a reference.

So for example:

PyObject *py_val = PyInt_FromLong(5)
PyObject_SetAttrString(py_obj, "val", py_val);
Py_DECREF(py_val)

Right?

Yes, because PyInt_FromLong returns a new reference, and you own it.

If so, take sysmodule.c:

        if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
                return NULL;

Shouldn't they also call Py_DECREF(Py_None) ?

No, because the reference count of Py_None was not incremented previously;
the code doesn't own a reference to Py_None at that time. It's not the
same as the example above.

Same logic applies to PyDict_SetItemString() and the reference should be decrement after setting the item (ofcourse if the value is not needed).

Yes, same as your first example.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to