Skip to content
Prev Previous commit
Next Next commit
Don't bother raising if PyModule_GetName() fails.
I copied some code from module_dir into module_{get|set}_annotations().
It seemed like it had a minor flaw: if the module dict failed a
PyDict_Check(), it wanted to raise an exception.  But to raise the
exception, it wanted the name of the module, and if getting the name
failed it skipped raising the exception.  It turns out, if getting
the name of the module fails, *that* raises an exception too.  So,
rather than stomp on that exception, we should do the same thing
module_dir() does: leave that exception alone and return an error.
  • Loading branch information
larryhastings committed Apr 27, 2021
commit 4a5d431b6b165e5d3392a78471930af516f08154
16 changes: 7 additions & 9 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,11 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))

if (dict == NULL) {
const char *name = PyModule_GetName((PyObject *)m);
if (!name) {
name = "<module>";
}
PyErr_Format(PyExc_TypeError,
if (name) {
PyErr_Format(PyExc_TypeError,
"%.200s.__dict__ is not a dictionary",
name);
}
return NULL;
}

Expand Down Expand Up @@ -890,12 +889,11 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor

if (dict == NULL) {
const char *name = PyModule_GetName((PyObject *)m);
if (!name) {
name = "<module>";
if (name) {
PyErr_Format(PyExc_TypeError,
"%.200s.__dict__ is not a dictionary",
name);
}
PyErr_Format(PyExc_TypeError,
"%.200s.__dict__ is not a dictionary",
name);
return -1;
}

Expand Down