Skip to content

Ctrl+C corrupts autocomplete #134869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
danielzgtg opened this issue May 28, 2025 · 1 comment
Open

Ctrl+C corrupts autocomplete #134869

danielzgtg opened this issue May 28, 2025 · 1 comment
Labels
stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@danielzgtg
Copy link

danielzgtg commented May 28, 2025

Bug report

Bug description:

  1. Open the REPL
  2. Type dict.c
  3. Press tab twice
  4. Press Ctrl + C

Expected behavior

There should be nothing after KeyboardInterrupt, and nothing after >>> like Node.js and PowerShell.

$ ./python
Python 3.15.0a0 (heads/main: future expected behavior ) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dict.c
KeyboardInterrupt
>>> 

Actual behavior

The autocomplete was not cleared, and what is a KeyboardInterrupt.copy(?

$ ./python
Python 3.15.0a0 (heads/main:e9d845b41dc, May 28 2025, 18:37:23) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dict.c
KeyboardInterrupt.copy(                                                                                                                                                               
>>> 
dict.clear(  dict.copy(

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

@danielzgtg danielzgtg added the type-bug An unexpected behavior, bug, or error label May 28, 2025
@tomasr8 tomasr8 added the topic-repl Related to the interactive shell label May 28, 2025
@ggqlq
Copy link
Contributor

ggqlq commented May 30, 2025

This issue appears to stem from the fact that the completion menu is not reset when handling the KeyboardInterrupt exception. To address this, we can add the reset logic in the run_multiline_interactive_console function of Lib/_pyrepl/simple_interact.py.

# Lib/_pyrepl/simple_interact.py line 159 to 171
        except KeyboardInterrupt:
            r = _get_reader()
+           r.cmpltn_menu_visible = False
+           r.cmpltn_message_visible = False
+           r.cmpltn_menu = []
+           r.cmpltn_menu_end = 0
            if r.input_trans is r.isearch_trans:
                r.do_cmd(("isearch-end", [""]))
            r.pos = len(r.get_unicode())
            r.dirty = True
            r.refresh()
            console.write("\nKeyboardInterrupt\n")
            console.resetbuffer()

The completion menu state variables (cmpltn_menu_visible, cmpltn_message_visible, etc.) control the display and content of the autocomplete suggestions. Resetting them ensures that any pending completions are cleared when the user interrupts with Ctrl+C.

Here's a preview of this fix:

# Before press ctrl + c
Python 3.15.0a0 (heads/gh-134869-dirty:c6003106632, May 30 2025, 20:53:02) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list.c
list.clear(  list.copy(   list.count(

# After press ctrl + c
Python 3.15.0a0 (heads/gh-134869-dirty:c6003106632, May 30 2025, 20:53:02) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list.c
KeyboardInterrupt
>>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants