From 6b2ac1d67793c40e1d4d7fe39bf39eca4a26e9a5 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 19 Feb 2025 19:30:34 +0000 Subject: [PATCH 1/3] gh-130250: fix regression in traceback.print_last --- Lib/test/test_traceback.py | 7 +++++++ Lib/traceback.py | 4 ++-- .../Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index c2b115b53889d3..58f28902686a17 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -510,6 +510,13 @@ def test_print_exception_exc(self): traceback.print_exception(Exception("projector"), file=output) 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): e = Exception("projector") output = traceback.format_exception(e) diff --git a/Lib/traceback.py b/Lib/traceback.py index 2b402dd4cc2401..6cd9a284784375 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -217,10 +217,10 @@ def print_last(limit=None, file=None, chain=True): raise ValueError("no last exception") 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: print_exception(sys.last_type, sys.last_value, sys.last_traceback, - limit, file, chain) + limit=limit, file=file, chain=chain) # diff --git a/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst b/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst new file mode 100644 index 00000000000000..9b970cdfd67733 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst @@ -0,0 +1 @@ +Fix regression in `traceback.print_last()`. From 2e8cd55ba69065eedbdf3b4c91fa6f1ea24b386a Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 19 Feb 2025 19:35:23 +0000 Subject: [PATCH 2/3] fomatting --- .../next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst b/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst index 9b970cdfd67733..10ffb9dc1ee6a1 100644 --- a/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst +++ b/Misc/NEWS.d/next/Library/2025-02-19-19-29-19.gh-issue-130250.T00tql.rst @@ -1 +1 @@ -Fix regression in `traceback.print_last()`. +Fix regression in ``traceback.print_last()``. From 7d1ec9d629e6baf3053f0e06479a220e4deae959 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 19 Feb 2025 19:52:08 +0000 Subject: [PATCH 3/3] update doc/docstrings --- Doc/library/traceback.rst | 8 ++++---- Lib/traceback.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/traceback.rst b/Doc/library/traceback.rst index 7e05144bfb34cb..3475b014634d58 100644 --- a/Doc/library/traceback.rst +++ b/Doc/library/traceback.rst @@ -115,14 +115,14 @@ Module-Level Functions .. function:: print_exc(limit=None, file=None, chain=True) - This is a shorthand for ``print_exception(sys.exception(), limit, file, - chain)``. + This is a shorthand for ``print_exception(sys.exception(), limit=limit, file=file, + chain=chain)``. .. function:: print_last(limit=None, file=None, chain=True) - This is a shorthand for ``print_exception(sys.last_exc, limit, file, - chain)``. In general it will work only after an exception has reached + This is a shorthand for ``print_exception(sys.last_exc, limit=limit, file=file, + chain=chain)``. In general it will work only after an exception has reached an interactive prompt (see :data:`sys.last_exc`). diff --git a/Lib/traceback.py b/Lib/traceback.py index 6cd9a284784375..38ea5b4bdccf6d 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -204,7 +204,7 @@ def _safe_string(value, what, func=str): # -- 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) def format_exc(limit=None, chain=True): @@ -212,7 +212,7 @@ def format_exc(limit=None, chain=True): return "".join(format_exception(sys.exception(), limit=limit, chain=chain)) 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"): raise ValueError("no last exception")