python / python/cpython

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

オープン
#150,462 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

新しい REPL で input("Question? _\b") を使ってカーソル位置の問題を再現し、その後、表示長の計算周辺にある _pyrepl/utils.py を調べます。gh-150488 に関連付けられた作業と動作を比較します。プロンプトと入力したテキストによって、カーソルが基本の REPL と同じ位置に残れば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
cli
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。