Skip to content

Commit 263dcc3

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

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
@@ -1806,9 +1806,13 @@ PyImport_Import(PyObject *module_name)
18061806
Py_DECREF(r);
18071807

18081808
modules = PyImport_GetModuleDict();
1809-
r = PyDict_GetItem(modules, module_name);
1810-
if (r != NULL)
1809+
r = PyDict_GetItemWithError(modules, module_name);
1810+
if (r != NULL) {
18111811
Py_INCREF(r);
1812+
}
1813+
else if (!PyErr_Occurred()) {
1814+
PyErr_SetObject(PyExc_KeyError, module_name);
1815+
}
18121816

18131817
err:
18141818
Py_XDECREF(globals);

0 commit comments

Comments
 (0)