Skip to content

Commit 8d58070

Browse files
[3.12] gh-130250: fix regression in traceback.print_last (GH-130318) (#130326)
gh-130250: fix regression in traceback.print_last (GH-130318) (cherry picked from commit 6c982ae) Co-authored-by: Irit Katriel <[email protected]>
1 parent 720de0d commit 8d58070

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

Doc/library/traceback.rst

Lines changed: 4 additions & 4 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ def test_print_exception_exc(self):
328328
traceback.print_exception(Exception("projector"), file=output)
329329
self.assertEqual(output.getvalue(), "Exception: projector\n")
330330

331+
def test_print_last(self):
332+
self.assertIsNone(getattr(sys, "last_exc", None))
333+
sys.last_exc = ValueError(42)
334+
output = StringIO()
335+
traceback.print_last(file=output)
336+
self.assertEqual(output.getvalue(), "ValueError: 42\n")
337+
331338
def test_format_exception_exc(self):
332339
e = Exception("projector")
333340
output = traceback.format_exception(e)

Lib/traceback.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,23 @@ def _safe_string(value, what, func=str):
176176
# --
177177

178178
def print_exc(limit=None, file=None, chain=True):
179-
"""Shorthand for 'print_exception(sys.exception(), limit, file, chain)'."""
179+
"""Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain)'."""
180180
print_exception(sys.exception(), limit=limit, file=file, chain=chain)
181181

182182
def format_exc(limit=None, chain=True):
183183
"""Like print_exc() but return a string."""
184184
return "".join(format_exception(sys.exception(), limit=limit, chain=chain))
185185

186186
def print_last(limit=None, file=None, chain=True):
187-
"""This is a shorthand for 'print_exception(sys.last_exc, limit, file, chain)'."""
187+
"""This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain)'."""
188188
if not hasattr(sys, "last_exc") and not hasattr(sys, "last_type"):
189189
raise ValueError("no last exception")
190190

191191
if hasattr(sys, "last_exc"):
192-
print_exception(sys.last_exc, limit, file, chain)
192+
print_exception(sys.last_exc, limit=limit, file=file, chain=chain)
193193
else:
194194
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
195-
limit, file, chain)
195+
limit=limit, file=file, chain=chain)
196196

197197

198198
#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression in ``traceback.print_last()``.

0 commit comments

Comments
 (0)