gh-99947: Ensure unreported errors are chained for SystemError during import (GH-99946)

This commit is contained in:
Sebastian Berg 2022-12-24 00:43:19 +01:00 committed by GitHub
parent a68e585c8b
commit 474220e3a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 7 deletions

View File

@ -351,9 +351,14 @@ class MultiPhaseExtensionModuleTests(abc.LoaderTests):
]:
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

View File

@ -0,0 +1 @@
Raising SystemError on import will now have its cause be set to the original unexpected exception.

View File

@ -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;
}
}
@ -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);

View File

@ -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);