From 145541cfa05394c38cfd64c0be2c5fb382860995 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 15 Jun 2017 20:54:38 +0300 Subject: [PATCH] bpo-30626: Fix error handling in PyImport_Import(). (#2103) In rare circumstances PyImport_Import() could return NULL without raising an error. --- Python/import.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Python/import.c b/Python/import.c index 9a78d6adc7a..d8a207b4b36 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1784,9 +1784,13 @@ PyImport_Import(PyObject *module_name) Py_DECREF(r); modules = PyImport_GetModuleDict(); - r = PyDict_GetItem(modules, module_name); - if (r != NULL) + r = PyDict_GetItemWithError(modules, module_name); + if (r != NULL) { Py_INCREF(r); + } + else if (!PyErr_Occurred()) { + PyErr_SetObject(PyExc_KeyError, module_name); + } err: Py_XDECREF(globals);