Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -4839,15 +4839,15 @@ 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 {
errmsg = PyUnicode_FromFormat(
"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);
}

Expand Down
19 changes: 14 additions & 5 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down