]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99947: Ensure unreported errors are chained for SystemError during import (GH...
authorSebastian Berg <sebastianb@nvidia.com>
Fri, 23 Dec 2022 23:43:19 +0000 (00:43 +0100)
committerGitHub <noreply@github.com>
Fri, 23 Dec 2022 23:43:19 +0000 (15:43 -0800)
Lib/test/test_importlib/extension/test_loader.py
Misc/NEWS.d/next/C API/2022-12-02-09-31-19.gh-issue-99947.Ski7OC.rst [new file with mode: 0644]
Objects/moduleobject.c
Python/importdl.c

index d69192b56bacb6cfe1c608f9612241865a4c21a9..3bf2bbdcdcc4e6be640c0a89d438392f09a54bd5 100644 (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
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 (file)
index 0000000..fbed192
--- /dev/null
@@ -0,0 +1 @@
+Raising SystemError on import will now have its cause be set to the original unexpected exception.
index 8e03f2446f6fcd088985ba61b2b76933b84fd2b0..24190e320ee6d666b703b17c5e2b37cd6d38ee1d 100644 (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);
index 40227674ca47ee83e7ad3b57895e8235c8019c8e..91fa06f49c289776b8a3114a05cf76a1bf2ddc5e 100644 (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);