Skip to content

Commit 027b09c

Browse files
author
Stefan Krah
authored
bpo-36370: Check for PyErr_Occurred() after PyImport_GetModule() (GH-12504)
1 parent d1e768a commit 027b09c

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4948,7 +4948,7 @@ import_from(PyObject *v, PyObject *name)
49484948
}
49494949
x = PyImport_GetModule(fullmodname);
49504950
Py_DECREF(fullmodname);
4951-
if (x == NULL) {
4951+
if (x == NULL && !PyErr_Occurred()) {
49524952
goto error;
49534953
}
49544954
Py_DECREF(pkgname);
@@ -4971,15 +4971,15 @@ import_from(PyObject *v, PyObject *name)
49714971
"cannot import name %R from %R (unknown location)",
49724972
name, pkgname_or_unknown
49734973
);
4974-
/* NULL check for errmsg done by PyErr_SetImportError. */
4974+
/* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
49754975
PyErr_SetImportError(errmsg, pkgname, NULL);
49764976
}
49774977
else {
49784978
errmsg = PyUnicode_FromFormat(
49794979
"cannot import name %R from %R (%S)",
49804980
name, pkgname_or_unknown, pkgpath
49814981
);
4982-
/* NULL check for errmsg done by PyErr_SetImportError. */
4982+
/* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
49834983
PyErr_SetImportError(errmsg, pkgname, pkgpath);
49844984
}
49854985

Python/import.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,10 @@ exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object
966966
Py_DECREF(v);
967967

968968
m = PyImport_GetModule(name);
969-
if (m == NULL) {
969+
if (m == NULL && !PyErr_Occurred()) {
970970
PyErr_Format(PyExc_ImportError,
971971
"Loaded module %R not found in sys.modules",
972972
name);
973-
return NULL;
974973
}
975974

976975
return m;
@@ -1735,6 +1734,10 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
17351734
}
17361735

17371736
mod = PyImport_GetModule(abs_name);
1737+
if (mod == NULL && PyErr_Occurred()) {
1738+
goto error;
1739+
}
1740+
17381741
if (mod != NULL && mod != Py_None) {
17391742
_Py_IDENTIFIER(__spec__);
17401743
_Py_IDENTIFIER(_lock_unlock_module);
@@ -1810,9 +1813,11 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
18101813
final_mod = PyImport_GetModule(to_return);
18111814
Py_DECREF(to_return);
18121815
if (final_mod == NULL) {
1813-
PyErr_Format(PyExc_KeyError,
1814-
"%R not in sys.modules as expected",
1815-
to_return);
1816+
if (!PyErr_Occurred()) {
1817+
PyErr_Format(PyExc_KeyError,
1818+
"%R not in sys.modules as expected",
1819+
to_return);
1820+
}
18161821
goto error;
18171822
}
18181823
}
@@ -1875,6 +1880,10 @@ PyImport_ReloadModule(PyObject *m)
18751880
PyObject *reloaded_module = NULL;
18761881
PyObject *imp = _PyImport_GetModuleId(&PyId_imp);
18771882
if (imp == NULL) {
1883+
if (PyErr_Occurred()) {
1884+
return NULL;
1885+
}
1886+
18781887
imp = PyImport_ImportModule("imp");
18791888
if (imp == NULL) {
18801889
return NULL;

Python/pylifecycle.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,8 +2157,10 @@ wait_for_thread_shutdown(void)
21572157
PyObject *result;
21582158
PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
21592159
if (threading == NULL) {
2160-
/* threading not imported */
2161-
PyErr_Clear();
2160+
if (PyErr_Occurred()) {
2161+
PyErr_WriteUnraisable(NULL);
2162+
}
2163+
/* else: threading not imported */
21622164
return;
21632165
}
21642166
result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);

Python/sysmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ sys_displayhook(PyObject *module, PyObject *o)
283283

284284
builtins = _PyImport_GetModuleId(&PyId_builtins);
285285
if (builtins == NULL) {
286-
PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
286+
if (!PyErr_Occurred()) {
287+
PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
288+
}
287289
return NULL;
288290
}
289291
Py_DECREF(builtins);

0 commit comments

Comments
 (0)