Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-99947: Ensure unreported error is chained for SystemError during import #99946

Merged
merged 5 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
ENH: Ensure unreported error is chained for import SystemErrors
Debugging a small issue in SciPy was made a bit tricky by the error
being cleared seemingly unnecessary here.

This ensures that module load errors chain their exceptions when
this occurs (similar to function calls).

Expanded the test to check that the `__cause__` is set where it seems
that it should be.
  • Loading branch information
seberg committed Dec 2, 2022
commit fc7027c76314863bf781e415da5b7c2b62cc75d3
6 changes: 5 additions & 1 deletion Lib/test/test_importlib/extension/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,13 @@ 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
brettcannon marked this conversation as resolved.
Show resolved Hide resolved
if "unreported_exception" in name_base:
self.assertIsNot(cm.exception.__cause__, None)
brettcannon marked this conversation as resolved.
Show resolved Hide resolved

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
9 changes: 5 additions & 4 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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