python / python/cpython

New REPL `input` bad cursor position when a backspace char `\b` is in the prompt

未关闭
#150,462 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

New REPL does not position the cursor correctly, if the prompt includes backspace character (\b = \x08)

# Basic REPL shows correct behavior
 ~$ PYTHON_BASIC_REPL=1 python3
Python 3.15.0a7+ (heads/main:5197ecb, Mar 10 2026, 20:55:52) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input("Question? _\b")
Question? █
Question? Yes█
'Yes'
>>> 

# New REPL misposition the cursor. 
 ~$ python3
Python 3.15.0a7+ (heads/main:5197ecb, Mar 10 2026, 20:55:52) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input("Question? _\b")
Question? _ █
Question? Yes  █
'Yes'
>>> 

Image

I've tested code below, and it fixes the issue:

--- ~/.pyenv/versions/3.15-dev/lib/python3.15/_pyrepl/utils.py
+++ ~/.pyenv/versions/3.15-dev/lib/python3.15/_pyrepl/utils.py
@@ -82,7 +82,9 @@
     # remove lengths of any escape sequences
     sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
     ctrl_z_cnt = s.count("\x1a")
-    return length - sum(len(i) for i in sequence) + ctrl_z_cnt
+    # remove length of backspaces x 2
+    backspace = s.count("\b")
+    return length - sum(len(i) for i in sequence) + ctrl_z_cnt - 2 * backspace
 
 
 def unbracket(s: str, including_content: bool = False) -> str:
CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-150488

贡献指南

打开贡献指南

从这里开始

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

调研方向

使用 input("Question? _\b") 重现新 REPL 中的光标位置问题,然后检查 _pyrepl/utils.py 中显示长度计算附近的代码。将其行为与 gh-150488 中链接的工作进行比较;当提示符和输入的文本使光标停留在与基本 REPL 相同的位置时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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