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 摘要。