[Python-checkins] bpo-33809: add the TracebackException.print() method (GH-24231)

iritkatriel webhook-mailer at python.org
Sat May 22 12:39:52 EDT 2021


https://github.com/python/cpython/commit/220dd80a2671f57486055955d5422163cf73ed33
commit: 220dd80a2671f57486055955d5422163cf73ed33
branch: main
author: Irit Katriel <iritkatriel at yahoo.com>
committer: iritkatriel <iritkatriel at yahoo.com>
date: 2021-05-22T17:39:33+01:00
summary:

bpo-33809: add the TracebackException.print() method (GH-24231)

files:
A Misc/NEWS.d/next/Library/2021-01-16-18-36-00.bpo-33809.BiMK6V.rst
M Doc/library/traceback.rst
M Lib/test/test_traceback.py
M Lib/traceback.py

diff --git a/Doc/library/traceback.rst b/Doc/library/traceback.rst
index e938dd58b0531..bd53bc066becc 100644
--- a/Doc/library/traceback.rst
+++ b/Doc/library/traceback.rst
@@ -271,6 +271,13 @@ capture data for later printing in a lightweight fashion.
 
       Note that when locals are captured, they are also shown in the traceback.
 
+   .. method::  print(*, file=None, chain=True)
+
+      Print to *file* (default ``sys.stderr``) the exception information returned by
+      :meth:`format`.
+
+      .. versionadded:: 3.11
+
    .. method:: format(*, chain=True)
 
       Format the exception.
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 5bd969d62493a..e9df1ce9c79c0 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -1378,6 +1378,23 @@ def test_traceback_header(self):
         exc = traceback.TracebackException(Exception, Exception("haven"), None)
         self.assertEqual(list(exc.format()), ["Exception: haven\n"])
 
+    def test_print(self):
+        def f():
+            x = 12
+            try:
+                x/0
+            except Exception:
+                return sys.exc_info()
+        exc = traceback.TracebackException(*f(), capture_locals=True)
+        output = StringIO()
+        exc.print(file=output)
+        self.assertEqual(
+            output.getvalue().split('\n')[-4:],
+            ['    x/0',
+             '    x = 12',
+             'ZeroDivisionError: division by zero',
+             ''])
+
 
 class MiscTest(unittest.TestCase):
 
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 8f908dd2e0944..e19745df6def6 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -111,11 +111,8 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
     position of the error.
     """
     value, tb = _parse_value_tb(exc, value, tb)
-    if file is None:
-        file = sys.stderr
     te = TracebackException(type(value), value, tb, limit=limit, compact=True)
-    for line in te.format(chain=chain):
-        print(line, file=file, end="")
+    te.print(file=file, chain=chain)
 
 
 def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
@@ -669,3 +666,10 @@ def format(self, *, chain=True):
                 yield 'Traceback (most recent call last):\n'
                 yield from exc.stack.format()
             yield from exc.format_exception_only()
+
+    def print(self, *, file=None, chain=True):
+        """Print the result of self.format(chain=chain) to 'file'."""
+        if file is None:
+            file = sys.stderr
+        for line in self.format(chain=chain):
+            print(line, file=file, end="")
diff --git a/Misc/NEWS.d/next/Library/2021-01-16-18-36-00.bpo-33809.BiMK6V.rst b/Misc/NEWS.d/next/Library/2021-01-16-18-36-00.bpo-33809.BiMK6V.rst
new file mode 100644
index 0000000000000..a8a550dc0d418
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-01-16-18-36-00.bpo-33809.BiMK6V.rst
@@ -0,0 +1,2 @@
+Add the :meth:`traceback.TracebackException.print` method which prints
+the formatted exception information.



More information about the Python-checkins mailing list