Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 220dd80

Browse files
authored
bpo-33809: add the TracebackException.print() method (GH-24231)
1 parent 9e746e3 commit 220dd80

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

Doc/library/traceback.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ capture data for later printing in a lightweight fashion.
271271

272272
Note that when locals are captured, they are also shown in the traceback.
273273

274+
.. method:: print(*, file=None, chain=True)
275+
276+
Print to *file* (default ``sys.stderr``) the exception information returned by
277+
:meth:`format`.
278+
279+
.. versionadded:: 3.11
280+
274281
.. method:: format(*, chain=True)
275282

276283
Format the exception.

Lib/test/test_traceback.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,23 @@ def test_traceback_header(self):
13781378
exc = traceback.TracebackException(Exception, Exception("haven"), None)
13791379
self.assertEqual(list(exc.format()), ["Exception: haven\n"])
13801380

1381+
def test_print(self):
1382+
def f():
1383+
x = 12
1384+
try:
1385+
x/0
1386+
except Exception:
1387+
return sys.exc_info()
1388+
exc = traceback.TracebackException(*f(), capture_locals=True)
1389+
output = StringIO()
1390+
exc.print(file=output)
1391+
self.assertEqual(
1392+
output.getvalue().split('\n')[-4:],
1393+
[' x/0',
1394+
' x = 12',
1395+
'ZeroDivisionError: division by zero',
1396+
''])
1397+
13811398

13821399
class MiscTest(unittest.TestCase):
13831400

Lib/traceback.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
111111
position of the error.
112112
"""
113113
value, tb = _parse_value_tb(exc, value, tb)
114-
if file is None:
115-
file = sys.stderr
116114
te = TracebackException(type(value), value, tb, limit=limit, compact=True)
117-
for line in te.format(chain=chain):
118-
print(line, file=file, end="")
115+
te.print(file=file, chain=chain)
119116

120117

121118
def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
@@ -669,3 +666,10 @@ def format(self, *, chain=True):
669666
yield 'Traceback (most recent call last):\n'
670667
yield from exc.stack.format()
671668
yield from exc.format_exception_only()
669+
670+
def print(self, *, file=None, chain=True):
671+
"""Print the result of self.format(chain=chain) to 'file'."""
672+
if file is None:
673+
file = sys.stderr
674+
for line in self.format(chain=chain):
675+
print(line, file=file, end="")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add the :meth:`traceback.TracebackException.print` method which prints
2+
the formatted exception information.

0 commit comments

Comments
 (0)