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