Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
First code review.
  • Loading branch information
brandtbucher committed Sep 9, 2019
commit bb345d994148c7626ad19dd045045ce1469882b0
6 changes: 3 additions & 3 deletions Doc/c-api/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,18 @@ 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
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)) {
if (PyModule_AddObject(module, "spam", spam) < 0) {
Py_DECREF(module);
Py_DECREF(spam);
return NULL;
Expand Down
14 changes: 5 additions & 9 deletions Doc/extending/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -221,9 +221,10 @@ with an exception object (leaving out the error checking for now)::
return NULL;

SpamError = PyErr_NewException("spam.error", NULL, NULL);
Py_INCREF(SpamError);
Py_XINCREF(SpamError);
if (PyModule_AddObject(m, "error", SpamError)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to add also < 0 in all other checks for PyModule_AddObject() result.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an agreed-upon style? I had always thought it was a matter of taste.

In either case, I guess all of these files should at least be consistent...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the extra test with < 0

Py_DECREF(SpamError);
Py_XDECREF(SpamError);
Py_CLEAR(SpamError);
Py_DECREF(m);
return NULL;
}
Expand Down Expand Up @@ -1266,13 +1267,8 @@ 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) {
Py_DECREF(m);
return NULL;
}

if (PyModule_AddObject(m, "_C_API", c_api_object)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, change the example with if (PyModule_AddObject(m, "_C_API", c_api_object) < 0)

Py_DECREF(c_api_object);
Py_XDECREF(c_api_object);
Py_DECREF(m);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/sublist.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PyInit_sublist(void)
return NULL;

Py_INCREF(&SubListType);
if ((m, "SubList", (PyObject *) &SubListType)) {
if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Py_DECREF(&SubListType);
Py_DECREF(m);
return NULL;
Expand Down