python / python/cpython

TracebackException.format_exception_only() mutates attributes on keyword typo suggestion

未关闭
#154,544 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug description

TracebackException.format_exception_only() is not idempotent when _find_keyword_typos() finds a suggestion. The method directly mutates self.text, self.offset, self.end_offset, self.lineno, self.end_lineno, and self.msg, so a second call produces different output (e.g. wrong line number).

import traceback

code = "fr x in range(10):\n    pass\n"
try:
    compile(code, "<test>", "exec")
except SyntaxError as exc:
    te = traceback.TracebackException(type(exc), exc, None)
    r1 = list(te.format_exception_only())  # correct
    r2 = list(te.format_exception_only())  # different output
    assert r1 == r2  # fails

_find_keyword_typos was added in gh-132449.

CPython versions tested on

main

Operating systems tested on

macOS

Linked PRs
  • gh-154545

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 traceback.TracebackException.format_exception_only() 和 issue 中描述的 _find_keyword_typos() 入口点开始。复现提供的 SyntaxError 示例,调用 format_exception_only() 两次并比较结果。当重复调用产生完全相同的输出(包括行号和其他异常详细信息)时,即视为完成。

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

评估

技术栈
python
领域
compilers
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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