Skip to content

gh-101944: mistake in documentation for PyModule_AddObjectRef() #101957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

OTheDev
Copy link
Contributor

@OTheDev OTheDev commented Feb 16, 2023

Closes #101944.

The issue author opened #101945, but closed it shortly after (not sure why).

I double-checked the source code of the function and can confirm that there is a mistake in the docs:

cpython/Python/modsupport.c

Lines 630 to 660 in 924a3bf

int
PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
{
if (!PyModule_Check(mod)) {
PyErr_SetString(PyExc_TypeError,
"PyModule_AddObjectRef() first argument "
"must be a module");
return -1;
}
if (!value) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_SystemError,
"PyModule_AddObjectRef() must be called "
"with an exception raised if value is NULL");
}
return -1;
}
PyObject *dict = PyModule_GetDict(mod);
if (dict == NULL) {
/* Internal error -- modules must have a dict! */
PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
PyModule_GetName(mod));
return -1;
}
if (PyDict_SetItemString(dict, name, value)) {
return -1;
}
return 0;
}

@AlexWaygood AlexWaygood self-requested a review February 16, 2023 19:59
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @OTheDev!

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

I don't think that this change makes the documentation easier to understand. For me, it sounds more misleading than before :-(

@@ -449,10 +449,11 @@ state:
Add an object to *module* as *name*. This is a convenience function which
can be used from the module's initialization function.

On success, return ``0``. On error, raise an exception and return ``-1``.
Copy link
Member

Choose a reason for hiding this comment

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

Please keep this general statement. There are many cases where the function can fail. For example, if mod is not a module or has no dictionary. PyDict_SetItemString() can fail with MemoryError, etc.

raised in this case.
If *value* is ``NULL`` (indicating failure), raise an exception and return
``-1``. If an exception is set, raise that exception. If no exception is
set, raise :exc:`SystemError`.
Copy link
Member

Choose a reason for hiding this comment

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

This documentation sounds misleading to me. The normal case when value is NULL is that an exception is already set, so the function does not "raise an exception". Also, I dislike documenting the exact exception type for implementation details: please don't document "SystemError".

Copy link
Contributor

Choose a reason for hiding this comment

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

please don't document "SystemError".

That was my fault; I encouraged it in #101957 (comment). Sorry 'bout that!

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@OTheDev OTheDev closed this Jan 28, 2025
@OTheDev OTheDev deleted the fix-issue-101944 branch January 28, 2025 03:57
@vstinner
Copy link
Member

I created a different doc change to clarify the NULL case: PR gh-129433.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Docs: PyModule_AddObjectRef should return -1 if value is NULL.
5 participants