Debian Codegraph @

Lib/test/test_pyrepl/test_unix_console.py

raw
1 import errno
2 import itertools
3 import os
4 import signal
5 import sys
6 import threading
7 import unittest
8 from functools import partial
9 from test.support import os_helper, force_not_colorized_test_class
10 from test.support import threading_helper
11
12 from unittest import TestCase
13 from unittest.mock import MagicMock, call, patch, ANY, Mock
14
15 from .support import handle_all_events, code_to_events
16
17 try:
18 from _pyrepl.console import Event
19 from _pyrepl.unix_console import UnixConsole
20 except ImportError:
21 pass
22
23 from _pyrepl.terminfo import _TERMINAL_CAPABILITIES
24
25 TERM_CAPABILITIES = _TERMINAL_CAPABILITIES["ansi"]
26
27
28 def unix_console(events, **kwargs):
29 console = UnixConsole(term="xterm")
30 console.get_event = MagicMock(side_effect=events)
31 console.getpending = MagicMock(return_value=Event("key", ""))
32
33 height = kwargs.get("height", 25)
34 width = kwargs.get("width", 80)
35 console.getheightwidth = MagicMock(side_effect=lambda: (height, width))
36 console.wait = MagicMock()
37
38 console.prepare()
39 for key, val in kwargs.items():
40 setattr(console, key, val)
41 return console
42
43
44 handle_events_unix_console = partial(
45 handle_all_events,
46 prepare_console=unix_console,
47 )
48 handle_events_narrow_unix_console = partial(
49 handle_all_events,
50 prepare_console=partial(unix_console, width=5),
51 )
52 handle_events_short_unix_console = partial(
53 handle_all_events,
54 prepare_console=partial(unix_console, height=1),
55 )
56 handle_events_unix_console_height_3 = partial(
57 handle_all_events, prepare_console=partial(unix_console, height=3)
58 )
59
60
61 @unittest.skipIf(sys.platform == "win32", "No Unix event queue on Windows")
62 @patch(
63 "_pyrepl.terminfo.tparm",
64 lambda s, *args: s + b":" + b",".join(str(i).encode() for i in args),
65 )
66 @patch(
67 "termios.tcgetattr",
68 lambda _: [
69 27394,
70 3,
71 19200,
72 536872399,
73 38400,
74 38400,
75 [
76 b"\x04",
77 b"\xff",
78 b"\xff",
79 b"\x7f",
80 b"\x17",
81 b"\x15",
82 b"\x12",
83 b"\x00",
84 b"\x03",
85 b"\x1c",
86 b"\x1a",
87 b"\x19",
88 b"\x11",
89 b"\x13",
90 b"\x16",
91 b"\x0f",
92 b"\x01",
93 b"\x00",
94 b"\x14",
95 b"\x00",
96 ],
97 ],
98 )
99 @patch("termios.tcsetattr", lambda a, b, c: None)
100 @patch("os.write")
101 @force_not_colorized_test_class
102 class TestConsole(TestCase):
103 def test_no_newline(self, _os_write):
104 code = "1"
105 events = code_to_events(code)
106 _, con = handle_events_unix_console(events)
107 self.assertNotIn(call(ANY, b'\n'), _os_write.mock_calls)
108 con.restore()
109
110 def test_newline(self, _os_write):
111 code = "\n"
112 events = code_to_events(code)
113 _, con = handle_events_unix_console(events)
114 _os_write.assert_any_call(ANY, b"\n")
115 con.restore()
116
117 def test_simple_addition(self, _os_write):
118 code = "12+34"
119 events = code_to_events(code)
120 _, con = handle_events_unix_console(events)
121 _os_write.assert_any_call(ANY, b"1")
122 _os_write.assert_any_call(ANY, b"2")
123 _os_write.assert_any_call(ANY, b"+")
124 _os_write.assert_any_call(ANY, b"3")
125 _os_write.assert_any_call(ANY, b"4")
126 con.restore()
127
128 def test_wrap(self, _os_write):
129 code = "12+34"
130 events = code_to_events(code)
131 _, con = handle_events_narrow_unix_console(events)
132 _os_write.assert_any_call(ANY, b"1")
133 _os_write.assert_any_call(ANY, b"2")
134 _os_write.assert_any_call(ANY, b"+")
135 _os_write.assert_any_call(ANY, b"3")
136 _os_write.assert_any_call(ANY, b"\\")
137 _os_write.assert_any_call(ANY, b"\n")
138 _os_write.assert_any_call(ANY, b"4")
139 con.restore()
140
141 def test_cursor_left(self, _os_write):
142 code = "1"
143 events = itertools.chain(
144 code_to_events(code),
145 [Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))],
146 )
147 _, con = handle_events_unix_console(events)
148 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
149 con.restore()
150
151 def test_cursor_left_right(self, _os_write):
152 code = "1"
153 events = itertools.chain(
154 code_to_events(code),
155 [
156 Event(evt="key", data="left", raw=bytearray(b"\x1bOD")),
157 Event(evt="key", data="right", raw=bytearray(b"\x1bOC")),
158 ],
159 )
160 _, con = handle_events_unix_console(events)
161 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
162 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuf"] + b":1")
163 con.restore()
164
165 def test_cursor_up(self, _os_write):
166 code = "1\n2+3"
167 events = itertools.chain(
168 code_to_events(code),
169 [Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))],
170 )
171 _, con = handle_events_unix_console(events)
172 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuu"] + b":1")
173 con.restore()
174
175 def test_cursor_up_down(self, _os_write):
176 code = "1\n2+3"
177 events = itertools.chain(
178 code_to_events(code),
179 [
180 Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
181 Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
182 ],
183 )
184 _, con = handle_events_unix_console(events)
185 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cuu"] + b":1")
186 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cud"] + b":1")
187 con.restore()
188
189 def test_cursor_back_write(self, _os_write):
190 events = itertools.chain(
191 code_to_events("1"),
192 [Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))],
193 code_to_events("2"),
194 )
195 _, con = handle_events_unix_console(events)
196 _os_write.assert_any_call(ANY, b"1")
197 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["cub"] + b":1")
198 _os_write.assert_any_call(ANY, b"2")
199 con.restore()
200
201 def test_multiline_function_move_up_short_terminal(self, _os_write):
202 # fmt: off
203 code = (
204 "def f():\n"
205 " foo"
206 )
207 # fmt: on
208
209 events = itertools.chain(
210 code_to_events(code),
211 [
212 Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
213 Event(evt="scroll", data=None),
214 ],
215 )
216 _, con = handle_events_short_unix_console(events)
217 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["ri"] + b":")
218 con.restore()
219
220 def test_multiline_function_move_up_down_short_terminal(self, _os_write):
221 # fmt: off
222 code = (
223 "def f():\n"
224 " foo"
225 )
226 # fmt: on
227
228 events = itertools.chain(
229 code_to_events(code),
230 [
231 Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
232 Event(evt="scroll", data=None),
233 Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
234 Event(evt="scroll", data=None),
235 ],
236 )
237 _, con = handle_events_short_unix_console(events)
238 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["ri"] + b":")
239 _os_write.assert_any_call(ANY, TERM_CAPABILITIES["ind"] + b":")
240 con.restore()
241
242 def test_resize_bigger_on_multiline_function(self, _os_write):
243 # fmt: off
244 code = (
245 "def f():\n"
246 " foo"
247 )
248 # fmt: on
249
250 events = itertools.chain(code_to_events(code))
251 reader, console = handle_events_short_unix_console(events)
252
253 console.getheightwidth = MagicMock(side_effect=lambda: (2, 80))
254
255 def same_reader(_):
256 return reader
257
258 def same_console(events):
259 console.get_event = MagicMock(side_effect=events)
260 return console
261
262 _, con = handle_all_events(
263 [Event(evt="resize", data=None)],
264 prepare_reader=same_reader,
265 prepare_console=same_console,
266 )
267 _os_write.assert_has_calls(
268 [
269 call(ANY, TERM_CAPABILITIES["ri"] + b":"),
270 call(ANY, TERM_CAPABILITIES["cup"] + b":0,0"),
271 call(ANY, b"def f():"),
272 ]
273 )
274 console.restore()
275 con.restore()
276
277 def test_resize_smaller_on_multiline_function(self, _os_write):
278 # fmt: off
279 code = (
280 "def f():\n"
281 " foo"
282 )
283 # fmt: on
284
285 events = itertools.chain(code_to_events(code))
286 reader, console = handle_events_unix_console_height_3(events)
287
288 console.getheightwidth = MagicMock(side_effect=lambda: (1, 80))
289
290 def same_reader(_):
291 return reader
292
293 def same_console(events):
294 console.get_event = MagicMock(side_effect=events)
295 return console
296
297 _, con = handle_all_events(
298 [Event(evt="resize", data=None)],
299 prepare_reader=same_reader,
300 prepare_console=same_console,
301 )
302 _os_write.assert_has_calls(
303 [
304 call(ANY, TERM_CAPABILITIES["ind"] + b":"),
305 call(ANY, TERM_CAPABILITIES["cup"] + b":0,0"),
306 call(ANY, b" foo"),
307 ]
308 )
309 console.restore()
310 con.restore()
311
312 def test_getheightwidth_with_invalid_environ(self, _os_write):
313 # gh-128636
314 console = UnixConsole(term="xterm")
315 with os_helper.EnvironmentVarGuard() as env:
316 env["LINES"] = ""
317 self.assertIsInstance(console.getheightwidth(), tuple)
318 env["COLUMNS"] = ""
319 self.assertIsInstance(console.getheightwidth(), tuple)
320 os.environ = []
321 self.assertIsInstance(console.getheightwidth(), tuple)
322
323 @unittest.skipUnless(sys.platform == "darwin", "requires macOS")
324 def test_restore_with_invalid_environ_on_macos(self, _os_write):
325 # gh-128636 for macOS
326 console = UnixConsole(term="xterm")
327 with os_helper.EnvironmentVarGuard():
328 os.environ = []
329 console.prepare() # needed to call restore()
330 console.restore() # this should succeed
331
332 @threading_helper.reap_threads
333 @threading_helper.requires_working_threading()
334 def test_restore_in_thread(self, _os_write):
335 # gh-139391: ensure that console.restore() silently suppresses
336 # exceptions when calling signal.signal() from a non-main thread.
337 console = unix_console([])
338 console.old_sigwinch = signal.SIG_DFL
339 thread = threading.Thread(target=console.restore)
340 thread.start()
341 thread.join() # this should not raise
342
343
344 @unittest.skipIf(sys.platform == "win32", "No Unix console on Windows")
345 class TestUnixConsoleEIOHandling(TestCase):
346
347 @patch('_pyrepl.unix_console.tcsetattr')
348 @patch('_pyrepl.unix_console.tcgetattr')
349 def test_eio_error_handling_in_restore(self, mock_tcgetattr, mock_tcsetattr):
350
351 import termios
352 mock_termios = Mock()
353 mock_termios.iflag = 0
354 mock_termios.oflag = 0
355 mock_termios.cflag = 0
356 mock_termios.lflag = 0
357 mock_termios.cc = [0] * 32
358 mock_termios.copy.return_value = mock_termios
359 mock_tcgetattr.return_value = mock_termios
360
361 console = UnixConsole(term="xterm")
362 console.prepare()
363
364 mock_tcsetattr.side_effect = termios.error(errno.EIO, "Input/output error")
365
366 # EIO error should be handled gracefully in restore()
367 console.restore()