summaryrefslogtreecommitdiffstats
path: root/Doc/extending
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/extending
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/extending')
-rw-r--r--Doc/extending/extending.rst20
-rw-r--r--Doc/extending/newtypes_tutorial.rst14
2 files changed, 27 insertions, 7 deletions
diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst
index e459514..5b4ea82 100644
--- a/Doc/extending/extending.rst
+++ b/Doc/extending/extending.rst
@@ -209,7 +209,7 @@ usually declare a static object variable at the beginning of your file::
static PyObject *SpamError;
and initialize it in your module's initialization function (:c:func:`PyInit_spam`)
-with an exception object (leaving out the error checking for now)::
+with an exception object::
PyMODINIT_FUNC
PyInit_spam(void)
@@ -221,8 +221,14 @@ with an exception object (leaving out the error checking for now)::
return NULL;
SpamError = PyErr_NewException("spam.error", NULL, NULL);
- Py_INCREF(SpamError);
- PyModule_AddObject(m, "error", SpamError);
+ Py_XINCREF(SpamError);
+ if (PyModule_AddObject(m, "error", SpamError) < 0) {
+ Py_XDECREF(SpamError);
+ Py_CLEAR(SpamError);
+ Py_DECREF(m);
+ return NULL;
+ }
+
return m;
}
@@ -1261,8 +1267,12 @@ function must take care of initializing the C API pointer array::
/* Create a Capsule containing the API pointer array's address */
c_api_object = PyCapsule_New((void *)PySpam_API, "spam._C_API", NULL);
- if (c_api_object != NULL)
- PyModule_AddObject(m, "_C_API", c_api_object);
+ if (PyModule_AddObject(m, "_C_API", c_api_object) < 0) {
+ Py_XDECREF(c_api_object);
+ Py_DECREF(m);
+ return NULL;
+ }
+
return m;
}
diff --git a/Doc/extending/newtypes_tutorial.rst b/Doc/extending/newtypes_tutorial.rst
index 59c8cc0..94ca747 100644
--- a/Doc/extending/newtypes_tutorial.rst
+++ b/Doc/extending/newtypes_tutorial.rst
@@ -179,7 +179,12 @@ This initializes the :class:`Custom` type, filling in a number of members
to the appropriate default values, including :attr:`ob_type` that we initially
set to *NULL*. ::
- PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
+ Py_INCREF(&CustomType);
+ if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
+ Py_DECREF(&CustomType);
+ PY_DECREF(m);
+ return NULL;
+ }
This adds the type to the module dictionary. This allows us to create
:class:`Custom` instances by calling the :class:`Custom` class:
@@ -864,7 +869,12 @@ function::
return NULL;
Py_INCREF(&SubListType);
- PyModule_AddObject(m, "SubList", (PyObject *) &SubListType);
+ if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType) < 0) {
+ Py_DECREF(&SubListType);
+ Py_DECREF(m);
+ return NULL;
+ }
+
return m;
}