Skip to content

Commit 6c982ae

Browse files
authored
gh-130250: fix regression in traceback.print_last (#130318)
1 parent 7380186 commit 6c982ae

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

Doc/library/traceback.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ Module-Level Functions
115115

116116
.. function:: print_exc(limit=None, file=None, chain=True)
117117

118-
This is a shorthand for ``print_exception(sys.exception(), limit, file,
119-
chain)``.
118+
This is a shorthand for ``print_exception(sys.exception(), limit=limit, file=file,
119+
chain=chain)``.
120120

121121

122122
.. function:: print_last(limit=None, file=None, chain=True)
123123

124-
This is a shorthand for ``print_exception(sys.last_exc, limit, file,
125-
chain)``. In general it will work only after an exception has reached
124+
This is a shorthand for ``print_exception(sys.last_exc, limit=limit, file=file,
125+
chain=chain)``. In general it will work only after an exception has reached
126126
an interactive prompt (see :data:`sys.last_exc`).
127127

128128

Lib/test/test_traceback.py

+7
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ def test_print_exception_exc(self):
510510
traceback.print_exception(Exception("projector"), file=output)
511511
self.assertEqual(output.getvalue(), "Exception: projector\n")
512512

513+
def test_print_last(self):
514+
self.assertIsNone(getattr(sys, "last_exc", None))
515+
sys.last_exc = ValueError(42)
516+
output = StringIO()
517+
traceback.print_last(file=output)
518+
self.assertEqual(output.getvalue(), "ValueError: 42\n")
519+
513520
def test_format_exception_exc(self):
514521
e = Exception("projector")
515522
output = traceback.format_exception(e)

Lib/traceback.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,23 @@ def _safe_string(value, what, func=str):
204204
# --
205205

206206
def print_exc(limit=None, file=None, chain=True):
207-
"""Shorthand for 'print_exception(sys.exception(), limit, file, chain)'."""
207+
"""Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain)'."""
208208
print_exception(sys.exception(), limit=limit, file=file, chain=chain)
209209

210210
def format_exc(limit=None, chain=True):
211211
"""Like print_exc() but return a string."""
212212
return "".join(format_exception(sys.exception(), limit=limit, chain=chain))
213213

214214
def print_last(limit=None, file=None, chain=True):
215-
"""This is a shorthand for 'print_exception(sys.last_exc, limit, file, chain)'."""
215+
"""This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain)'."""
216216
if not hasattr(sys, "last_exc") and not hasattr(sys, "last_type"):
217217
raise ValueError("no last exception")
218218

219219
if hasattr(sys, "last_exc"):
220-
print_exception(sys.last_exc, limit, file, chain)
220+
print_exception(sys.last_exc, limit=limit, file=file, chain=chain)
221221
else:
222222
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
223-
limit, file, chain)
223+
limit=limit, file=file, chain=chain)
224224

225225

226226
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression in ``traceback.print_last()``.

0 commit comments

Comments
 (0)