]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45615: Add missing test for printing traceback for non-exception. Fix traceback... 30340/head
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sun, 2 Jan 2022 09:34:03 +0000 (09:34 +0000)
committerGitHub <noreply@github.com>
Sun, 2 Jan 2022 09:34:03 +0000 (09:34 +0000)
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst [new file with mode: 0644]
Modules/_testcapimodule.c

index 97bd9bae1d58e45c0e4cf76e75f87c35d1b570bf..a0e4656d3d9eaa86f15e32acb0e35575f4dd7593 100644 (file)
@@ -1060,6 +1060,22 @@ class TracebackFormatTests(unittest.TestCase):
         self.assertIn('ExceptionGroup', output)
         self.assertLessEqual(output.count('ExceptionGroup'), LIMIT)
 
+    @cpython_only
+    def test_print_exception_bad_type_capi(self):
+        from _testcapi import exception_print
+        with captured_output("stderr") as stderr:
+            exception_print(42)
+        self.assertEqual(
+            stderr.getvalue(),
+            ('TypeError: print_exception(): '
+             'Exception expected for value, int found\n')
+        )
+
+    def test_print_exception_bad_type_python(self):
+        msg = "Exception expected for value, int found"
+        with self.assertRaisesRegex(TypeError, msg):
+            traceback.print_exception(42)
+
 
 cause_message = (
     "\nThe above exception was the direct cause "
index b244750fd016eacb8c69b160ed0364a09f01b98b..05f1fffef0d3b0da2a7a44216b5345dc401fa784 100644 (file)
@@ -98,7 +98,11 @@ def _parse_value_tb(exc, value, tb):
         raise ValueError("Both or neither of value and tb must be given")
     if value is tb is _sentinel:
         if exc is not None:
-            return exc, exc.__traceback__
+            if isinstance(exc, BaseException):
+                return exc, exc.__traceback__
+
+            raise TypeError(f'Exception expected for value, '
+                            f'{type(exc).__name__} found')
         else:
             return None, None
     return value, tb
diff --git a/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst b/Misc/NEWS.d/next/Library/2021-12-13-15-51-16.bpo-45615.hVx83Q.rst
new file mode 100644 (file)
index 0000000..f8cd911
--- /dev/null
@@ -0,0 +1 @@
+Functions in the :mod:`traceback` module raise :exc:`TypeError` rather than :exc:`AttributeError` when an exception argument is not of type :exc:`BaseException`.
\ No newline at end of file
index 6116365b2c0f7191e9bbf2a12ff5c4e6069f5050..be40d68b40b17cd9a7f07a910968cf1a15a7abb5 100644 (file)
@@ -3513,17 +3513,17 @@ static PyObject *
 exception_print(PyObject *self, PyObject *args)
 {
     PyObject *value;
-    PyObject *tb;
+    PyObject *tb = NULL;
 
     if (!PyArg_ParseTuple(args, "O:exception_print",
-                            &value))
-        return NULL;
-    if (!PyExceptionInstance_Check(value)) {
-        PyErr_Format(PyExc_TypeError, "an exception instance is required");
+                            &value)) {
         return NULL;
     }
 
-    tb = PyException_GetTraceback(value);
+    if (PyExceptionInstance_Check(value)) {
+        tb = PyException_GetTraceback(value);
+    }
+
     PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
     Py_XDECREF(tb);