Skip to content

Commit 474220e

Browse files
authored
gh-99947: Ensure unreported errors are chained for SystemError during import (GH-99946)
1 parent a68e585 commit 474220e

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

Lib/test/test_importlib/extension/test_loader.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,14 @@ def test_bad_modules(self):
351351
]:
352352
with self.subTest(name_base):
353353
name = self.name + '_' + name_base
354-
with self.assertRaises(SystemError):
354+
with self.assertRaises(SystemError) as cm:
355355
self.load_module_by_name(name)
356356

357+
# If there is an unreported exception, it should be chained
358+
# with the `SystemError`.
359+
if "unreported_exception" in name_base:
360+
self.assertIsNotNone(cm.exception.__cause__)
361+
357362
def test_nonascii(self):
358363
# Test that modules with non-ASCII names can be loaded.
359364
# punycode behaves slightly differently in some-ASCII and no-ASCII
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raising SystemError on import will now have its cause be set to the original unexpected exception.

Objects/moduleobject.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,10 @@ PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_versio
327327
goto error;
328328
} else {
329329
if (PyErr_Occurred()) {
330-
PyErr_Format(PyExc_SystemError,
331-
"creation of module %s raised unreported exception",
332-
name);
330+
_PyErr_FormatFromCause(
331+
PyExc_SystemError,
332+
"creation of module %s raised unreported exception",
333+
name);
333334
goto error;
334335
}
335336
}
@@ -431,7 +432,7 @@ PyModule_ExecDef(PyObject *module, PyModuleDef *def)
431432
return -1;
432433
}
433434
if (PyErr_Occurred()) {
434-
PyErr_Format(
435+
_PyErr_FormatFromCause(
435436
PyExc_SystemError,
436437
"execution of module %s raised unreported exception",
437438
name);

Python/importdl.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
180180
}
181181
goto error;
182182
} else if (PyErr_Occurred()) {
183-
PyErr_Clear();
184-
PyErr_Format(
183+
_PyErr_FormatFromCause(
185184
PyExc_SystemError,
186185
"initialization of %s raised unreported exception",
187186
name_buf);

0 commit comments

Comments
 (0)