summaryrefslogtreecommitdiffstats
path: root/Doc/c-api/module.rst
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@gmail.com>2019-09-12 12:11:20 (GMT)
committerStéphane Wirtel <stephane@wirtel.be>2019-09-12 12:11:20 (GMT)
commit224b8aaa7e8f67f748e8b7b6a4a77a25f6554651 (patch)
tree209bec1d2dd42b404f13c42c3c8e98988908a863 /Doc/c-api/module.rst
parent967b84c913c7b09ae2fc86272cb9373415e2beaf (diff)
downloadcpython-224b8aaa7e8f67f748e8b7b6a4a77a25f6554651.zip
cpython-224b8aaa7e8f67f748e8b7b6a4a77a25f6554651.tar.gz
cpython-224b8aaa7e8f67f748e8b7b6a4a77a25f6554651.tar.bz2
bpo-26868: Fix example usage of PyModule_AddObject. (#15725)
* Add a note to the PyModule_AddObject docs. * Correct example usages of PyModule_AddObject. * Whitespace. * Clean up wording. * 📜🤖 Added by blurb_it. * First code review. * Add < 0 in the tests with PyModule_AddObject
Diffstat (limited to 'Doc/c-api/module.rst')
-rw-r--r--Doc/c-api/module.rst17
1 files changed, 16 insertions, 1 deletions
diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst
index 68cbda2..feca1ec 100644
--- a/Doc/c-api/module.rst
+++ b/Doc/c-api/module.rst
@@ -417,7 +417,22 @@ state:
Add an object to *module* as *name*. This is a convenience function which can
be used from the module's initialization function. This steals a reference to
- *value*. Return ``-1`` on error, ``0`` on success.
+ *value* on success. Return ``-1`` on error, ``0`` on success.
+
+ .. note::
+
+ Unlike other functions that steal references, ``PyModule_AddObject()`` only
+ decrements the reference count of *value* **on success**.
+
+ This means that its return value must be checked, and calling code must
+ :c:func:`Py_DECREF` *value* manually on error. Example usage::
+
+ Py_INCREF(spam);
+ if (PyModule_AddObject(module, "spam", spam) < 0) {
+ Py_DECREF(module);
+ Py_DECREF(spam);
+ return NULL;
+ }
.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)