8000 [3.13] gh-119469: Fix _pyrepl reference leaks (GH-119470) (#119471) · python/cpython@8fd8cc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8fd8cc5

Browse files
miss-islingtonambv
andauthored
[3.13] gh-119469: Fix _pyrepl reference leaks (GH-119470) (#119471)
(cherry picked from commit 6e012ce) Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 251ef2e commit 8fd8cc5

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

Lib/test/test_pyrepl/test_interact.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ def bar(self):
2727
a
2828
""")
2929
console = InteractiveColoredConsole(namespace, filename="<stdin>")
30+
f = io.StringIO()
3031
with (
3132
patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror,
3233
patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource,
34+
contextlib.redirect_stdout(f),
3335
):
3436
more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
3537
self.assertFalse(more)
@@ -71,7 +73,9 @@ def test_runsource_compiles_and_runs_code(self):
7173
def test_runsource_returns_false_for_successful_compilation(self):
7274
console = InteractiveColoredConsole()
7375
source = "print('Hello, world!')"
74-
result = console.runsource(source)
76+
f = io.StringIO()
77+
with contextlib.redirect_stdout(f):
78+
result = console.runsource(source)
7579
self.assertFalse(result)
7680

7781
@force_not_colorized

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,33 +112,37 @@ class TestConsole(TestCase):
112112
def test_simple_addition(self, _os_write):
113113
code = "12+34"
114114
events = code_to_events(code)
115-
_, _ = handle_events_unix_console(events)
115+
_, con = handle_events_unix_console(events)
116116
_os_write.assert_any_call(ANY, b"1")
117117
_os_write.assert_any_call(ANY, b"2")
118118
_os_write.assert_any_call(ANY, b"+")
119119
_os_write.assert_any_call(ANY, b"3")
120120
_os_write.assert_any_call(ANY, b"4")
121+
con.restore()
121122

122123
def test_wrap(self, _os_write):
123124
code = "12+34"
124125
events = code_to_events(code)
125-
_, _ = handle_events_narrow_unix_console(events)
126+
_, con = handle_events_narrow_unix_console(events)
126127
_os_write.assert_any_call(ANY, b"1")
127128
_os_write.assert_any_call(ANY, b"2")
128129
_os_write.assert_any_call(ANY, b"+")
129130
_os_write.assert_any_call(ANY, b"3")
130131
_os_write.assert_any_call(ANY, b"\\")
131132
_os_write.assert_any_call(ANY, b"\n")
132133
_os_write.assert_any_call(ANY, b"4")
134+
con.restore()
135+
133136

134137
def test_cursor_left(self, _os_write):
135138
code = "1"
136139
events = itertools.chain(
137140
code_to_events(code),
138141
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))],
139142
)
140-
_, _ = handle_events_unix_console(events)
143+
_, con = handle_events_unix_console(events)
141144
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
145+
con.restore()
142146

143147
def test_cursor_left_right(self, _os_write):
144148
code = "1"
@@ -149,18 +153,20 @@ def test_cursor_left_right(self, _os_write):
149153
Event(evt="key", data="right", raw=bytearray(b"\x1bOC")),
150154
],
151155
)
152-
_, _ = handle_events_unix_console(events)
156+
_, con = handle_events_unix_console(events)
153157
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
154158
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuf"] + b":1")
159+
con.restore()
155160

156161
def test_cursor_up(self, _os_write):
157162
code = "1\n2+3"
158163
events = itertools.chain(
159164
code_to_events(code),
160165
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))],
161166
)
162-
_, _ = handle_events_unix_console(events)
167+
_, con = handle_events_unix_console(events)
163168
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuu"] + b":1")
169+
con.restore()
164170

165171
def test_cursor_up_down(self, _os_write):
166172
code = "1\n2+3"
@@ -171,20 +177,22 @@ def test_cursor_up_down(self, _os_write):
171177
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
172178
],
173179
)
174-
_, _ = handle_events_unix_console(events)
180+
_, con = handle_events_unix_console(events)
175181
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuu"] + b":1")
176182
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cud"] + b":1")
183+
con.restore()
177184

178185
def test_cursor_back_write(self, _os_write):
179186
events = itertools.chain(
180187
code_to_events("1"),
181188
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))],
182189
code_to_events("2"),
183190
)
184-
_, _ = handle_events_unix_console(events)
191+
_, con = handle_events_unix_console(events)
185192
_os_write.assert_any_call(ANY, b"1")
186193
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
187194
_os_write.assert_any_call(ANY, b"2")
195+
con.restore()
188196

189197
def test_multiline_function_move_up_short_terminal(self, _os_write):
190198
# fmt: off
@@ -201,8 +209,9 @@ def test_multiline_function_move_up_short_terminal(self, _os_write):
201209
Event(evt="scroll", data=None),
202210
],
203211
)
204-
_, _ = handle_events_short_unix_console(events)
212+
_, con = handle_events_short_unix_console(events)
205213
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["ri"] + b":")
214+
con.restore()
206215

207216
def test_multiline_function_move_up_down_short_terminal(self, _os_write):
208217
# fmt: off
@@ -221,9 +230,10 @@ def test_multiline_function_move_up_down_short_terminal(self, _os_write):
221230
Event(evt="scroll", data=None),
222231
],
223232
)
224-
_, _ = handle_events_short_unix_console(events)
233+
_, con = handle_events_short_unix_console(events)
225234
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["ri"] + b":")
226235
_os_write.assert_any_call(ANY, TERM_CAPABILITIES["ind"] + b":")
236+
con.restore()
227237

228238
def test_resize_bigger_on_multiline_function(self, _os_write):
229239
# fmt: off
@@ -246,7 +256,7 @@ def same_console(events):
246256
console.get_event = MagicMock(side_effect=events)
247257
return console
248258

249-
_, _ = handle_all_events(
259+
_, con = handle_all_events(
250260
[Event(evt="resize", data=None)],
251261
prepare_reader=same_reader,
252262
prepare_console=same_console,
@@ -258,6 +268,8 @@ def same_console(events):
258268
call(ANY, b"def f():"),
259269
]
260270
)
271+
console.restore()
272+
con.restore()
261273

262274
def test_resize_smaller_on_multiline_function(self, _os_write):
263275
# fmt: off
@@ -280,7 +292,7 @@ def same_console(events):
280292
console.get_event = MagicMock(side_effect=events)
281293
return console
282294

283-
_, _ = handle_all_events(
295+
_, con = handle_all_events(
284296
[Event(evt="resize", data=None)],
285297
prepare_reader=same_reader,
286298
prepare_console=same_console,
@@ -292,3 +304,5 @@ def same_console(events):
292304
call(ANY, b" foo"),
293305
]
294306
)
307+
console.restore()
308+
con.restore()

0 commit comments

Comments
 (0)
0