Skip to content

Commit fab05de

Browse files
[3.6] bpo-30626: Fix error handling in PyImport_Import(). (GH-2103) (#2221)
In rare circumstances PyImport_Import() could return NULL without raising an error. (cherry picked from commit 145541c)
1 parent e45ea37 commit fab05de

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Python/import.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,9 +1785,13 @@ PyImport_Import(PyObject *module_name)
17851785
Py_DECREF(r);
17861786

17871787
modules = PyImport_GetModuleDict();
1788-
r = PyDict_GetItem(modules, module_name);
1789-
if (r != NULL)
1788+
r = PyDict_GetItemWithError(modules, module_name);
1789+
if (r != NULL) {
17901790
Py_INCREF(r);
1791+
}
1792+
else if (!PyErr_Occurred()) {
1793+
PyErr_SetObject(PyExc_KeyError, module_name);
1794+
}
17911795

17921796
err:
17931797
Py_XDECREF(globals);

0 commit comments

Comments
 (0)