Remove duplicate code by making `traceback.print_list()` delegate to `format_list()`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
In Lib/traceback.py, the public helpers print_list() and format_list() independently contain the same formatting expression, so their outputs agree only by duplication. We propose routing print_list() through format_list() so that they share a single formatting path, with no change in behavior.
Problem
format_list() returns the formatted lines:
def format_list(extracted_list):
return StackSummary.from_list(extracted_list).format()
print_list() writes those same lines, but re-derives them with the identical expression instead of reusing format_list():
def print_list(extracted_list, file=None):
if file is None:
file = sys.stderr
for item in StackSummary.from_list(extracted_list).format():
print(item, file=file, end="")
The outputs match only because StackSummary.from_list(extracted_list).format() is duplicated in both. Any future change to how a frame list is formatted has to be applied in both places to prevent the two public helpers from silently diverging.
Solution
Have print_list() iterate format_list():
def print_list(extracted_list, file=None):
if file is None:
file = sys.stderr
for item in format_list(extracted_list):
print(item, file=file, end="")
This eliminates the possibility of drift and mirrors the delegation already used in this module; for example, print_tb() calls print_list() rather than re-deriving extract_tb(...).format().
Linked PRs
- gh-153783
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Lib/traceback.py by comparing the print_list() and format_list() entry points and their current formatting paths. Done means print_list() delegates to format_list() without changing output or file handling; the issue links PR gh-153783, so check that work before proceeding.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Refactor
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100