diff options
author | Sebastian Berg <sebastianb@nvidia.com> | 2022-12-23 23:43:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-23 23:43:19 (GMT) |
commit | 474220e3a58d739acc5154eb3e000461d2222d62 (patch) | |
tree | 2f26951f08993d62a28107b03b1191d6467fd23b | |
parent | a68e585c8b7b27323f67905868467ce0588a1dae (diff) | |
download | cpython-474220e3a58d739acc5154eb3e000461d2222d62.zip cpython-474220e3a58d739acc5154eb3e000461d2222d62.tar.gz cpython-474220e3a58d739acc5154eb3e000461d2222d62.tar.bz2 |
gh-99947: Ensure unreported errors are chained for SystemError during import (GH-99946)
-rw-r--r-- | Lib/test/test_importlib/extension/test_loader.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/C API/2022-12-02-09-31-19.gh-issue-99947.Ski7OC.rst | 1 | ||||
-rw-r--r-- | Objects/moduleobject.c | 9 | ||||
-rw-r--r-- | Python/importdl.c | 3 |
4 files changed, 13 insertions, 7 deletions
diff --git a/Lib/test/test_importlib/extension/test_loader.py b/Lib/test/test_importlib/extension/test_loader.py index d69192b..3bf2bbd 100644 --- a/Lib/test/test_importlib/extension/test_loader.py +++ b/Lib/test/test_importlib/extension/test_loader.py @@ -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 diff --git a/Misc/NEWS.d/next/C API/2022-12-02-09-31-19.gh-issue-99947.Ski7OC.rst b/Misc/NEWS.d/next/C API/2022-12-02-09-31-19.gh-issue-99947.Ski7OC.rst new file mode 100644 index 0000000..fbed192 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-12-02-09-31-19.gh-issue-99947.Ski7OC.rst @@ -0,0 +1 @@ +Raising SystemError on import will now have its cause be set to the original unexpected exception. diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 8e03f24..24190e3 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -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); diff --git a/Python/importdl.c b/Python/importdl.c index 4022767..91fa06f 100644 --- a/Python/importdl.c +++ b/Python/importdl.c @@ -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); |