bpo-26389: Allow passing an exception object in the traceback module (GH-22610)
The format_exception(), format_exception_only(), and
print_exception() functions can now take an exception object as a positional-only argument.
Co-Authored-By: Matthias Bussonnier <bussonniermatthias@gmail.com>
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 730596e..91688ff 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -212,6 +212,26 @@ def test_print_exception(self):
)
self.assertEqual(output.getvalue(), "Exception: projector\n")
+ def test_print_exception_exc(self):
+ output = StringIO()
+ traceback.print_exception(Exception("projector"), file=output)
+ self.assertEqual(output.getvalue(), "Exception: projector\n")
+
+ def test_format_exception_exc(self):
+ e = Exception("projector")
+ output = traceback.format_exception(e)
+ self.assertEqual(output, ["Exception: projector\n"])
+ with self.assertRaisesRegex(ValueError, 'Both or neither'):
+ traceback.format_exception(e.__class__, e)
+ with self.assertRaisesRegex(ValueError, 'Both or neither'):
+ traceback.format_exception(e.__class__, tb=e.__traceback__)
+ with self.assertRaisesRegex(TypeError, 'positional-only'):
+ traceback.format_exception(exc=e)
+
+ def test_format_exception_only_exc(self):
+ output = traceback.format_exception_only(Exception("projector"))
+ self.assertEqual(output, ["Exception: projector\n"])
+
class TracebackFormatTests(unittest.TestCase):