python / python/cpython

Remove duplicate code by making `traceback.print_list()` delegate to `format_list()`

オープン
#153,782 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-refactor
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/traceback.py から始め、エントリーポイントである print_list() と format_list() および現在のフォーマット処理の経路を比較します。print_list() が出力やファイル処理を変更せずに format_list() に委譲すれば完了です。この issue では PR gh-153783 がリンクされているため、先にその作業を確認してから進めてください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
リファクタリング
難易度
1/5
見積もり時間
1時間未満
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。