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 serhiy.storchaka
Recipients serhiy.storchaka, vstinner
Date 2020-11-11.20:35:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
There is a design flaw in PyModule_AddObject(). It steals reference of its value only if the result is success. To avoid leaks it should be used in the following form:

    PyObject *tmp = <new reference>;
    if (PyModule_AddObject(name, name, tmp) < 0) {
        goto error;

It is inconvenient and many code forgot to use a temporary variable and call Py_XDECREF().

It was not intention, but it is too late to change this behavior now, because some code calls Py_XDECREF() if PyModule_AddObject() fails. Fixing PyModule_AddObject() now will break hard such code.

There was an idea to make the change gradual, controlled by a special macro (see issue26871). But it still has significant risk.

I propose to add new function PyModule_Add() which always steals reference to its argument. It is more convenient and allows to get rid of temporary variable:

    if (PyModule_Add(name, name, <new reference>) < 0) {
        goto error;

I choose name PyModule_Add because it is short, and allow to write the call in one line with moderately long expression <new reference> (like PyFloat_FromDouble(...) or PyLong_FromUnsignedLong(...)).
Date User Action Args
2020-11-11 20:35:14serhiy.storchakasetrecipients: + serhiy.storchaka, vstinner
2020-11-11 20:35:14serhiy.storchakasetmessageid: <>
2020-11-11 20:35:14serhiy.storchakalinkissue42327 messages
2020-11-11 20:35:14serhiy.storchakacreate