mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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
This commit is contained in:
parent
967b84c913
commit
224b8aaa7e
9 changed files with 74 additions and 13 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue