classification
Title: PyModule_GetDict() claims it can never fail, but it can
Type: behavior Stage:
Components: Documentation Versions: Python 3.3, Python 3.2, Python 3.1, Python 2.7, Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: amaury.forgeotdarc, docs@python, haypo, scoder
Priority: normal Keywords:

Created on 2011-09-09 16:19 by scoder, last changed 2011-09-14 04:06 by scoder.

Messages (4)
msg143764 - (view) Author: Stefan Behnel (scoder) * Date: 2011-09-09 16:19
As is obvious from the code, PyModule_GetDict() can fail if being passed a non-module object, and when the (unlikely) dict creation at the end fails. The documentation of the C-API function should be fixed to reflect that, i.e. it should state that NULL is returned in the case of an error.

PyObject *
PyModule_GetDict(PyObject *m)
{
    PyObject *d;
    if (!PyModule_Check(m)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    d = ((PyModuleObject *)m) -> md_dict;
    if (d == NULL)
        ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
    return d;
}
msg143940 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-09-12 21:40
The path with PyDict_New() is never taken, because PyModule_New already fills md_dict.
msg143994 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2011-09-13 23:13
So, can we close this issue?
msg144007 - (view) Author: Stefan Behnel (scoder) * Date: 2011-09-14 04:06
I gave two reasons why this function can fail, and one turns out to be assumed-to-be-dead code. So, no, there are two issues now, one with the documentation, one with the code.
History
Date User Action Args
2011-09-14 04:06:14scodersetmessages: + msg144007
2011-09-13 23:13:03hayposetnosy: + haypo
messages: + msg143994
2011-09-12 21:40:03amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg143940
2011-09-09 16:19:51scodercreate