python / python/cpython

Incorrect code collapse while prompting where the error is.

Đang mở
#128,327 5 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-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

Read the following code:

# incorrect_collapse.py
def fib(number: int) -> int:
    assert number > 0
    if number == 1:
        return 1

    return fib(number - 1) + fib(number - 2)  # Notice here


print(fib(2))

There is a mistake in the code above, so if you try to run it, you will get the following traceback:

PC-Killer@ArchLinuxPC ~/p/bugs> python incorrect_collapse.py
Traceback (most recent call last):
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 9, in <module>
    print(fib(2))
          ~~~^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
                             ~~~^^^^^^^^^^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 2, in fib
    assert number > 0
           ^^^^^^^^^^
AssertionError

Notice that the error happened while running the code fib(number - 2).
However, if the number is 5, the duplicate code will be automatically collapsed:

# incorrect_collapse.py
def fib(number: int) -> int:
    assert number > 0
    if number == 1:
        return 1

    return fib(number - 1) + fib(number - 2)  # Notice here


print(fib(5))
PC-Killer@ArchLinuxPC ~/p/bugs [1]> python incorrect_collapse.py
Traceback (most recent call last):
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 9, in <module>
    print(fib(5))
          ~~~^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
           ~~~^^^^^^^^^^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
           ~~~^^^^^^^^^^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
           ~~~^^^^^^^^^^^^
  [Previous line repeated 1 more time]
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 2, in fib
    assert number > 0
           ^^^^^^^^^^
AssertionError

The error happened in fib(number - 2). However, in the traceback above, I can only suppose the error happened while running fib(number - 1), which increases the time to find where the real bug is.

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-129973

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 bằng cách tái hiện hành vi thu gọn traceback với hai ví dụ Fibonacci trong issue trên CPython 3.13 hoặc 3.14, sau đó kiểm tra các entry point để định dạng traceback và thu gọn frame. So sánh đầu ra đã thu gọn với traceback chưa bị cắt và PR được liên kết gh-129973; được coi là hoàn tất khi biểu thức gây lỗi vẫn có thể phân biệt được sau khi các frame lặp lại đã được thu gọn.

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
developer-experience
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
20/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.