[3.5] bpo-30626: Fix error handling in PyImport_Import(). (GH-2103) (… · python/cpython@263dcc3

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);