@@ -486,12 +486,29 @@ state:
486486 .. versionadded :: 3.10
487487
488488
489+ .. c :function :: int PyModule_Add (PyObject *module, const char *name, PyObject *value)
490+
491+ Similar to :c:func: `PyModule_AddObjectRef `, but "steals" a reference
492+ to *value *.
493+ It can be called with a result of function that returns a new reference
494+ without bothering to check its result or even saving it to a variable.
495+
496+ Example usage::
497+
498+ if (PyModule_Add(module, "spam", PyBytes_FromString(value)) < 0) {
499+ goto error;
500+ }
501+
502+ .. versionadded :: 3.13
503+
504+
489505.. c :function :: int PyModule_AddObject (PyObject *module, const char *name, PyObject *value)
490506
491507 Similar to :c:func: `PyModule_AddObjectRef `, but steals a reference to
492508 *value * on success (if it returns ``0 ``).
493509
494- The new :c:func:`PyModule_AddObjectRef` function is recommended, since it is
510+ The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef`
511+ functions are recommended, since it is
495512 easy to introduce reference leaks by misusing the
496513 :c:func:`PyModule_AddObject` function.
497514
@@ -501,44 +518,24 @@ state:
501518 only decrements the reference count of *value* **on success**.
502519
503520 This means that its return value must be checked, and calling code must
504- :c:func:`Py_DECREF ` *value* manually on error.
521+ :c:func:`Py_XDECREF ` *value* manually on error.
505522
506523 Example usage::
507524
508- static int
509- add_spam(PyObject *module, int value)
510- {
511- PyObject *obj = PyLong_FromLong(value);
512- if (obj == NULL) {
513- return -1;
514- }
515- if (PyModule_AddObject(module, "spam", obj) < 0) {
516- Py_DECREF(obj);
517- return -1;
518- }
519- // PyModule_AddObject() stole a reference to obj:
520- // Py_DECREF(obj) is not needed here
521- return 0;
522- }
523-
524- The example can also be written without checking explicitly if *obj * is
525- ``NULL ``::
525+ PyObject *obj = PyBytes_FromString(value);
526+ if (PyModule_AddObject(module, " spam" , obj) < 0 ) {
527+ // If 'obj' is not NULL and PyModule_AddObject () failed,
528+ // 'obj' strong reference must be deleted with Py_XDECREF ().
529+ // If 'obj' is NULL, Py_XDECREF () does nothing.
530+ Py_XDECREF (obj);
531+ goto error;
532+ }
533+ // PyModule_AddObject() stole a reference to obj:
534+ // Py_XDECREF(obj) is not needed here.
526535
527- static int
528- add_spam(PyObject *module, int value)
529- {
530- PyObject *obj = PyLong_FromLong(value);
531- if (PyModule_AddObject(module, "spam", obj) < 0) {
532- Py_XDECREF(obj);
533- return -1;
534- }
535- // PyModule_AddObject() stole a reference to obj:
536- // Py_DECREF(obj) is not needed here
537- return 0;
538- }
536+ .. deprecated :: 3.13
539537
540- Note that ``Py_XDECREF() `` should be used instead of ``Py_DECREF() `` in
541- this case, since *obj * can be ``NULL ``.
538+ :c:func: `PyModule_AddObject ` is :term: `soft deprecated `.
542539
543540
544541.. c :function :: int PyModule_AddIntConstant (PyObject *module, const char *name, long value)
0 commit comments