python / python/cpython

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

Open
#150,462 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-repl type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the cursor-position problem in the new REPL with input("Question? _\b"), then inspect _pyrepl/utils.py around the displayed length calculation. Compare the behavior with the linked work in gh-150488; done means the prompt and typed text leave the cursor in the same position as the basic REPL.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.