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
Document PyModule_AddObject's behavior on error #71055
Comments
This is probably harmless, but Modules/_csv.c has the following code: Py_INCREF(&Dialect_Type);
if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type))
return NULL; However, PyModule_AddObject returns only -1 and 0. It also doesn't decref Dialect_Type if it returns -1 so I guess more correct code should be: Py_INCREF(&Dialect_Type);
if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type) == -1) {
Py_DECREF(&Dialect_Type);
return NULL;
} The same pattern can be found in a few more modules. |
Testing returned value of PyModule_AddObject() is correct. This is a matter of style what to use: But the problem with a leak is more general. I have opened a discussion on Python-Dev: http://comments.gmane.org/gmane.comp.python.devel/157545 . |
Thanks for the write-up, Serhiy. It looks like "... == -1" is more popular in the codebase (for PyModule_AddObject, "... < 0" is the most popular style). Here is a patch to document the current behavior of PyModule_AddObject. |
Added comments on Rietveld. |
As for Modules/_csv.c, I think it is better to change the behavior of PyModule_AddObject() (bpo-26871). This will fix similar issues in all modules. |
Thanks for the review Serhiy. Here is an updated patch. |
Serhiy, do you have further comments about issue26868_v2.diff? |
issue26868_v2.diff LGTM. |
It looks like the idiom of calling PyModule_AddObject without Py_DECREF'ing on the error condition (or even checking for it at all) has spread quite a bit since this first reported. I'm preparing a PR to fix the other call sites. |
This has been documented, looks like #83004 is to track fixing the various call sites |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: