Skip to content

Commit

Permalink
pythongh-119469: Fix _pyrepl reference leaks (python#119470)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambv authored and estyxx committed Jul 17, 2024
1 parent afde92e commit 43e6a4b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Lib/test/test_pyrepl/test_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ def bar(self):
a
""")
console = InteractiveColoredConsole(namespace, filename="<stdin>")
f = io.StringIO()
with (
patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror,
patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource,
contextlib.redirect_stdout(f),
):
more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
self.assertFalse(more)
Expand Down Expand Up @@ -71,7 +73,9 @@ def test_runsource_compiles_and_runs_code(self):
def test_runsource_returns_false_for_successful_compilation(self):
console = InteractiveColoredConsole()
source = "print('Hello, world!')"
result = console.runsource(source)
f = io.StringIO()
with contextlib.redirect_stdout(f):
result = console.runsource(source)
self.assertFalse(result)

@force_not_colorized
Expand Down
36 changes: 25 additions & 11 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,37 @@ class TestConsole(TestCase):
def test_simple_addition(self, _os_write):
code = "12+34"
events = code_to_events(code)
_, _ = handle_events_unix_console(events)
_, con = handle_events_unix_console(events)
_os_write.assert_any_call(ANY, b"1")
_os_write.assert_any_call(ANY, b"2")
_os_write.assert_any_call(ANY, b"+")
_os_write.assert_any_call(ANY, b"3")
_os_write.assert_any_call(ANY, b"4")
con.restore()

def test_wrap(self, _os_write):
code = "12+34"
events = code_to_events(code)
_, _ = handle_events_narrow_unix_console(events)
_, con = handle_events_narrow_unix_console(events)
_os_write.assert_any_call(ANY, b"1")
_os_write.assert_any_call(ANY, b"2")
_os_write.assert_any_call(ANY, b"+")
_os_write.assert_any_call(ANY, b"3")
_os_write.assert_any_call(ANY, b"\\")
_os_write.assert_any_call(ANY, b"\n")
_os_write.assert_any_call(ANY, b"4")
con.restore()


def test_cursor_left(self, _os_write):
code = "1"
events = itertools.chain(
code_to_events(code),
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))],
)
_, _ = handle_events_unix_console(events)
_, con = handle_events_unix_console(events)
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
con.restore()

def test_cursor_left_right(self, _os_write):
code = "1"
Expand All @@ -149,18 +153,20 @@ def test_cursor_left_right(self, _os_write):
Event(evt="key", data="right", raw=bytearray(b"\x1bOC")),
],
)
_, _ = handle_events_unix_console(events)
_, con = handle_events_unix_console(events)
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuf"] + b":1")
con.restore()

def test_cursor_up(self, _os_write):
code = "1\n2+3"
events = itertools.chain(
code_to_events(code),
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))],
)
_, _ = handle_events_unix_console(events)
_, con = handle_events_unix_console(events)
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuu"] + b":1")
con.restore()

def test_cursor_up_down(self, _os_write):
code = "1\n2+3"
Expand All @@ -171,20 +177,22 @@ def test_cursor_up_down(self, _os_write):
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
],
)
_, _ = handle_events_unix_console(events)
_, con = handle_events_unix_console(events)
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuu"] + b":1")
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cud"] + b":1")
con.restore()

def test_cursor_back_write(self, _os_write):
events = itertools.chain(
code_to_events("1"),
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))],
code_to_events("2"),
)
_, _ = handle_events_unix_console(events)
_, con = handle_events_unix_console(events)
_os_write.assert_any_call(ANY, b"1")
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
_os_write.assert_any_call(ANY, b"2")
con.restore()

def test_multiline_function_move_up_short_terminal(self, _os_write):
# fmt: off
Expand All @@ -201,8 +209,9 @@ def test_multiline_function_move_up_short_terminal(self, _os_write):
Event(evt="scroll", data=None),
],
)
_, _ = handle_events_short_unix_console(events)
_, con = handle_events_short_unix_console(events)
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["ri"] + b":")
con.restore()

def test_multiline_function_move_up_down_short_terminal(self, _os_write):
# fmt: off
Expand All @@ -221,9 +230,10 @@ def test_multiline_function_move_up_down_short_terminal(self, _os_write):
Event(evt="scroll", data=None),
],
)
_, _ = handle_events_short_unix_console(events)
_, con = handle_events_short_unix_console(events)
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["ri"] + b":")
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["ind"] + b":")
con.restore()

def test_resize_bigger_on_multiline_function(self, _os_write):
# fmt: off
Expand All @@ -246,7 +256,7 @@ def same_console(events):
console.get_event = MagicMock(side_effect=events)
return console

_, _ = handle_all_events(
_, con = handle_all_events(
[Event(evt="resize", data=None)],
prepare_reader=same_reader,
prepare_console=same_console,
Expand All @@ -258,6 +268,8 @@ def same_console(events):
call(ANY, b"def f():"),
]
)
console.restore()
con.restore()

def test_resize_smaller_on_multiline_function(self, _os_write):
# fmt: off
Expand All @@ -280,7 +292,7 @@ def same_console(events):
console.get_event = MagicMock(side_effect=events)
return console

_, _ = handle_all_events(
_, con = handle_all_events(
[Event(evt="resize", data=None)],
prepare_reader=same_reader,
prepare_console=same_console,
Expand All @@ -292,3 +304,5 @@ def same_console(events):
call(ANY, b" foo"),
]
)
console.restore()
con.restore()

0 comments on commit 43e6a4b

Please sign in to comment.