Skip to content

Commit

Permalink
gh-99947: Ensure unreported errors are chained for SystemError during…
Browse files Browse the repository at this point in the history
… import (GH-99946)
  • Loading branch information
seberg committed Dec 23, 2022
1 parent a68e585 commit 474220e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Lib/test/test_importlib/extension/test_loader.py
Expand Up @@ -351,9 +351,14 @@ def test_bad_modules(self):
]:
with self.subTest(name_base):
name = self.name + '_' + name_base
with self.assertRaises(SystemError):
with self.assertRaises(SystemError) as cm:
self.load_module_by_name(name)

# If there is an unreported exception, it should be chained
# with the `SystemError`.
if "unreported_exception" in name_base:
self.assertIsNotNone(cm.exception.__cause__)

def test_nonascii(self):
# Test that modules with non-ASCII names can be loaded.
# punycode behaves slightly differently in some-ASCII and no-ASCII
Expand Down
@@ -0,0 +1 @@
Raising SystemError on import will now have its cause be set to the original unexpected exception.
9 changes: 5 additions & 4 deletions Objects/moduleobject.c
Expand Up @@ -327,9 +327,10 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
goto error;
} else {
if (PyErr_Occurred()) {
PyErr_Format(PyExc_SystemError,
"creation of module %s raised unreported exception",
name);
_PyErr_FormatFromCause(
PyExc_SystemError,
"creation of module %s raised unreported exception",
name);
goto error;
}
}
Expand Down Expand Up @@ -431,7 +432,7 @@ PyModule_ExecDef(PyObject *module, PyModuleDef *def)
return -1;
}
if (PyErr_Occurred()) {
PyErr_Format(
_PyErr_FormatFromCause(
PyExc_SystemError,
"execution of module %s raised unreported exception",
name);
Expand Down
3 changes: 1 addition & 2 deletions Python/importdl.c
Expand Up @@ -180,8 +180,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
}
goto error;
} else if (PyErr_Occurred()) {
PyErr_Clear();
PyErr_Format(
_PyErr_FormatFromCause(
PyExc_SystemError,
"initialization of %s raised unreported exception",
name_buf);
Expand Down

0 comments on commit 474220e

Please sign in to comment.