mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
[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 6c982aeb54
)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
parent
6c92424b94
commit
d172415a4e
4 changed files with 16 additions and 8 deletions
|
@ -111,14 +111,14 @@ Module-Level Functions
|
||||||
|
|
||||||
.. function:: print_exc(limit=None, file=None, chain=True)
|
.. function:: print_exc(limit=None, file=None, chain=True)
|
||||||
|
|
||||||
This is a shorthand for ``print_exception(sys.exception(), limit, file,
|
This is a shorthand for ``print_exception(sys.exception(), limit=limit, file=file,
|
||||||
chain)``.
|
chain=chain)``.
|
||||||
|
|
||||||
|
|
||||||
.. function:: print_last(limit=None, file=None, chain=True)
|
.. function:: print_last(limit=None, file=None, chain=True)
|
||||||
|
|
||||||
This is a shorthand for ``print_exception(sys.last_exc, limit, file,
|
This is a shorthand for ``print_exception(sys.last_exc, limit=limit, file=file,
|
||||||
chain)``. In general it will work only after an exception has reached
|
chain=chain)``. In general it will work only after an exception has reached
|
||||||
an interactive prompt (see :data:`sys.last_exc`).
|
an interactive prompt (see :data:`sys.last_exc`).
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -516,6 +516,13 @@ class TracebackCases(unittest.TestCase):
|
||||||
traceback.print_exception(Exception("projector"), file=output)
|
traceback.print_exception(Exception("projector"), file=output)
|
||||||
self.assertEqual(output.getvalue(), "Exception: projector\n")
|
self.assertEqual(output.getvalue(), "Exception: projector\n")
|
||||||
|
|
||||||
|
def test_print_last(self):
|
||||||
|
self.assertIsNone(getattr(sys, "last_exc", None))
|
||||||
|
sys.last_exc = ValueError(42)
|
||||||
|
output = StringIO()
|
||||||
|
traceback.print_last(file=output)
|
||||||
|
self.assertEqual(output.getvalue(), "ValueError: 42\n")
|
||||||
|
|
||||||
def test_format_exception_exc(self):
|
def test_format_exception_exc(self):
|
||||||
e = Exception("projector")
|
e = Exception("projector")
|
||||||
output = traceback.format_exception(e)
|
output = traceback.format_exception(e)
|
||||||
|
|
|
@ -204,7 +204,7 @@ def _safe_string(value, what, func=str):
|
||||||
# --
|
# --
|
||||||
|
|
||||||
def print_exc(limit=None, file=None, chain=True):
|
def print_exc(limit=None, file=None, chain=True):
|
||||||
"""Shorthand for 'print_exception(sys.exception(), limit, file, chain)'."""
|
"""Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain)'."""
|
||||||
print_exception(sys.exception(), limit=limit, file=file, chain=chain)
|
print_exception(sys.exception(), limit=limit, file=file, chain=chain)
|
||||||
|
|
||||||
def format_exc(limit=None, chain=True):
|
def format_exc(limit=None, chain=True):
|
||||||
|
@ -212,15 +212,15 @@ def format_exc(limit=None, chain=True):
|
||||||
return "".join(format_exception(sys.exception(), limit=limit, chain=chain))
|
return "".join(format_exception(sys.exception(), limit=limit, chain=chain))
|
||||||
|
|
||||||
def print_last(limit=None, file=None, chain=True):
|
def print_last(limit=None, file=None, chain=True):
|
||||||
"""This is a shorthand for 'print_exception(sys.last_exc, limit, file, chain)'."""
|
"""This is a shorthand for 'print_exception(sys.last_exc, limit=limit, file=file, chain=chain)'."""
|
||||||
if not hasattr(sys, "last_exc") and not hasattr(sys, "last_type"):
|
if not hasattr(sys, "last_exc") and not hasattr(sys, "last_type"):
|
||||||
raise ValueError("no last exception")
|
raise ValueError("no last exception")
|
||||||
|
|
||||||
if hasattr(sys, "last_exc"):
|
if hasattr(sys, "last_exc"):
|
||||||
print_exception(sys.last_exc, limit, file, chain)
|
print_exception(sys.last_exc, limit=limit, file=file, chain=chain)
|
||||||
else:
|
else:
|
||||||
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
|
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
|
||||||
limit, file, chain)
|
limit=limit, file=file, chain=chain)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix regression in ``traceback.print_last()``.
|
Loading…
Add table
Add a link
Reference in a new issue