bpo-31492: Fix assertion failures in case of a module with a bad __na… · pythoncapi/cpython@6db7033 · GitHub
Skip to content

Commit 6db7033

Browse files
orenmnserhiy-storchaka
authored andcommitted
bpo-31492: Fix assertion failures in case of a module with a bad __name__ attribute. (python#3620)
1 parent 453408a commit 6db7033

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

Lib/test/test_import/__init__.py

Lines changed: 12 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix assertion failures in case of failing to import from a module with a bad
2+
``__name__`` attribute, and in case of failing to access an attribute of such
3+
a module. Patch by Oren Milman.

Objects/moduleobject.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,11 @@ module_getattro(PyModuleObject *m, PyObject *name)
687687
if (m->md_dict) {
688688
_Py_IDENTIFIER(__name__);
689689
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
690-
if (mod_name) {
690+
if (mod_name && PyUnicode_Check(mod_name)) {
691691
PyErr_Format(PyExc_AttributeError,
692692
"module '%U' has no attribute '%U'", mod_name, name);
693693
return NULL;
694694
}
695-
else if (PyErr_Occurred()) {
696-
PyErr_Clear();
697-
}
698695
}
699696
PyErr_Format(PyExc_AttributeError,
700697
"module has no attribute '%U'", name);

Python/ceval.c

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)