Python C API and references

lallous lallous at lgwm.org
Thu Nov 12 04:23:54 EST 2009


Hello,

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?

So for example:

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

Right?

If so, take sysmodule.c:

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

Shouldn't they also call Py_DECREF(Py_None) ?

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

--
Elias 




More information about the Python-list mailing list