Skip to content

Commit d172415

Browse files
[3.13] gh-130250: fix regression in traceback.print_last (GH-130318) (#130325)
gh-130250: fix regression in traceback.print_last (GH-130318) (cherry picked from commit 6c982ae) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
1 parent 6c92424 commit d172415

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
@@ -111,14 +111,14 @@ Module-Level Functions
111111

112112
.. function:: print_exc(limit=None, file=None, chain=True)
113113

114-
This is a shorthand for ``print_exception(sys.exception(), limit, file,
115-
chain)``.
114+
This is a shorthand for ``print_exception(sys.exception(), limit=limit, file=file,
115+
chain=chain)``.
116116

117117

118118
.. function:: print_last(limit=None, file=None, chain=True)
119119

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

124124

Lib/test/test_traceback.py

+7
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,13 @@ def test_print_exception_exc(self):
516516
traceback.print_exception(Exception("projector"), file=output)
517517
self.assertEqual(output.getvalue(), "Exception: projector\n")
518518

519+
def test_print_last(self):
520+
self.assertIsNone(getattr(sys, "last_exc", None))
521+
sys.last_exc = ValueError(42)
522+
output = StringIO()
523+
traceback.print_last(file=output)
524+
self.assertEqual(output.getvalue(), "ValueError: 42\n")
525+
519526
def test_format_exception_exc(self):
520527
e = Exception("projector")
521528
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)