Skip to content

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

Open

Description

@florentx

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

Activity

  1. added
    type-bug
    on May 26, 2026
  2. added
    topic-repl
    on May 26, 2026
  3. added
    stdlib
    on May 26, 2026
  4. edvilme commented on May 26, 2026

    @edvilme
    Contributor

    Hi, if this is still available, I would like to work on it :)

  5. tanloong commented on May 30, 2026

    @tanloong
    Contributor

    The basic REPL input() has the same behavior on \b.

    Image
  6. florentx commented on May 30, 2026

    @florentx
    ContributorAuthor

    @tanloong what you describe is OK. It is not buggy.

    When using new REPL (Python 3.13+), the behavior is different. And this is the bug:

    >>> input("Question?\b")
    Question? █
  7. tanloong commented on May 30, 2026

    @tanloong
    Contributor

    @florentx Ah, you are right! I misunderstood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtopic-repltype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions