summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-03-25 14:36:43 -0700
committerStefan Krah <skrah@bytereef.org>2019-03-25 22:36:43 +0100
commitcdd8d4d6dd57f4c9429566706009d4613277d391 (patch)
treeef8ff48e6365df8353edb58c1bf3e7a902e33b25 /Python
parentf3c5535f63e7cd49166ab33b7b0ebb3285ef4ed0 (diff)
downloadcpython-git-cdd8d4d6dd57f4c9429566706009d4613277d391.tar.gz
bpo-36370: Check for PyErr_Occurred() after PyImport_GetModule() (GH-12504)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c6
-rw-r--r--Python/import.c19
-rw-r--r--Python/pylifecycle.c6
-rw-r--r--Python/sysmodule.c4
4 files changed, 24 insertions, 11 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index c394b9b482..634edbaec0 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4816,7 +4816,7 @@ import_from(PyObject *v, PyObject *name)
}
x = PyImport_GetModule(fullmodname);
Py_DECREF(fullmodname);
- if (x == NULL) {
+ if (x == NULL && !PyErr_Occurred()) {
goto error;
}
Py_DECREF(pkgname);
@@ -4839,7 +4839,7 @@ import_from(PyObject *v, PyObject *name)
"cannot import name %R from %R (unknown location)",
name, pkgname_or_unknown
);
- /* NULL check for errmsg done by PyErr_SetImportError. */
+ /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
PyErr_SetImportError(errmsg, pkgname, NULL);
}
else {
@@ -4847,7 +4847,7 @@ import_from(PyObject *v, PyObject *name)
"cannot import name %R from %R (%S)",
name, pkgname_or_unknown, pkgpath
);
- /* NULL check for errmsg done by PyErr_SetImportError. */
+ /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
PyErr_SetImportError(errmsg, pkgname, pkgpath);
}
diff --git a/Python/import.c b/Python/import.c
index ccdd599305..63c99ea5c1 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -953,11 +953,10 @@ exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object
Py_DECREF(v);
m = PyImport_GetModule(name);
- if (m == NULL) {
+ if (m == NULL && !PyErr_Occurred()) {
PyErr_Format(PyExc_ImportError,
"Loaded module %R not found in sys.modules",
name);
- return NULL;
}
return m;
@@ -1714,6 +1713,10 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
mod = PyImport_GetModule(abs_name);
+ if (mod == NULL && PyErr_Occurred()) {
+ goto error;
+ }
+
if (mod != NULL && mod != Py_None) {
_Py_IDENTIFIER(__spec__);
_Py_IDENTIFIER(_initializing);
@@ -1801,9 +1804,11 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
final_mod = PyImport_GetModule(to_return);
Py_DECREF(to_return);
if (final_mod == NULL) {
- PyErr_Format(PyExc_KeyError,
- "%R not in sys.modules as expected",
- to_return);
+ if (!PyErr_Occurred()) {
+ PyErr_Format(PyExc_KeyError,
+ "%R not in sys.modules as expected",
+ to_return);
+ }
goto error;
}
}
@@ -1855,6 +1860,10 @@ PyImport_ReloadModule(PyObject *m)
PyObject *reloaded_module = NULL;
PyObject *imp = _PyImport_GetModuleId(&PyId_imp);
if (imp == NULL) {
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+
imp = PyImport_ImportModule("imp");
if (imp == NULL) {
return NULL;
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 94b6d43c0e..d8e6f8fa89 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -2239,8 +2239,10 @@ wait_for_thread_shutdown(void)
PyObject *result;
PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
if (threading == NULL) {
- /* threading not imported */
- PyErr_Clear();
+ if (PyErr_Occurred()) {
+ PyErr_WriteUnraisable(NULL);
+ }
+ /* else: threading not imported */
return;
}
result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index c01a04e6c0..d87b4e2c01 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -274,7 +274,9 @@ sys_displayhook(PyObject *self, PyObject *o)
builtins = _PyImport_GetModuleId(&PyId_builtins);
if (builtins == NULL) {
- PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
+ }
return NULL;
}
Py_DECREF(builtins);