python / python/cpython

Incorrect code collapse while prompting where the error is.

未关闭
#128,327 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,在 CPython 3.13 或 3.14 上使用 issue 中的两个 Fibonacci 示例重现 traceback-collapse 行为,然后检查 traceback 格式化和帧折叠的入口点。将折叠后的输出与未裁剪的 traceback 以及链接的 PR gh-129973 进行比较;完成的标准是,重复帧被折叠后,仍能辨认出失败的表达式。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
developer-experience
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。