[3.6] bpo-30626: Fix error handling in PyImport_Import(). (GH-2103) (… · python/cpython@fab05de (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit fab05de

In rare circumstances PyImport_Import() could return NULL without raising an error. (cherry picked from commit 145541c)

File tree

1 file changed

lines changed

1 file changed

lines changed

Lines changed: 6 additions & 2 deletions

Original file line number Diff line number Diff line change
@@ -1785,9 +1785,13 @@ PyImport_Import(PyObject *module_name)
1785 1785 Py_DECREF(r);
1786 1786
1787 1787 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) {
1790 1790 Py_INCREF(r);
1791 + }
1792 +else if (!PyErr_Occurred()) {
1793 +PyErr_SetObject(PyExc_KeyError, module_name);
1794 + }
1791 1795
1792 1796 err:
1793 1797 Py_XDECREF(globals);