bpo-1635741: Add PyModule_AddObjectRef() function (GH-23122) · python/cpython@8021875
@@ -264,7 +264,7 @@ of the following two module creation functions:
264264 instead; only use this if you are sure you need it.
265265266266Before it is returned from in the initialization function, the resulting module
267-object is typically populated using functions like :c:func:`PyModule_AddObject`.
267+object is typically populated using functions like :c:func:`PyModule_AddObjectRef`.
268268269269.. _multi-phase-initialization:
270270@@ -437,26 +437,102 @@ a function called from a module execution slot (if using multi-phase
437437initialization), can use the following functions to help initialize the module
438438state:
439439440+.. c:function:: int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
441+442+ Add an object to *module* as *name*. This is a convenience function which
443+ can be used from the module's initialization function.
444+445+ On success, return ``0``. On error, raise an exception and return ``-1``.
446+447+ Return ``NULL`` if *value* is ``NULL``. It must be called with an exception
448+ raised in this case.
449+450+ Example usage::
451+452+ static int
453+ add_spam(PyObject *module, int value)
454+ {
455+ PyObject *obj = PyLong_FromLong(value);
456+ if (obj == NULL) {
457+ return -1;
458+ }
459+ int res = PyModule_AddObjectRef(module, "spam", obj);
460+ Py_DECREF(obj);
461+ return res;
462+ }
463+464+ The example can also be written without checking explicitly if *obj* is
465+ ``NULL``::
466+467+ static int
468+ add_spam(PyObject *module, int value)
469+ {
470+ PyObject *obj = PyLong_FromLong(value);
471+ int res = PyModule_AddObjectRef(module, "spam", obj);
472+ Py_XDECREF(obj);
473+ return res;
474+ }
475+476+ Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
477+ this case, since *obj* can be ``NULL``.
478+479+ .. versionadded:: 3.10
480+481+440482.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
441483442- Add an object to *module* as *name*. This is a convenience function which can
443- be used from the module's initialization function. This steals a reference to
444- *value* on success. Return ``-1`` on error, ``0`` on success.
484+ Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to
485+ *value* on success (if it returns ``0``).
486+487+ The new :c:func:`PyModule_AddObjectRef` function is recommended, since it is
488+ easy to introduce reference leaks by misusing the
489+ :c:func:`PyModule_AddObject` function.
445490446491 .. note::
447492448- Unlike other functions that steal references, ``PyModule_AddObject()`` only
449- decrements the reference count of *value* **on success**.
493+ Unlike other functions that steal references, ``PyModule_AddObject()``
494+ only decrements the reference count of *value* **on success**.
450495451496 This means that its return value must be checked, and calling code must
452- :c:func:`Py_DECREF` *value* manually on error. Example usage::
453-454- Py_INCREF(spam);
455- if (PyModule_AddObject(module, "spam", spam) < 0) {
456- Py_DECREF(module);
457- Py_DECREF(spam);
458- return NULL;
459- }
497+ :c:func:`Py_DECREF` *value* manually on error.
498+499+ Example usage::
500+501+ static int
502+ add_spam(PyObject *module, int value)
503+ {
504+ PyObject *obj = PyLong_FromLong(value);
505+ if (obj == NULL) {
506+ return -1;
507+ }
508+ if (PyModule_AddObject(module, "spam", obj) < 0) {
509+ Py_DECREF(obj);
510+ return -1;
511+ }
512+ // PyModule_AddObject() stole a reference to obj:
513+ // Py_DECREF(obj) is not needed here
514+ return 0;
515+ }
516+517+ The example can also be written without checking explicitly if *obj* is
518+ ``NULL``::
519+520+ static int
521+ add_spam(PyObject *module, int value)
522+ {
523+ PyObject *obj = PyLong_FromLong(value);
524+ if (PyModule_AddObject(module, "spam", obj) < 0) {
525+ Py_XDECREF(obj);
526+ return -1;
527+ }
528+ // PyModule_AddObject() stole a reference to obj:
529+ // Py_DECREF(obj) is not needed here
530+ return 0;
531+ }
532+533+ Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
534+ this case, since *obj* can be ``NULL``.
535+460536461537.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
462538