python / python/cpython

TabError and IndentationError from line continuation

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

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

The documentation states:

Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

When this rule applies to some, but not all, lines in a block, a TabError is sometimes unexpectedly raised:

L1 = 'if 1:\n'
L2 = '    1\n'
L3 = '    \\\n'
L4 = '        1\n'
exec(L1 + L2 + L3 + L4)
# works as expected

L1 = 'if 1:\n'
L2 = '\t1\n'
L3 = '\t\\\n'
L4 = '\t\t1\n'
exec(L1 + L2 + L3 + L4)
# TabError: inconsistent use of tabs and spaces in indentation

Also, when a line containing only a backslash is explicitly joined with an indented line, an IndentationError is unexpectedly raised:

exec('\\\n    1')
# IndentationError: unexpected indent
CPython versions tested on:

3.10, 3.11, 3.12, 3.13, 3.14

Operating systems tested on:

macOS

贡献指南

打开贡献指南

从这里开始

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

调研方向

先在列出的 CPython 版本上运行两个 exec() 复现程序,然后跟踪这两个案例中涉及的缩进和行继续处理。为报告的 TabError 和 IndentationError 行为添加回归测试覆盖,并确认测试捕获了预期结果,同时不改变无关的其他缩进错误。

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

评估

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

把新 issue 发到你的邮箱

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