From d151f33d1728db290e7d50a063d613cc3b6101cd Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 15 Jul 2026 12:41:43 -0700 Subject: [PATCH] gh-153782: Make traceback.print_list() delegate to format_list() print_list() and format_list() each formatted a StackSummary with the same expression, so their outputs agreed only by duplication. Route print_list() through format_list() so print_list(x), keeping the two public helpers from drifting, and add a regression test that print_list() matches format_list() for tuple, FrameSummary, and StackSummary inputs. --- Lib/test/test_traceback.py | 17 +++++++++++++++++ Lib/traceback.py | 2 +- Misc/ACKS | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index e38d0942e463e9c..190618110d81a86 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -3431,6 +3431,23 @@ def test_format_smoke(self): [' File "foo.py", line 1, in fred\n line\n'], s.format()) + def test_print_list_matches_format_list(self): + frame = traceback.FrameSummary('foo.py', 1, 'fred', line='line') + inputs = [ + ('tuple', [('foo.py', 1, 'fred', 'line')]), + ('FrameSummary', [frame]), + ('StackSummary', + traceback.StackSummary.from_list([('foo.py', 1, 'fred', 'line')])), + ] + for input_type, extracted_list in inputs: + with self.subTest(input_type=input_type): + file = StringIO() + traceback.print_list(extracted_list, file=file) + self.assertEqual( + file.getvalue(), + ''.join(traceback.format_list(extracted_list)), + ) + def test_locals(self): linecache.updatecache('/foo.py', globals()) c = test_code('/foo.py', 'method') diff --git a/Lib/traceback.py b/Lib/traceback.py index dcdab1f12e9a168..10784e6eadff656 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -72,7 +72,7 @@ def print_list(extracted_list, file=None): extract_stack() as a formatted stack trace to the given file.""" if file is None: file = sys.stderr - for item in StackSummary.from_list(extracted_list).format(): + for item in format_list(extracted_list): print(item, file=file, end="") def format_list(extracted_list): diff --git a/Misc/ACKS b/Misc/ACKS index fec00c1b272f4ea..7fee4a8ccc67d63 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -404,6 +404,7 @@ Laura Creighton Nick Crews Tyler Crompton Simon Cross +Basil Crow Felipe Cruz Drew Csillag Alessandro Cucci