Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-131430: Fix crashes on an empty DELETE_WORD_BACKWARDS (^W) followed by CLEAR_TO_START (^K) #131452

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
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
optimize pyrepl test cases using subtests
  • Loading branch information
deepwzh committed May 12, 2025
commit a1e2e3845c955e9e9e86f47fc95383a494ab8498
74 changes: 40 additions & 34 deletions 74 Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,44 +371,50 @@ def test_empty_line_control_w_k(self):

def test_control_w_delete_word(self):
"""Test Control-W delete word"""
def test_with_text(text: str, expected: list[str], before_pos: int, after_pos: int):
events = itertools.chain(
code_to_events(text),
[Event(evt="key", data="left", raw=bytearray(b"\x1b[D"))] * (len(text) - before_pos), # Move cursor to specified position
[
Event(evt="key", data="\x17", raw=bytearray(b"\x17")), # Control-W
],
)
reader, _ = handle_all_events(events)
self.assertEqual(reader.screen, expected)
self.assertEqual(reader.pos, after_pos)
cases = (
("", 0, 0, []),
("a", 1, 0, [""]),
("abc", 3, 0, [""]),
("abc def", 4, 0, ["def"]),
("abc def", 7, 4, ["abc "]),
("def xxx():xxx\n ", 18, 10, ["def xxx():"]),
)

test_with_text("", [], 0, 0)
test_with_text("a", [""], 1, 0)
test_with_text("abc", [""], 3, 0)
test_with_text("abc def", ["def"], 4, 0)
test_with_text("abc def", ["abc "], 7, 4)
test_with_text("def xxx():xxx\n ", ["def xxx():"], 18, 10)
for text, before_pos, after_pos, expected in cases:
with self.subTest(text=text, before_pos=before_pos):
events = itertools.chain(
code_to_events(text),
[Event(evt="key", data="left", raw=bytearray(b"\x1b[D"))] * (len(text) - before_pos), # Move cursor to specified position
[
Event(evt="key", data="\x17", raw=bytearray(b"\x17")), # Control-W
],
)
reader, _ = handle_all_events(events)
self.assertEqual(reader.screen, expected)
self.assertEqual(reader.pos, after_pos)

def test_control_k_delete_to_eol(self):
"""Test Control-K delete from cursor to end of line"""
def test_with_text(text: str, pos: int, expected: list[str]):
events = itertools.chain(
code_to_events(text) if len(text) else [],
[Event(evt="key", data="left", raw=bytearray(b"\x1b[D"))] * (len(text) - pos), # Move cursor to specified position
[
Event(evt="key", data="\x0b", raw=bytearray(b"\x0b")), # Control-K
],
)
reader, _ = handle_all_events(events)
self.assertEqual(reader.screen, expected)
self.assertEqual(reader.pos, pos)

test_with_text("", 0, [""])
test_with_text("a", 0, [""])
test_with_text("abc", 1, ["a"])
test_with_text("abc def", 4, ["abc "])
test_with_text("def xxx():xxx\n pass", 10, ["def xxx():", " pass"])
cases = (
("", 0, [""]),
("a", 0, [""]),
("abc", 1, ["a"]),
("abc def", 4, ["abc "]),
("def xxx():xxx\n pass", 10, ["def xxx():", " pass"]),
)

for text, pos, expected in cases:
with self.subTest(text=text, pos=pos):
events = itertools.chain(
code_to_events(text),
[Event(evt="key", data="left", raw=bytearray(b"\x1b[D"))] * (len(text) - pos), # Move cursor to specified position
[
Event(evt="key", data="\x0b", raw=bytearray(b"\x0b")), # Control-K
],
)
reader, _ = handle_all_events(events)
self.assertEqual(reader.screen, expected)
self.assertEqual(reader.pos, pos)

@force_colorized_test_class
class TestReaderInColor(ScreenEqualMixin, TestCase):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.