Skip to content

Commit

Permalink
gh-119553: Clear reader on Ctrl-C command (GH-119801)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou authored Jun 4, 2024
1 parent d419d46 commit 010ea93
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def do(self) -> None:

class ctrl_c(Command):
def do(self) -> None:
self.reader.finish()
raise KeyboardInterrupt


Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_pyrepl/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def handle_all_events(
reader.handle1()
except StopIteration:
pass
except KeyboardInterrupt:
pass
return reader, console


Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ def test_newline_within_block_trailing_whitespace(self):
self.assert_screen_equals(reader, expected)
self.assertTrue(reader.finished)

def test_keyboard_interrupt_clears_screen(self):
namespace = {"itertools": itertools}
code = "import itertools\nitertools."
events = itertools.chain(code_to_events(code), [
Event(evt='key', data='\t', raw=bytearray(b'\t')), # Two tabs for completion
Event(evt='key', data='\t', raw=bytearray(b'\t')),
Event(evt='key', data='\x03', raw=bytearray(b'\x03')), # Ctrl-C
])

completing_reader = functools.partial(
prepare_reader,
readline_completer=rlcompleter.Completer(namespace).complete
)
reader, _ = handle_all_events(events, prepare_reader=completing_reader)
self.assertEqual(reader.calc_screen(), code.split("\n"))

def test_prompt_length(self):
# Handles simple ASCII prompt
ps1 = ">>> "
Expand Down

0 comments on commit 010ea93

Please sign in to comment.