This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hniksic
Recipients hniksic
Date 2008-01-10.14:47:49
SpamBayes Score 0.0045175436
Marked as misclassified No
Message-id <1199976472.88.0.939409341911.issue1782@psf.upfronthosting.co.za>
In-reply-to
Content
PyModule_AddObject has somewhat strange reference-counting behavior in
that it *conditionally* steals a reference.  In case of error it doesn't
change the reference to the passed object, but in case of success it
steals it.  This means that, as written, PyModule_AddIntConstant and
PyModuleAddStringConstant can leak created objects if PyModule_AddObject
fails.

As far as I can tell, the correct way to write those functions would be
(using PyModule_AddIntConstant as the example):

int 
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
        PyObject *o = PyInt_FromLong(value);
	if (PyModule_AddObject(m, name, o) == 0)
                return 0;
        Py_XDECREF(o);
        return -1;
}

PyModule_AddObject was obviously intended to enable writing the "simple"
code (it even gracefully handles being passed NULL object to add) like
the one in PyModule_AddIntConstant, but I don't see a way to enable such
usage and avoid both leaks and an interface change.  Changing the
reference-counting behavior of PyModule_AddObject would be
backward-incompatible, but it might be a good idea to consider it for
Python 3.

If there is agreement that my analysis and the proposed fixes are
correct, I will produce a proper patch.
History
Date User Action Args
2008-01-10 14:47:53hniksicsetspambayes_score: 0.00451754 -> 0.0045175436
recipients: + hniksic
2008-01-10 14:47:52hniksicsetspambayes_score: 0.00451754 -> 0.00451754
messageid: <1199976472.88.0.939409341911.issue1782@psf.upfronthosting.co.za>
2008-01-10 14:47:50hniksiclinkissue1782 messages
2008-01-10 14:47:49hniksiccreate