python / python/cpython

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

Đang mở
#153,782 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-refactor
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong Lib/traceback.py bằng cách so sánh các entry point print_list() và format_list() cùng các luồng định dạng hiện tại của chúng. Công việc được hoàn tất khi print_list() ủy quyền cho format_list() mà không thay đổi đầu ra hoặc việc xử lý tệp; issue có liên kết đến PR gh-153783, vì vậy hãy kiểm tra phần việc đó trước khi tiếp tục.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
devtools
Loại issue
Tái cấu trúc
Độ khó
1/5
Thời gian dự kiến
Dưới một giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.