Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
15 changes: 15 additions & 0 deletions Doc/c-api/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,21 @@ state:
be used from the module's initialization function. This steals a reference to
*value*. Return ``-1`` on error, ``0`` on success.
Comment thread
brandtbucher marked this conversation as resolved.
Outdated

.. note::

Unlike other functions that steal references, ``PyModule_AddObject`` only
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
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)) {
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
Py_DECREF(module);
Py_DECREF(spam);
return NULL;
}

.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)

Add an integer constant to *module* as *name*. This convenience function can be
Expand Down
20 changes: 17 additions & 3 deletions Doc/extending/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ with an exception object (leaving out the error checking for now)::

SpamError = PyErr_NewException("spam.error", NULL, NULL);
Py_INCREF(SpamError);
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
PyModule_AddObject(m, "error", 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);
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
Py_DECREF(m);
return NULL;
}

return m;
}

Expand Down Expand Up @@ -1261,8 +1266,17 @@ 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 (c_api_object == NULL) {
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
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_DECREF(m);
return NULL;
}

return m;
}

Expand Down
14 changes: 12 additions & 2 deletions Doc/extending/newtypes_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

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(&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:
Expand Down Expand Up @@ -864,7 +869,12 @@ function::
return NULL;

Py_INCREF(&SubListType);
PyModule_AddObject(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;
}

return m;
}

Expand Down
7 changes: 6 additions & 1 deletion Doc/includes/custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ PyInit_custom(void)
return NULL;

Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) {

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(&CustomType);
PY_DECREF(m);
return NULL;
}

return m;
}
7 changes: 6 additions & 1 deletion Doc/includes/custom2.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ PyInit_custom2(void)
return NULL;

Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) {

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(&CustomType);
Py_DECREF(m);
return NULL;
}

return m;
}
7 changes: 6 additions & 1 deletion Doc/includes/custom3.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ PyInit_custom3(void)
return NULL;

Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) {

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(&CustomType);
Py_DECREF(m);
return NULL;
}

return m;
}
7 changes: 6 additions & 1 deletion Doc/includes/custom4.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ PyInit_custom4(void)
return NULL;

Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType)) {

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(&CustomType);
Py_DECREF(m);
return NULL;
}

return m;
}
7 changes: 6 additions & 1 deletion Doc/includes/sublist.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ PyInit_sublist(void)
return NULL;

Py_INCREF(&SubListType);
PyModule_AddObject(m, "SubList", (PyObject *) &SubListType);
if ((m, "SubList", (PyObject *) &SubListType)) {
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
Py_DECREF(&SubListType);
Py_DECREF(m);
return NULL;
}

return m;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix example usage of :c:func:`PyModule_AddObject` to properly handle errors.