| 1 |
import importlib |
| 2 |
import io |
| 3 |
import itertools |
| 4 |
import os |
| 5 |
import pathlib |
| 6 |
import pkgutil |
| 7 |
import re |
| 8 |
import rlcompleter |
| 9 |
import select |
| 10 |
import subprocess |
| 11 |
import sys |
| 12 |
import tempfile |
| 13 |
from pkgutil import ModuleInfo |
| 14 |
from unittest import TestCase, skipUnless, skipIf, SkipTest |
| 15 |
from unittest.mock import Mock, patch |
| 16 |
from test.support import force_not_colorized, make_clean_env, Py_DEBUG |
| 17 |
from test.support import has_subprocess_support, SHORT_TIMEOUT, STDLIB_DIR |
| 18 |
from test.support.import_helper import import_module |
| 19 |
from test.support.os_helper import EnvironmentVarGuard, unlink |
| 20 |
|
| 21 |
from .support import ( |
| 22 |
FakeConsole, |
| 23 |
ScreenEqualMixin, |
| 24 |
handle_all_events, |
| 25 |
handle_events_narrow_console, |
| 26 |
more_lines, |
| 27 |
multiline_input, |
| 28 |
code_to_events, |
| 29 |
) |
| 30 |
from _pyrepl.console import Event |
| 31 |
from _pyrepl._module_completer import ( |
| 32 |
ImportParser, |
| 33 |
ModuleCompleter, |
| 34 |
HARDCODED_SUBMODULES, |
| 35 |
) |
| 36 |
from _pyrepl.readline import ( |
| 37 |
ReadlineAlikeReader, |
| 38 |
ReadlineConfig, |
| 39 |
_ReadlineWrapper, |
| 40 |
) |
| 41 |
from _pyrepl.readline import multiline_input as readline_multiline_input |
| 42 |
|
| 43 |
try: |
| 44 |
import pty |
| 45 |
except ImportError: |
| 46 |
pty = None |
| 47 |
try: |
| 48 |
import readline as readline_module |
| 49 |
except ImportError: |
| 50 |
readline_module = None |
| 51 |
|
| 52 |
|
| 53 |
class ReplTestCase(TestCase): |
| 54 |
def setUp(self): |
| 55 |
if not has_subprocess_support: |
| 56 |
raise SkipTest("test module requires subprocess") |
| 57 |
|
| 58 |
def run_repl( |
| 59 |
self, |
| 60 |
repl_input: str | list[str], |
| 61 |
env: dict | None = None, |
| 62 |
*, |
| 63 |
cmdline_args: list[str] | None = None, |
| 64 |
cwd: str | None = None, |
| 65 |
skip: bool = False, |
| 66 |
timeout: float = SHORT_TIMEOUT, |
| 67 |
exit_on_output: str | None = None, |
| 68 |
) -> tuple[str, int]: |
| 69 |
temp_dir = None |
| 70 |
if cwd is None: |
| 71 |
temp_dir = tempfile.TemporaryDirectory(ignore_cleanup_errors=True) |
| 72 |
cwd = temp_dir.name |
| 73 |
try: |
| 74 |
return self._run_repl( |
| 75 |
repl_input, |
| 76 |
env=env, |
| 77 |
cmdline_args=cmdline_args, |
| 78 |
cwd=cwd, |
| 79 |
skip=skip, |
| 80 |
timeout=timeout, |
| 81 |
exit_on_output=exit_on_output, |
| 82 |
) |
| 83 |
finally: |
| 84 |
if temp_dir is not None: |
| 85 |
temp_dir.cleanup() |
| 86 |
|
| 87 |
def _run_repl( |
| 88 |
self, |
| 89 |
repl_input: str | list[str], |
| 90 |
*, |
| 91 |
env: dict | None, |
| 92 |
cmdline_args: list[str] | None, |
| 93 |
cwd: str, |
| 94 |
skip: bool, |
| 95 |
timeout: float, |
| 96 |
exit_on_output: str | None, |
| 97 |
) -> tuple[str, int]: |
| 98 |
assert pty |
| 99 |
master_fd, slave_fd = pty.openpty() |
| 100 |
cmd = [sys.executable, "-i", "-u"] |
| 101 |
if env is None: |
| 102 |
cmd.append("-I") |
| 103 |
elif "PYTHON_HISTORY" not in env: |
| 104 |
env["PYTHON_HISTORY"] = os.path.join(cwd, ".regrtest_history") |
| 105 |
if cmdline_args is not None: |
| 106 |
cmd.extend(cmdline_args) |
| 107 |
|
| 108 |
try: |
| 109 |
import termios |
| 110 |
except ModuleNotFoundError: |
| 111 |
pass |
| 112 |
else: |
| 113 |
term_attr = termios.tcgetattr(slave_fd) |
| 114 |
term_attr[6][termios.VREPRINT] = 0 # pass through CTRL-R |
| 115 |
term_attr[6][termios.VINTR] = 0 # pass through CTRL-C |
| 116 |
termios.tcsetattr(slave_fd, termios.TCSANOW, term_attr) |
| 117 |
|
| 118 |
process = subprocess.Popen( |
| 119 |
cmd, |
| 120 |
stdin=slave_fd, |
| 121 |
stdout=slave_fd, |
| 122 |
stderr=slave_fd, |
| 123 |
cwd=cwd, |
| 124 |
text=True, |
| 125 |
close_fds=True, |
| 126 |
env=env if env else os.environ, |
| 127 |
) |
| 128 |
os.close(slave_fd) |
| 129 |
if isinstance(repl_input, list): |
| 130 |
repl_input = "\n".join(repl_input) + "\n" |
| 131 |
os.write(master_fd, repl_input.encode("utf-8")) |
| 132 |
|
| 133 |
output = [] |
| 134 |
while select.select([master_fd], [], [], timeout)[0]: |
| 135 |
try: |
| 136 |
data = os.read(master_fd, 1024).decode("utf-8") |
| 137 |
if not data: |
| 138 |
break |
| 139 |
except OSError: |
| 140 |
break |
| 141 |
output.append(data) |
| 142 |
if exit_on_output is not None: |
| 143 |
output = ["".join(output)] |
| 144 |
if exit_on_output in output[0]: |
| 145 |
process.kill() |
| 146 |
break |
| 147 |
else: |
| 148 |
os.close(master_fd) |
| 149 |
process.kill() |
| 150 |
process.wait(timeout=timeout) |
| 151 |
self.fail(f"Timeout while waiting for output, got: {''.join(output)}") |
| 152 |
|
| 153 |
os.close(master_fd) |
| 154 |
try: |
| 155 |
exit_code = process.wait(timeout=timeout) |
| 156 |
except subprocess.TimeoutExpired: |
| 157 |
process.kill() |
| 158 |
exit_code = process.wait() |
| 159 |
output = "".join(output) |
| 160 |
if skip and "can't use pyrepl" in output: |
| 161 |
self.skipTest("pyrepl not available") |
| 162 |
return output, exit_code |
| 163 |
|
| 164 |
|
| 165 |
class TestCursorPosition(TestCase): |
| 166 |
def prepare_reader(self, events): |
| 167 |
console = FakeConsole(events) |
| 168 |
config = ReadlineConfig(readline_completer=None) |
| 169 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 170 |
return reader |
| 171 |
|
| 172 |
def test_up_arrow_simple(self): |
| 173 |
# fmt: off |
| 174 |
code = ( |
| 175 |
"def f():\n" |
| 176 |
" ...\n" |
| 177 |
) |
| 178 |
# fmt: on |
| 179 |
events = itertools.chain( |
| 180 |
code_to_events(code), |
| 181 |
[ |
| 182 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 183 |
], |
| 184 |
) |
| 185 |
|
| 186 |
reader, console = handle_all_events(events) |
| 187 |
self.assertEqual(reader.cxy, (0, 1)) |
| 188 |
console.move_cursor.assert_called_once_with(0, 1) |
| 189 |
|
| 190 |
def test_down_arrow_end_of_input(self): |
| 191 |
# fmt: off |
| 192 |
code = ( |
| 193 |
"def f():\n" |
| 194 |
" ...\n" |
| 195 |
) |
| 196 |
# fmt: on |
| 197 |
events = itertools.chain( |
| 198 |
code_to_events(code), |
| 199 |
[ |
| 200 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 201 |
], |
| 202 |
) |
| 203 |
|
| 204 |
reader, console = handle_all_events(events) |
| 205 |
self.assertEqual(reader.cxy, (0, 2)) |
| 206 |
console.move_cursor.assert_called_once_with(0, 2) |
| 207 |
|
| 208 |
def test_left_arrow_simple(self): |
| 209 |
events = itertools.chain( |
| 210 |
code_to_events("11+11"), |
| 211 |
[ |
| 212 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 213 |
], |
| 214 |
) |
| 215 |
|
| 216 |
reader, console = handle_all_events(events) |
| 217 |
self.assertEqual(reader.cxy, (4, 0)) |
| 218 |
console.move_cursor.assert_called_once_with(4, 0) |
| 219 |
|
| 220 |
def test_right_arrow_end_of_line(self): |
| 221 |
events = itertools.chain( |
| 222 |
code_to_events("11+11"), |
| 223 |
[ |
| 224 |
Event(evt="key", data="right", raw=bytearray(b"\x1bOC")), |
| 225 |
], |
| 226 |
) |
| 227 |
|
| 228 |
reader, console = handle_all_events(events) |
| 229 |
self.assertEqual(reader.cxy, (5, 0)) |
| 230 |
console.move_cursor.assert_called_once_with(5, 0) |
| 231 |
|
| 232 |
def test_cursor_position_simple_character(self): |
| 233 |
events = itertools.chain(code_to_events("k")) |
| 234 |
|
| 235 |
reader, _ = handle_all_events(events) |
| 236 |
self.assertEqual(reader.pos, 1) |
| 237 |
|
| 238 |
# 1 for simple character |
| 239 |
self.assertEqual(reader.cxy, (1, 0)) |
| 240 |
|
| 241 |
def test_cursor_position_double_width_character(self): |
| 242 |
events = itertools.chain(code_to_events("樂")) |
| 243 |
|
| 244 |
reader, _ = handle_all_events(events) |
| 245 |
self.assertEqual(reader.pos, 1) |
| 246 |
|
| 247 |
# 2 for wide character |
| 248 |
self.assertEqual(reader.cxy, (2, 0)) |
| 249 |
|
| 250 |
def test_cursor_position_double_width_character_move_left(self): |
| 251 |
events = itertools.chain( |
| 252 |
code_to_events("樂"), |
| 253 |
[ |
| 254 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 255 |
], |
| 256 |
) |
| 257 |
|
| 258 |
reader, _ = handle_all_events(events) |
| 259 |
self.assertEqual(reader.pos, 0) |
| 260 |
self.assertEqual(reader.cxy, (0, 0)) |
| 261 |
|
| 262 |
def test_cursor_position_double_width_character_move_left_right(self): |
| 263 |
events = itertools.chain( |
| 264 |
code_to_events("樂"), |
| 265 |
[ |
| 266 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 267 |
Event(evt="key", data="right", raw=bytearray(b"\x1bOC")), |
| 268 |
], |
| 269 |
) |
| 270 |
|
| 271 |
reader, _ = handle_all_events(events) |
| 272 |
self.assertEqual(reader.pos, 1) |
| 273 |
|
| 274 |
# 2 for wide character |
| 275 |
self.assertEqual(reader.cxy, (2, 0)) |
| 276 |
|
| 277 |
def test_cursor_position_double_width_characters_move_up(self): |
| 278 |
for_loop = "for _ in _:" |
| 279 |
|
| 280 |
# fmt: off |
| 281 |
code = ( |
| 282 |
f"{for_loop}\n" |
| 283 |
" ' 可口可乐; 可口可樂'" |
| 284 |
) |
| 285 |
# fmt: on |
| 286 |
|
| 287 |
events = itertools.chain( |
| 288 |
code_to_events(code), |
| 289 |
[ |
| 290 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 291 |
], |
| 292 |
) |
| 293 |
|
| 294 |
reader, _ = handle_all_events(events) |
| 295 |
|
| 296 |
# cursor at end of first line |
| 297 |
self.assertEqual(reader.pos, len(for_loop)) |
| 298 |
self.assertEqual(reader.cxy, (len(for_loop), 0)) |
| 299 |
|
| 300 |
def test_cursor_position_double_width_characters_move_up_down(self): |
| 301 |
for_loop = "for _ in _:" |
| 302 |
|
| 303 |
# fmt: off |
| 304 |
code = ( |
| 305 |
f"{for_loop}\n" |
| 306 |
" ' 可口可乐; 可口可樂'" |
| 307 |
) |
| 308 |
# fmt: on |
| 309 |
|
| 310 |
events = itertools.chain( |
| 311 |
code_to_events(code), |
| 312 |
[ |
| 313 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 314 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 315 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 316 |
], |
| 317 |
) |
| 318 |
|
| 319 |
reader, _ = handle_all_events(events) |
| 320 |
|
| 321 |
# cursor here (showing 2nd line only): |
| 322 |
# < ' 可口可乐; 可口可樂'> |
| 323 |
# ^ |
| 324 |
self.assertEqual(reader.pos, 19) |
| 325 |
self.assertEqual(reader.cxy, (10, 1)) |
| 326 |
|
| 327 |
def test_cursor_position_multiple_double_width_characters_move_left(self): |
| 328 |
events = itertools.chain( |
| 329 |
code_to_events("' 可口可乐; 可口可樂'"), |
| 330 |
[ |
| 331 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 332 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 333 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 334 |
], |
| 335 |
) |
| 336 |
|
| 337 |
reader, _ = handle_all_events(events) |
| 338 |
self.assertEqual(reader.pos, 10) |
| 339 |
|
| 340 |
# 1 for quote, 1 for space, 2 per wide character, |
| 341 |
# 1 for semicolon, 1 for space, 2 per wide character |
| 342 |
self.assertEqual(reader.cxy, (16, 0)) |
| 343 |
|
| 344 |
def test_cursor_position_move_up_to_eol(self): |
| 345 |
first_line = "for _ in _:" |
| 346 |
second_line = " hello" |
| 347 |
|
| 348 |
# fmt: off |
| 349 |
code = ( |
| 350 |
f"{first_line}\n" |
| 351 |
f"{second_line}\n" |
| 352 |
" h\n" |
| 353 |
" hel" |
| 354 |
) |
| 355 |
# fmt: on |
| 356 |
|
| 357 |
events = itertools.chain( |
| 358 |
code_to_events(code), |
| 359 |
[ |
| 360 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 361 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 362 |
], |
| 363 |
) |
| 364 |
|
| 365 |
reader, _ = handle_all_events(events) |
| 366 |
|
| 367 |
# Cursor should be at end of line 1, even though line 2 is shorter |
| 368 |
# for _ in _: |
| 369 |
# hello |
| 370 |
# h |
| 371 |
# hel |
| 372 |
self.assertEqual( |
| 373 |
reader.pos, len(first_line) + len(second_line) + 1 |
| 374 |
) # +1 for newline |
| 375 |
self.assertEqual(reader.cxy, (len(second_line), 1)) |
| 376 |
|
| 377 |
def test_cursor_position_move_down_to_eol(self): |
| 378 |
last_line = " hel" |
| 379 |
|
| 380 |
# fmt: off |
| 381 |
code = ( |
| 382 |
"for _ in _:\n" |
| 383 |
" hello\n" |
| 384 |
" h\n" |
| 385 |
f"{last_line}" |
| 386 |
) |
| 387 |
# fmt: on |
| 388 |
|
| 389 |
events = itertools.chain( |
| 390 |
code_to_events(code), |
| 391 |
[ |
| 392 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 393 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 394 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 395 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 396 |
], |
| 397 |
) |
| 398 |
|
| 399 |
reader, _ = handle_all_events(events) |
| 400 |
|
| 401 |
# Cursor should be at end of line 3, even though line 2 is shorter |
| 402 |
# for _ in _: |
| 403 |
# hello |
| 404 |
# h |
| 405 |
# hel |
| 406 |
self.assertEqual(reader.pos, len(code)) |
| 407 |
self.assertEqual(reader.cxy, (len(last_line), 3)) |
| 408 |
|
| 409 |
def test_cursor_position_multiple_mixed_lines_move_up(self): |
| 410 |
# fmt: off |
| 411 |
code = ( |
| 412 |
"def foo():\n" |
| 413 |
" x = '可口可乐; 可口可樂'\n" |
| 414 |
" y = 'abckdfjskldfjslkdjf'" |
| 415 |
) |
| 416 |
# fmt: on |
| 417 |
|
| 418 |
events = itertools.chain( |
| 419 |
code_to_events(code), |
| 420 |
13 * [Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))], |
| 421 |
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))], |
| 422 |
) |
| 423 |
|
| 424 |
reader, _ = handle_all_events(events) |
| 425 |
|
| 426 |
# By moving left, we're before the s: |
| 427 |
# y = 'abckdfjskldfjslkdjf' |
| 428 |
# ^ |
| 429 |
# And we should move before the semi-colon despite the different offset |
| 430 |
# x = '可口可乐; 可口可樂' |
| 431 |
# ^ |
| 432 |
self.assertEqual(reader.pos, 22) |
| 433 |
self.assertEqual(reader.cxy, (15, 1)) |
| 434 |
|
| 435 |
def test_cursor_position_after_wrap_and_move_up(self): |
| 436 |
# fmt: off |
| 437 |
code = ( |
| 438 |
"def foo():\n" |
| 439 |
" hello" |
| 440 |
) |
| 441 |
# fmt: on |
| 442 |
|
| 443 |
events = itertools.chain( |
| 444 |
code_to_events(code), |
| 445 |
[ |
| 446 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 447 |
], |
| 448 |
) |
| 449 |
reader, _ = handle_events_narrow_console(events) |
| 450 |
|
| 451 |
# The code looks like this: |
| 452 |
# def foo()\ |
| 453 |
# : |
| 454 |
# hello |
| 455 |
# After moving up we should be after the colon in line 2 |
| 456 |
self.assertEqual(reader.pos, 10) |
| 457 |
self.assertEqual(reader.cxy, (1, 1)) |
| 458 |
|
| 459 |
|
| 460 |
class TestPyReplAutoindent(TestCase): |
| 461 |
def prepare_reader(self, events): |
| 462 |
console = FakeConsole(events) |
| 463 |
config = ReadlineConfig(readline_completer=None) |
| 464 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 465 |
return reader |
| 466 |
|
| 467 |
def test_auto_indent_default(self): |
| 468 |
# fmt: off |
| 469 |
input_code = ( |
| 470 |
"def f():\n" |
| 471 |
"pass\n\n" |
| 472 |
) |
| 473 |
|
| 474 |
output_code = ( |
| 475 |
"def f():\n" |
| 476 |
" pass\n" |
| 477 |
" " |
| 478 |
) |
| 479 |
# fmt: on |
| 480 |
|
| 481 |
events = code_to_events(input_code) |
| 482 |
reader = self.prepare_reader(events) |
| 483 |
output = multiline_input(reader) |
| 484 |
self.assertEqual(output, output_code) |
| 485 |
|
| 486 |
def test_auto_indent_continuation(self): |
| 487 |
# auto indenting according to previous user indentation |
| 488 |
# fmt: off |
| 489 |
events = itertools.chain( |
| 490 |
code_to_events("def f():\n"), |
| 491 |
# add backspace to delete default auto-indent |
| 492 |
[ |
| 493 |
Event(evt="key", data="backspace", raw=bytearray(b"\x7f")), |
| 494 |
], |
| 495 |
code_to_events( |
| 496 |
" pass\n" |
| 497 |
"pass\n\n" |
| 498 |
), |
| 499 |
) |
| 500 |
|
| 501 |
output_code = ( |
| 502 |
"def f():\n" |
| 503 |
" pass\n" |
| 504 |
" pass\n" |
| 505 |
" " |
| 506 |
) |
| 507 |
# fmt: on |
| 508 |
|
| 509 |
reader = self.prepare_reader(events) |
| 510 |
output = multiline_input(reader) |
| 511 |
self.assertEqual(output, output_code) |
| 512 |
|
| 513 |
def test_auto_indent_prev_block(self): |
| 514 |
# auto indenting according to indentation in different block |
| 515 |
# fmt: off |
| 516 |
events = itertools.chain( |
| 517 |
code_to_events("def f():\n"), |
| 518 |
# add backspace to delete default auto-indent |
| 519 |
[ |
| 520 |
Event(evt="key", data="backspace", raw=bytearray(b"\x7f")), |
| 521 |
], |
| 522 |
code_to_events( |
| 523 |
" pass\n" |
| 524 |
"pass\n\n" |
| 525 |
), |
| 526 |
code_to_events( |
| 527 |
"def g():\n" |
| 528 |
"pass\n\n" |
| 529 |
), |
| 530 |
) |
| 531 |
|
| 532 |
output_code = ( |
| 533 |
"def g():\n" |
| 534 |
" pass\n" |
| 535 |
" " |
| 536 |
) |
| 537 |
# fmt: on |
| 538 |
|
| 539 |
reader = self.prepare_reader(events) |
| 540 |
output1 = multiline_input(reader) |
| 541 |
output2 = multiline_input(reader) |
| 542 |
self.assertEqual(output2, output_code) |
| 543 |
|
| 544 |
def test_auto_indent_multiline(self): |
| 545 |
# fmt: off |
| 546 |
events = itertools.chain( |
| 547 |
code_to_events( |
| 548 |
"def f():\n" |
| 549 |
"pass" |
| 550 |
), |
| 551 |
[ |
| 552 |
# go to the end of the first line |
| 553 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 554 |
Event(evt="key", data="\x05", raw=bytearray(b"\x1bO5")), |
| 555 |
# new line should be autoindented |
| 556 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 557 |
], |
| 558 |
code_to_events( |
| 559 |
"pass" |
| 560 |
), |
| 561 |
[ |
| 562 |
# go to end of last line |
| 563 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 564 |
Event(evt="key", data="\x05", raw=bytearray(b"\x1bO5")), |
| 565 |
# double newline to terminate the block |
| 566 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 567 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 568 |
], |
| 569 |
) |
| 570 |
|
| 571 |
output_code = ( |
| 572 |
"def f():\n" |
| 573 |
" pass\n" |
| 574 |
" pass\n" |
| 575 |
" " |
| 576 |
) |
| 577 |
# fmt: on |
| 578 |
|
| 579 |
reader = self.prepare_reader(events) |
| 580 |
output = multiline_input(reader) |
| 581 |
self.assertEqual(output, output_code) |
| 582 |
|
| 583 |
def test_auto_indent_with_comment(self): |
| 584 |
# fmt: off |
| 585 |
events = code_to_events( |
| 586 |
"def f(): # foo\n" |
| 587 |
"pass\n\n" |
| 588 |
) |
| 589 |
|
| 590 |
output_code = ( |
| 591 |
"def f(): # foo\n" |
| 592 |
" pass\n" |
| 593 |
" " |
| 594 |
) |
| 595 |
# fmt: on |
| 596 |
|
| 597 |
reader = self.prepare_reader(events) |
| 598 |
output = multiline_input(reader) |
| 599 |
self.assertEqual(output, output_code) |
| 600 |
|
| 601 |
def test_auto_indent_with_multicomment(self): |
| 602 |
# fmt: off |
| 603 |
events = code_to_events( |
| 604 |
"def f(): ## foo\n" |
| 605 |
"pass\n\n" |
| 606 |
) |
| 607 |
|
| 608 |
output_code = ( |
| 609 |
"def f(): ## foo\n" |
| 610 |
" pass\n" |
| 611 |
" " |
| 612 |
) |
| 613 |
# fmt: on |
| 614 |
|
| 615 |
reader = self.prepare_reader(events) |
| 616 |
output = multiline_input(reader) |
| 617 |
self.assertEqual(output, output_code) |
| 618 |
|
| 619 |
def test_auto_indent_ignore_comments(self): |
| 620 |
# fmt: off |
| 621 |
events = code_to_events( |
| 622 |
"pass #:\n" |
| 623 |
) |
| 624 |
|
| 625 |
output_code = ( |
| 626 |
"pass #:" |
| 627 |
) |
| 628 |
# fmt: on |
| 629 |
|
| 630 |
reader = self.prepare_reader(events) |
| 631 |
output = multiline_input(reader) |
| 632 |
self.assertEqual(output, output_code) |
| 633 |
|
| 634 |
|
| 635 |
class TestPyReplOutput(ScreenEqualMixin, TestCase): |
| 636 |
def prepare_reader(self, events): |
| 637 |
console = FakeConsole(events) |
| 638 |
config = ReadlineConfig(readline_completer=None) |
| 639 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 640 |
reader.can_colorize = False |
| 641 |
return reader |
| 642 |
|
| 643 |
def test_stdin_is_tty(self): |
| 644 |
# Used during test log analysis to figure out if a TTY was available. |
| 645 |
try: |
| 646 |
if os.isatty(sys.stdin.fileno()): |
| 647 |
return |
| 648 |
except OSError as ose: |
| 649 |
self.skipTest(f"stdin tty check failed: {ose}") |
| 650 |
else: |
| 651 |
self.skipTest("stdin is not a tty") |
| 652 |
|
| 653 |
def test_stdout_is_tty(self): |
| 654 |
# Used during test log analysis to figure out if a TTY was available. |
| 655 |
try: |
| 656 |
if os.isatty(sys.stdout.fileno()): |
| 657 |
return |
| 658 |
except OSError as ose: |
| 659 |
self.skipTest(f"stdout tty check failed: {ose}") |
| 660 |
else: |
| 661 |
self.skipTest("stdout is not a tty") |
| 662 |
|
| 663 |
def test_basic(self): |
| 664 |
reader = self.prepare_reader(code_to_events("1+1\n")) |
| 665 |
|
| 666 |
output = multiline_input(reader) |
| 667 |
self.assertEqual(output, "1+1") |
| 668 |
self.assert_screen_equal(reader, "1+1", clean=True) |
| 669 |
|
| 670 |
def test_get_line_buffer_returns_str(self): |
| 671 |
reader = self.prepare_reader(code_to_events("\n")) |
| 672 |
wrapper = _ReadlineWrapper(f_in=None, f_out=None, reader=reader) |
| 673 |
self.assertIs(type(wrapper.get_line_buffer()), str) |
| 674 |
|
| 675 |
def test_multiline_edit(self): |
| 676 |
events = itertools.chain( |
| 677 |
code_to_events("def f():\n...\n\n"), |
| 678 |
[ |
| 679 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 680 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 681 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 682 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 683 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), |
| 684 |
Event(evt="key", data="backspace", raw=bytearray(b"\x08")), |
| 685 |
Event(evt="key", data="g", raw=bytearray(b"g")), |
| 686 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 687 |
Event(evt="key", data="backspace", raw=bytearray(b"\x08")), |
| 688 |
Event(evt="key", data="delete", raw=bytearray(b"\x7F")), |
| 689 |
Event(evt="key", data="right", raw=bytearray(b"g")), |
| 690 |
Event(evt="key", data="backspace", raw=bytearray(b"\x08")), |
| 691 |
Event(evt="key", data="p", raw=bytearray(b"p")), |
| 692 |
Event(evt="key", data="a", raw=bytearray(b"a")), |
| 693 |
Event(evt="key", data="s", raw=bytearray(b"s")), |
| 694 |
Event(evt="key", data="s", raw=bytearray(b"s")), |
| 695 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 696 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 697 |
], |
| 698 |
) |
| 699 |
reader = self.prepare_reader(events) |
| 700 |
|
| 701 |
output = multiline_input(reader) |
| 702 |
expected = "def f():\n ...\n " |
| 703 |
self.assertEqual(output, expected) |
| 704 |
self.assert_screen_equal(reader, expected, clean=True) |
| 705 |
output = multiline_input(reader) |
| 706 |
expected = "def g():\n pass\n " |
| 707 |
self.assertEqual(output, expected) |
| 708 |
self.assert_screen_equal(reader, expected, clean=True) |
| 709 |
|
| 710 |
def test_history_navigation_with_up_arrow(self): |
| 711 |
events = itertools.chain( |
| 712 |
code_to_events("1+1\n2+2\n"), |
| 713 |
[ |
| 714 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 715 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 716 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 717 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 718 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 719 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 720 |
], |
| 721 |
) |
| 722 |
|
| 723 |
reader = self.prepare_reader(events) |
| 724 |
|
| 725 |
output = multiline_input(reader) |
| 726 |
self.assertEqual(output, "1+1") |
| 727 |
self.assert_screen_equal(reader, "1+1", clean=True) |
| 728 |
output = multiline_input(reader) |
| 729 |
self.assertEqual(output, "2+2") |
| 730 |
self.assert_screen_equal(reader, "2+2", clean=True) |
| 731 |
output = multiline_input(reader) |
| 732 |
self.assertEqual(output, "2+2") |
| 733 |
self.assert_screen_equal(reader, "2+2", clean=True) |
| 734 |
output = multiline_input(reader) |
| 735 |
self.assertEqual(output, "1+1") |
| 736 |
self.assert_screen_equal(reader, "1+1", clean=True) |
| 737 |
|
| 738 |
def test_history_with_multiline_entries(self): |
| 739 |
code = "def foo():\nx = 1\ny = 2\nz = 3\n\ndef bar():\nreturn 42\n\n" |
| 740 |
events = list(itertools.chain( |
| 741 |
code_to_events(code), |
| 742 |
[ |
| 743 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 744 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 745 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 746 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 747 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 748 |
] |
| 749 |
)) |
| 750 |
|
| 751 |
reader = self.prepare_reader(events) |
| 752 |
output = multiline_input(reader) |
| 753 |
output = multiline_input(reader) |
| 754 |
output = multiline_input(reader) |
| 755 |
expected = "def foo():\n x = 1\n y = 2\n z = 3\n " |
| 756 |
self.assert_screen_equal(reader, expected, clean=True) |
| 757 |
self.assertEqual(output, expected) |
| 758 |
|
| 759 |
|
| 760 |
def test_history_navigation_with_down_arrow(self): |
| 761 |
events = itertools.chain( |
| 762 |
code_to_events("1+1\n2+2\n"), |
| 763 |
[ |
| 764 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 765 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 766 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 767 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 768 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 769 |
], |
| 770 |
) |
| 771 |
|
| 772 |
reader = self.prepare_reader(events) |
| 773 |
|
| 774 |
output = multiline_input(reader) |
| 775 |
self.assertEqual(output, "1+1") |
| 776 |
self.assert_screen_equal(reader, "1+1", clean=True) |
| 777 |
|
| 778 |
def test_history_search(self): |
| 779 |
events = itertools.chain( |
| 780 |
code_to_events("1+1\n2+2\n3+3\n"), |
| 781 |
[ |
| 782 |
Event(evt="key", data="\x12", raw=bytearray(b"\x12")), |
| 783 |
Event(evt="key", data="1", raw=bytearray(b"1")), |
| 784 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 785 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 786 |
], |
| 787 |
) |
| 788 |
|
| 789 |
reader = self.prepare_reader(events) |
| 790 |
|
| 791 |
output = multiline_input(reader) |
| 792 |
self.assertEqual(output, "1+1") |
| 793 |
self.assert_screen_equal(reader, "1+1", clean=True) |
| 794 |
output = multiline_input(reader) |
| 795 |
self.assertEqual(output, "2+2") |
| 796 |
self.assert_screen_equal(reader, "2+2", clean=True) |
| 797 |
output = multiline_input(reader) |
| 798 |
self.assertEqual(output, "3+3") |
| 799 |
self.assert_screen_equal(reader, "3+3", clean=True) |
| 800 |
output = multiline_input(reader) |
| 801 |
self.assertEqual(output, "1+1") |
| 802 |
self.assert_screen_equal(reader, "1+1", clean=True) |
| 803 |
|
| 804 |
def test_control_character(self): |
| 805 |
events = code_to_events("c\x1d\n") |
| 806 |
reader = self.prepare_reader(events) |
| 807 |
output = multiline_input(reader) |
| 808 |
self.assertEqual(output, "c\x1d") |
| 809 |
self.assert_screen_equal(reader, "c\x1d", clean=True) |
| 810 |
|
| 811 |
def test_history_search_backward(self): |
| 812 |
# Test <page up> history search backward with "imp" input |
| 813 |
events = itertools.chain( |
| 814 |
code_to_events("import os\n"), |
| 815 |
code_to_events("imp"), |
| 816 |
[ |
| 817 |
Event(evt='key', data='page up', raw=bytearray(b'\x1b[5~')), |
| 818 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 819 |
], |
| 820 |
) |
| 821 |
|
| 822 |
# fill the history |
| 823 |
reader = self.prepare_reader(events) |
| 824 |
multiline_input(reader) |
| 825 |
|
| 826 |
# search for "imp" in history |
| 827 |
output = multiline_input(reader) |
| 828 |
self.assertEqual(output, "import os") |
| 829 |
self.assert_screen_equal(reader, "import os", clean=True) |
| 830 |
|
| 831 |
def test_history_search_backward_empty(self): |
| 832 |
# Test <page up> history search backward with an empty input |
| 833 |
events = itertools.chain( |
| 834 |
code_to_events("import os\n"), |
| 835 |
[ |
| 836 |
Event(evt='key', data='page up', raw=bytearray(b'\x1b[5~')), |
| 837 |
Event(evt="key", data="\n", raw=bytearray(b"\n")), |
| 838 |
], |
| 839 |
) |
| 840 |
|
| 841 |
# fill the history |
| 842 |
reader = self.prepare_reader(events) |
| 843 |
multiline_input(reader) |
| 844 |
|
| 845 |
# search backward in history |
| 846 |
output = multiline_input(reader) |
| 847 |
self.assertEqual(output, "import os") |
| 848 |
self.assert_screen_equal(reader, "import os", clean=True) |
| 849 |
|
| 850 |
|
| 851 |
class TestPyReplCompleter(TestCase): |
| 852 |
def prepare_reader(self, events, namespace): |
| 853 |
console = FakeConsole(events) |
| 854 |
config = ReadlineConfig() |
| 855 |
config.readline_completer = rlcompleter.Completer(namespace).complete |
| 856 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 857 |
return reader |
| 858 |
|
| 859 |
@patch("rlcompleter._readline_available", False) |
| 860 |
def test_simple_completion(self): |
| 861 |
events = code_to_events("os.getpid\t\n") |
| 862 |
|
| 863 |
namespace = {"os": os} |
| 864 |
reader = self.prepare_reader(events, namespace) |
| 865 |
|
| 866 |
output = multiline_input(reader, namespace) |
| 867 |
self.assertEqual(output, "os.getpid()") |
| 868 |
|
| 869 |
def test_completion_with_many_options(self): |
| 870 |
# Test with something that initially displays many options |
| 871 |
# and then complete from one of them. The first time tab is |
| 872 |
# pressed, the options are displayed (which corresponds to |
| 873 |
# when the repl shows [ not unique ]) and the second completes |
| 874 |
# from one of them. |
| 875 |
events = code_to_events("os.\t\tO_AP\t\n") |
| 876 |
|
| 877 |
namespace = {"os": os} |
| 878 |
reader = self.prepare_reader(events, namespace) |
| 879 |
|
| 880 |
output = multiline_input(reader, namespace) |
| 881 |
self.assertEqual(output, "os.O_APPEND") |
| 882 |
|
| 883 |
def test_empty_namespace_completion(self): |
| 884 |
events = code_to_events("os.geten\t\n") |
| 885 |
namespace = {} |
| 886 |
reader = self.prepare_reader(events, namespace) |
| 887 |
|
| 888 |
output = multiline_input(reader, namespace) |
| 889 |
self.assertEqual(output, "os.geten") |
| 890 |
|
| 891 |
def test_global_namespace_completion(self): |
| 892 |
events = code_to_events("py\t\n") |
| 893 |
namespace = {"python": None} |
| 894 |
reader = self.prepare_reader(events, namespace) |
| 895 |
output = multiline_input(reader, namespace) |
| 896 |
self.assertEqual(output, "python") |
| 897 |
|
| 898 |
def test_up_down_arrow_with_completion_menu(self): |
| 899 |
"""Up arrow in the middle of unfinished tab completion when the menu is displayed |
| 900 |
should work and trigger going back in history. Down arrow should subsequently |
| 901 |
get us back to the incomplete command.""" |
| 902 |
code = "import os\nos.\t\t" |
| 903 |
namespace = {"os": os} |
| 904 |
|
| 905 |
events = itertools.chain( |
| 906 |
code_to_events(code), |
| 907 |
[ |
| 908 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 909 |
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), |
| 910 |
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), |
| 911 |
], |
| 912 |
code_to_events("\n"), |
| 913 |
) |
| 914 |
reader = self.prepare_reader(events, namespace=namespace) |
| 915 |
output = multiline_input(reader, namespace) |
| 916 |
# This is the first line, nothing to see here |
| 917 |
self.assertEqual(output, "import os") |
| 918 |
# This is the second line. We pressed up and down arrows |
| 919 |
# so we should end up where we were when we initiated tab completion. |
| 920 |
output = multiline_input(reader, namespace) |
| 921 |
self.assertEqual(output, "os.") |
| 922 |
|
| 923 |
@patch("_pyrepl.readline._ReadlineWrapper.get_reader") |
| 924 |
@patch("sys.stderr", new_callable=io.StringIO) |
| 925 |
def test_completion_with_warnings(self, mock_stderr, mock_get_reader): |
| 926 |
class Dummy: |
| 927 |
@property |
| 928 |
def test_func(self): |
| 929 |
import warnings |
| 930 |
|
| 931 |
warnings.warn("warnings\n") |
| 932 |
return None |
| 933 |
|
| 934 |
dummy = Dummy() |
| 935 |
events = code_to_events("dummy.test_func.\t\n\n") |
| 936 |
namespace = {"dummy": dummy} |
| 937 |
reader = self.prepare_reader(events, namespace) |
| 938 |
mock_get_reader.return_value = reader |
| 939 |
output = readline_multiline_input(more_lines, ">>>", "...") |
| 940 |
self.assertEqual(output, "dummy.test_func.__") |
| 941 |
self.assertEqual(mock_stderr.getvalue(), "") |
| 942 |
|
| 943 |
|
| 944 |
class TestPyReplModuleCompleter(TestCase): |
| 945 |
def setUp(self): |
| 946 |
# Make iter_modules() search only the standard library. |
| 947 |
# This makes the test more reliable in case there are |
| 948 |
# other user packages/scripts on PYTHONPATH which can |
| 949 |
# interfere with the completions. |
| 950 |
lib_path = os.path.dirname(importlib.__path__[0]) |
| 951 |
self._saved_sys_path = sys.path |
| 952 |
sys.path = [lib_path] |
| 953 |
|
| 954 |
def tearDown(self): |
| 955 |
sys.path = self._saved_sys_path |
| 956 |
|
| 957 |
def prepare_reader(self, events, namespace): |
| 958 |
console = FakeConsole(events) |
| 959 |
config = ReadlineConfig() |
| 960 |
config.module_completer = ModuleCompleter(namespace) |
| 961 |
config.readline_completer = rlcompleter.Completer(namespace).complete |
| 962 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 963 |
return reader |
| 964 |
|
| 965 |
def test_import_completions(self): |
| 966 |
cases = ( |
| 967 |
("import path\t\n", "import pathlib"), |
| 968 |
("import importlib.\t\tres\t\n", "import importlib.resources"), |
| 969 |
("import importlib.resources.\t\ta\t\n", "import importlib.resources.abc"), |
| 970 |
("import foo, impo\t\n", "import foo, importlib"), |
| 971 |
("import foo as bar, impo\t\n", "import foo as bar, importlib"), |
| 972 |
("from impo\t\n", "from importlib"), |
| 973 |
("from importlib.res\t\n", "from importlib.resources"), |
| 974 |
("from importlib.\t\tres\t\n", "from importlib.resources"), |
| 975 |
("from importlib.resources.ab\t\n", "from importlib.resources.abc"), |
| 976 |
("from importlib import mac\t\n", "from importlib import machinery"), |
| 977 |
("from importlib import res\t\n", "from importlib import resources"), |
| 978 |
("from importlib.res\t import a\t\n", "from importlib.resources import abc"), |
| 979 |
("from __phello__ import s\t\n", "from __phello__ import spam"), # frozen module |
| 980 |
) |
| 981 |
for code, expected in cases: |
| 982 |
with self.subTest(code=code): |
| 983 |
events = code_to_events(code) |
| 984 |
reader = self.prepare_reader(events, namespace={}) |
| 985 |
output = reader.readline() |
| 986 |
self.assertEqual(output, expected) |
| 987 |
|
| 988 |
@patch("pkgutil.iter_modules", lambda: [ModuleInfo(None, "public", True), |
| 989 |
ModuleInfo(None, "_private", True)]) |
| 990 |
@patch("sys.builtin_module_names", ()) |
| 991 |
def test_private_completions(self): |
| 992 |
cases = ( |
| 993 |
# Return public methods by default |
| 994 |
("import \t\n", "import public"), |
| 995 |
("from \t\n", "from public"), |
| 996 |
# Return private methods if explicitly specified |
| 997 |
("import _\t\n", "import _private"), |
| 998 |
("from _\t\n", "from _private"), |
| 999 |
) |
| 1000 |
for code, expected in cases: |
| 1001 |
with self.subTest(code=code): |
| 1002 |
events = code_to_events(code) |
| 1003 |
reader = self.prepare_reader(events, namespace={}) |
| 1004 |
output = reader.readline() |
| 1005 |
self.assertEqual(output, expected) |
| 1006 |
|
| 1007 |
@patch( |
| 1008 |
"_pyrepl._module_completer.ModuleCompleter.iter_submodules", |
| 1009 |
lambda *_: [ |
| 1010 |
ModuleInfo(None, "public", True), |
| 1011 |
ModuleInfo(None, "_private", True), |
| 1012 |
], |
| 1013 |
) |
| 1014 |
def test_sub_module_private_completions(self): |
| 1015 |
cases = ( |
| 1016 |
# Return public methods by default |
| 1017 |
("from foo import \t\n", "from foo import public"), |
| 1018 |
# Return private methods if explicitly specified |
| 1019 |
("from foo import _\t\n", "from foo import _private"), |
| 1020 |
) |
| 1021 |
for code, expected in cases: |
| 1022 |
with self.subTest(code=code): |
| 1023 |
events = code_to_events(code) |
| 1024 |
reader = self.prepare_reader(events, namespace={}) |
| 1025 |
output = reader.readline() |
| 1026 |
self.assertEqual(output, expected) |
| 1027 |
|
| 1028 |
def test_builtin_completion_top_level(self): |
| 1029 |
cases = ( |
| 1030 |
("import bui\t\n", "import builtins"), |
| 1031 |
("from bui\t\n", "from builtins"), |
| 1032 |
) |
| 1033 |
for code, expected in cases: |
| 1034 |
with self.subTest(code=code): |
| 1035 |
events = code_to_events(code) |
| 1036 |
reader = self.prepare_reader(events, namespace={}) |
| 1037 |
output = reader.readline() |
| 1038 |
self.assertEqual(output, expected) |
| 1039 |
|
| 1040 |
def test_relative_import_completions(self): |
| 1041 |
cases = ( |
| 1042 |
(None, "from .readl\t\n", "from .readl"), |
| 1043 |
(None, "from . import readl\t\n", "from . import readl"), |
| 1044 |
("_pyrepl", "from .readl\t\n", "from .readline"), |
| 1045 |
("_pyrepl", "from . import readl\t\n", "from . import readline"), |
| 1046 |
("_pyrepl", "from .. import toodeep\t\n", "from .. import toodeep"), |
| 1047 |
("concurrent", "from .futures.i\t\n", "from .futures.interpreter"), |
| 1048 |
) |
| 1049 |
for package, code, expected in cases: |
| 1050 |
with self.subTest(code=code): |
| 1051 |
events = code_to_events(code) |
| 1052 |
reader = self.prepare_reader(events, namespace={"__package__": package}) |
| 1053 |
output = reader.readline() |
| 1054 |
self.assertEqual(output, expected) |
| 1055 |
|
| 1056 |
@patch("pkgutil.iter_modules", lambda: [ModuleInfo(None, "valid_name", True), |
| 1057 |
ModuleInfo(None, "invalid-name", True)]) |
| 1058 |
def test_invalid_identifiers(self): |
| 1059 |
# Make sure modules which are not valid identifiers |
| 1060 |
# are not suggested as those cannot be imported via 'import'. |
| 1061 |
cases = ( |
| 1062 |
("import valid\t\n", "import valid_name"), |
| 1063 |
# 'invalid-name' contains a dash and should not be completed |
| 1064 |
("import invalid\t\n", "import invalid"), |
| 1065 |
) |
| 1066 |
for code, expected in cases: |
| 1067 |
with self.subTest(code=code): |
| 1068 |
events = code_to_events(code) |
| 1069 |
reader = self.prepare_reader(events, namespace={}) |
| 1070 |
output = reader.readline() |
| 1071 |
self.assertEqual(output, expected) |
| 1072 |
|
| 1073 |
def test_no_fallback_on_regular_completion(self): |
| 1074 |
cases = ( |
| 1075 |
("import pri\t\n", "import pri"), |
| 1076 |
("from pri\t\n", "from pri"), |
| 1077 |
("from typing import Na\t\n", "from typing import Na"), |
| 1078 |
) |
| 1079 |
for code, expected in cases: |
| 1080 |
with self.subTest(code=code): |
| 1081 |
events = code_to_events(code) |
| 1082 |
reader = self.prepare_reader(events, namespace={}) |
| 1083 |
output = reader.readline() |
| 1084 |
self.assertEqual(output, expected) |
| 1085 |
|
| 1086 |
def test_global_cache(self): |
| 1087 |
with (tempfile.TemporaryDirectory() as _dir1, |
| 1088 |
patch.object(sys, "path", [_dir1, *sys.path])): |
| 1089 |
dir1 = pathlib.Path(_dir1) |
| 1090 |
(dir1 / "mod_aa.py").mkdir() |
| 1091 |
(dir1 / "mod_bb.py").mkdir() |
| 1092 |
events = code_to_events("import mod_a\t\nimport mod_b\t\n") |
| 1093 |
reader = self.prepare_reader(events, namespace={}) |
| 1094 |
output_1, output_2 = reader.readline(), reader.readline() |
| 1095 |
self.assertEqual(output_1, "import mod_aa") |
| 1096 |
self.assertEqual(output_2, "import mod_bb") |
| 1097 |
|
| 1098 |
def test_hardcoded_stdlib_submodules(self): |
| 1099 |
cases = ( |
| 1100 |
("import collections.\t\n", "import collections.abc"), |
| 1101 |
("from os import \t\n", "from os import path"), |
| 1102 |
("import xml.parsers.expat.\t\te\t\n\n", "import xml.parsers.expat.errors"), |
| 1103 |
("from xml.parsers.expat import \t\tm\t\n\n", "from xml.parsers.expat import model"), |
| 1104 |
) |
| 1105 |
for code, expected in cases: |
| 1106 |
with self.subTest(code=code): |
| 1107 |
events = code_to_events(code) |
| 1108 |
reader = self.prepare_reader(events, namespace={}) |
| 1109 |
output = reader.readline() |
| 1110 |
self.assertEqual(output, expected) |
| 1111 |
|
| 1112 |
def test_hardcoded_stdlib_submodules_not_proposed_if_local_import(self): |
| 1113 |
with (tempfile.TemporaryDirectory() as _dir, |
| 1114 |
patch.object(sys, "modules", {})): # hide imported module |
| 1115 |
dir = pathlib.Path(_dir) |
| 1116 |
(dir / "collections").mkdir() |
| 1117 |
(dir / "collections" / "__init__.py").touch() |
| 1118 |
(dir / "collections" / "foo.py").touch() |
| 1119 |
with patch.object(sys, "path", [_dir, *sys.path]): |
| 1120 |
events = code_to_events("import collections.\t\n") |
| 1121 |
reader = self.prepare_reader(events, namespace={}) |
| 1122 |
output = reader.readline() |
| 1123 |
self.assertEqual(output, "import collections.foo") |
| 1124 |
|
| 1125 |
def test_already_imported_stdlib_module_no_other_suggestions(self): |
| 1126 |
with (tempfile.TemporaryDirectory() as _dir, |
| 1127 |
patch.object(sys, "path", [_dir, *sys.path])): |
| 1128 |
dir = pathlib.Path(_dir) |
| 1129 |
(dir / "collections").mkdir() |
| 1130 |
(dir / "collections" / "__init__.py").touch() |
| 1131 |
(dir / "collections" / "foo.py").touch() |
| 1132 |
|
| 1133 |
# collections found in dir, but was already imported |
| 1134 |
# from stdlib at startup -> suggest stdlib submodules only |
| 1135 |
events = code_to_events("import collections.\t\n") |
| 1136 |
reader = self.prepare_reader(events, namespace={}) |
| 1137 |
output = reader.readline() |
| 1138 |
self.assertEqual(output, "import collections.abc") |
| 1139 |
|
| 1140 |
def test_already_imported_custom_module_no_suggestions(self): |
| 1141 |
with (tempfile.TemporaryDirectory() as _dir1, |
| 1142 |
tempfile.TemporaryDirectory() as _dir2, |
| 1143 |
patch.object(sys, "path", [_dir2, _dir1, *sys.path])): |
| 1144 |
dir1 = pathlib.Path(_dir1) |
| 1145 |
(dir1 / "mymodule").mkdir() |
| 1146 |
(dir1 / "mymodule" / "__init__.py").touch() |
| 1147 |
(dir1 / "mymodule" / "foo.py").touch() |
| 1148 |
importlib.import_module("mymodule") |
| 1149 |
|
| 1150 |
dir2 = pathlib.Path(_dir2) |
| 1151 |
(dir2 / "mymodule").mkdir() |
| 1152 |
(dir2 / "mymodule" / "__init__.py").touch() |
| 1153 |
(dir2 / "mymodule" / "bar.py").touch() |
| 1154 |
# Purge FileFinder cache after adding files |
| 1155 |
pkgutil.get_importer(_dir2).invalidate_caches() |
| 1156 |
# mymodule found in dir2 before dir1, but it was already imported |
| 1157 |
# from dir1 -> do not suggest dir2 submodules |
| 1158 |
events = code_to_events("import mymodule.\t\n") |
| 1159 |
reader = self.prepare_reader(events, namespace={}) |
| 1160 |
output = reader.readline() |
| 1161 |
self.assertEqual(output, "import mymodule.") |
| 1162 |
|
| 1163 |
del sys.modules["mymodule"] |
| 1164 |
# mymodule not imported anymore -> suggest dir2 submodules |
| 1165 |
events = code_to_events("import mymodule.\t\n") |
| 1166 |
reader = self.prepare_reader(events, namespace={}) |
| 1167 |
output = reader.readline() |
| 1168 |
self.assertEqual(output, "import mymodule.bar") |
| 1169 |
|
| 1170 |
def test_already_imported_custom_file_no_suggestions(self): |
| 1171 |
# Same as before, but mymodule from dir1 has no submodules |
| 1172 |
# -> propose nothing |
| 1173 |
with (tempfile.TemporaryDirectory() as _dir1, |
| 1174 |
tempfile.TemporaryDirectory() as _dir2, |
| 1175 |
patch.object(sys, "path", [_dir2, _dir1, *sys.path])): |
| 1176 |
dir1 = pathlib.Path(_dir1) |
| 1177 |
(dir1 / "mymodule").mkdir() |
| 1178 |
(dir1 / "mymodule.py").touch() |
| 1179 |
importlib.import_module("mymodule") |
| 1180 |
|
| 1181 |
dir2 = pathlib.Path(_dir2) |
| 1182 |
(dir2 / "mymodule").mkdir() |
| 1183 |
(dir2 / "mymodule" / "__init__.py").touch() |
| 1184 |
(dir2 / "mymodule" / "bar.py").touch() |
| 1185 |
events = code_to_events("import mymodule.\t\n") |
| 1186 |
reader = self.prepare_reader(events, namespace={}) |
| 1187 |
output = reader.readline() |
| 1188 |
self.assertEqual(output, "import mymodule.") |
| 1189 |
del sys.modules["mymodule"] |
| 1190 |
|
| 1191 |
def test_already_imported_module_without_origin_or_spec(self): |
| 1192 |
with (tempfile.TemporaryDirectory() as _dir1, |
| 1193 |
patch.object(sys, "path", [_dir1, *sys.path])): |
| 1194 |
dir1 = pathlib.Path(_dir1) |
| 1195 |
for mod in ("no_origin", "not_has_location", "no_spec"): |
| 1196 |
(dir1 / mod).mkdir() |
| 1197 |
(dir1 / mod / "__init__.py").touch() |
| 1198 |
(dir1 / mod / "foo.py").touch() |
| 1199 |
pkgutil.get_importer(_dir1).invalidate_caches() |
| 1200 |
module = importlib.import_module(mod) |
| 1201 |
assert module.__spec__ |
| 1202 |
if mod == "no_origin": |
| 1203 |
module.__spec__.origin = None |
| 1204 |
elif mod == "not_has_location": |
| 1205 |
module.__spec__.has_location = False |
| 1206 |
else: |
| 1207 |
module.__spec__ = None |
| 1208 |
events = code_to_events(f"import {mod}.\t\n") |
| 1209 |
reader = self.prepare_reader(events, namespace={}) |
| 1210 |
output = reader.readline() |
| 1211 |
self.assertEqual(output, f"import {mod}.") |
| 1212 |
del sys.modules[mod] |
| 1213 |
|
| 1214 |
def test_get_path_and_prefix(self): |
| 1215 |
cases = ( |
| 1216 |
('', ('', '')), |
| 1217 |
('.', ('.', '')), |
| 1218 |
('..', ('..', '')), |
| 1219 |
('.foo', ('.', 'foo')), |
| 1220 |
('..foo', ('..', 'foo')), |
| 1221 |
('..foo.', ('..foo', '')), |
| 1222 |
('..foo.bar', ('..foo', 'bar')), |
| 1223 |
('.foo.bar.', ('.foo.bar', '')), |
| 1224 |
('..foo.bar.', ('..foo.bar', '')), |
| 1225 |
('foo', ('', 'foo')), |
| 1226 |
('foo.', ('foo', '')), |
| 1227 |
('foo.bar', ('foo', 'bar')), |
| 1228 |
('foo.bar.', ('foo.bar', '')), |
| 1229 |
('foo.bar.baz', ('foo.bar', 'baz')), |
| 1230 |
) |
| 1231 |
completer = ModuleCompleter() |
| 1232 |
for name, expected in cases: |
| 1233 |
with self.subTest(name=name): |
| 1234 |
self.assertEqual(completer.get_path_and_prefix(name), expected) |
| 1235 |
|
| 1236 |
def test_parse(self): |
| 1237 |
cases = ( |
| 1238 |
('import ', (None, '')), |
| 1239 |
('import foo', (None, 'foo')), |
| 1240 |
('import foo,', (None, '')), |
| 1241 |
('import foo, ', (None, '')), |
| 1242 |
('import foo, bar', (None, 'bar')), |
| 1243 |
('import foo, bar, baz', (None, 'baz')), |
| 1244 |
('import foo as bar,', (None, '')), |
| 1245 |
('import foo as bar, ', (None, '')), |
| 1246 |
('import foo as bar, baz', (None, 'baz')), |
| 1247 |
('import a.', (None, 'a.')), |
| 1248 |
('import a.b', (None, 'a.b')), |
| 1249 |
('import a.b.', (None, 'a.b.')), |
| 1250 |
('import a.b.c', (None, 'a.b.c')), |
| 1251 |
('import a.b.c, foo', (None, 'foo')), |
| 1252 |
('import a.b.c, foo.bar', (None, 'foo.bar')), |
| 1253 |
('import a.b.c, foo.bar,', (None, '')), |
| 1254 |
('import a.b.c, foo.bar, ', (None, '')), |
| 1255 |
('from foo', ('foo', None)), |
| 1256 |
('from a.', ('a.', None)), |
| 1257 |
('from a.b', ('a.b', None)), |
| 1258 |
('from a.b.', ('a.b.', None)), |
| 1259 |
('from a.b.c', ('a.b.c', None)), |
| 1260 |
('from foo import ', ('foo', '')), |
| 1261 |
('from foo import a', ('foo', 'a')), |
| 1262 |
('from ', ('', None)), |
| 1263 |
('from . import a', ('.', 'a')), |
| 1264 |
('from .foo import a', ('.foo', 'a')), |
| 1265 |
('from ..foo import a', ('..foo', 'a')), |
| 1266 |
('from foo import (', ('foo', '')), |
| 1267 |
('from foo import ( ', ('foo', '')), |
| 1268 |
('from foo import (a', ('foo', 'a')), |
| 1269 |
('from foo import (a,', ('foo', '')), |
| 1270 |
('from foo import (a, ', ('foo', '')), |
| 1271 |
('from foo import (a, c', ('foo', 'c')), |
| 1272 |
('from foo import (a as b, c', ('foo', 'c')), |
| 1273 |
) |
| 1274 |
for code, parsed in cases: |
| 1275 |
parser = ImportParser(code) |
| 1276 |
actual = parser.parse() |
| 1277 |
with self.subTest(code=code): |
| 1278 |
self.assertEqual(actual, parsed) |
| 1279 |
# The parser should not get tripped up by any |
| 1280 |
# other preceding statements |
| 1281 |
_code = f'import xyz\n{code}' |
| 1282 |
parser = ImportParser(_code) |
| 1283 |
actual = parser.parse() |
| 1284 |
with self.subTest(code=_code): |
| 1285 |
self.assertEqual(actual, parsed) |
| 1286 |
_code = f'import xyz;{code}' |
| 1287 |
parser = ImportParser(_code) |
| 1288 |
actual = parser.parse() |
| 1289 |
with self.subTest(code=_code): |
| 1290 |
self.assertEqual(actual, parsed) |
| 1291 |
|
| 1292 |
def test_parse_error(self): |
| 1293 |
cases = ( |
| 1294 |
'', |
| 1295 |
'import foo ', |
| 1296 |
'from foo ', |
| 1297 |
'import foo. ', |
| 1298 |
'import foo.bar ', |
| 1299 |
'from foo ', |
| 1300 |
'from foo. ', |
| 1301 |
'from foo.bar ', |
| 1302 |
'from foo import bar ', |
| 1303 |
'from foo import (bar ', |
| 1304 |
'from foo import bar, baz ', |
| 1305 |
'import foo as', |
| 1306 |
'import a. as', |
| 1307 |
'import a.b as', |
| 1308 |
'import a.b. as', |
| 1309 |
'import a.b.c as', |
| 1310 |
'import (foo', |
| 1311 |
'import (', |
| 1312 |
'import .foo', |
| 1313 |
'import ..foo', |
| 1314 |
'import .foo.bar', |
| 1315 |
'import foo; x = 1', |
| 1316 |
'import foo; 1,', |
| 1317 |
'import a.; x = 1', |
| 1318 |
'import a.b; x = 1', |
| 1319 |
'import a.b.; x = 1', |
| 1320 |
'import a.b.c; x = 1', |
| 1321 |
'from foo import a as', |
| 1322 |
'from foo import a. as', |
| 1323 |
'from foo import a.b as', |
| 1324 |
'from foo import a.b. as', |
| 1325 |
'from foo import a.b.c as', |
| 1326 |
'from foo impo', |
| 1327 |
'import import', |
| 1328 |
'import from', |
| 1329 |
'import as', |
| 1330 |
'from import', |
| 1331 |
'from from', |
| 1332 |
'from as', |
| 1333 |
'from foo import import', |
| 1334 |
'from foo import from', |
| 1335 |
'from foo import as', |
| 1336 |
'from \\x', # _tokenize SyntaxError -> tokenize TokenError |
| 1337 |
'if 1:\n pass\n\tpass', # _tokenize TabError -> tokenize TabError |
| 1338 |
) |
| 1339 |
for code in cases: |
| 1340 |
parser = ImportParser(code) |
| 1341 |
actual = parser.parse() |
| 1342 |
with self.subTest(code=code): |
| 1343 |
self.assertEqual(actual, None) |
| 1344 |
|
| 1345 |
|
| 1346 |
class TestHardcodedSubmodules(TestCase): |
| 1347 |
def test_hardcoded_stdlib_submodules_are_importable(self): |
| 1348 |
for parent_path, submodules in HARDCODED_SUBMODULES.items(): |
| 1349 |
for module_name in submodules: |
| 1350 |
path = f"{parent_path}.{module_name}" |
| 1351 |
with self.subTest(path=path): |
| 1352 |
# We can't use importlib.util.find_spec here, |
| 1353 |
# since some hardcoded submodules parents are |
| 1354 |
# not proper packages |
| 1355 |
importlib.import_module(path) |
| 1356 |
|
| 1357 |
|
| 1358 |
class TestPasteEvent(TestCase): |
| 1359 |
def prepare_reader(self, events): |
| 1360 |
console = FakeConsole(events) |
| 1361 |
config = ReadlineConfig(readline_completer=None) |
| 1362 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 1363 |
return reader |
| 1364 |
|
| 1365 |
def test_paste(self): |
| 1366 |
# fmt: off |
| 1367 |
code = ( |
| 1368 |
"def a():\n" |
| 1369 |
" for x in range(10):\n" |
| 1370 |
" if x%2:\n" |
| 1371 |
" print(x)\n" |
| 1372 |
" else:\n" |
| 1373 |
" pass\n" |
| 1374 |
) |
| 1375 |
# fmt: on |
| 1376 |
|
| 1377 |
events = itertools.chain( |
| 1378 |
[ |
| 1379 |
Event(evt="key", data="f3", raw=bytearray(b"\x1bOR")), |
| 1380 |
], |
| 1381 |
code_to_events(code), |
| 1382 |
[ |
| 1383 |
Event(evt="key", data="f3", raw=bytearray(b"\x1bOR")), |
| 1384 |
], |
| 1385 |
code_to_events("\n"), |
| 1386 |
) |
| 1387 |
reader = self.prepare_reader(events) |
| 1388 |
output = multiline_input(reader) |
| 1389 |
self.assertEqual(output, code) |
| 1390 |
|
| 1391 |
def test_paste_mid_newlines(self): |
| 1392 |
# fmt: off |
| 1393 |
code = ( |
| 1394 |
"def f():\n" |
| 1395 |
" x = y\n" |
| 1396 |
" \n" |
| 1397 |
" y = z\n" |
| 1398 |
) |
| 1399 |
# fmt: on |
| 1400 |
|
| 1401 |
events = itertools.chain( |
| 1402 |
[ |
| 1403 |
Event(evt="key", data="f3", raw=bytearray(b"\x1bOR")), |
| 1404 |
], |
| 1405 |
code_to_events(code), |
| 1406 |
[ |
| 1407 |
Event(evt="key", data="f3", raw=bytearray(b"\x1bOR")), |
| 1408 |
], |
| 1409 |
code_to_events("\n"), |
| 1410 |
) |
| 1411 |
reader = self.prepare_reader(events) |
| 1412 |
output = multiline_input(reader) |
| 1413 |
self.assertEqual(output, code) |
| 1414 |
|
| 1415 |
def test_paste_mid_newlines_not_in_paste_mode(self): |
| 1416 |
# fmt: off |
| 1417 |
code = ( |
| 1418 |
"def f():\n" |
| 1419 |
"x = y\n" |
| 1420 |
"\n" |
| 1421 |
"y = z\n\n" |
| 1422 |
) |
| 1423 |
|
| 1424 |
expected = ( |
| 1425 |
"def f():\n" |
| 1426 |
" x = y\n" |
| 1427 |
" " |
| 1428 |
) |
| 1429 |
# fmt: on |
| 1430 |
|
| 1431 |
events = code_to_events(code) |
| 1432 |
reader = self.prepare_reader(events) |
| 1433 |
output = multiline_input(reader) |
| 1434 |
self.assertEqual(output, expected) |
| 1435 |
|
| 1436 |
def test_paste_not_in_paste_mode(self): |
| 1437 |
# fmt: off |
| 1438 |
input_code = ( |
| 1439 |
"def a():\n" |
| 1440 |
"for x in range(10):\n" |
| 1441 |
"if x%2:\n" |
| 1442 |
"print(x)\n" |
| 1443 |
"else:\n" |
| 1444 |
"pass\n\n" |
| 1445 |
) |
| 1446 |
|
| 1447 |
output_code = ( |
| 1448 |
"def a():\n" |
| 1449 |
" for x in range(10):\n" |
| 1450 |
" if x%2:\n" |
| 1451 |
" print(x)\n" |
| 1452 |
" else:" |
| 1453 |
) |
| 1454 |
# fmt: on |
| 1455 |
|
| 1456 |
events = code_to_events(input_code) |
| 1457 |
reader = self.prepare_reader(events) |
| 1458 |
output = multiline_input(reader) |
| 1459 |
self.assertEqual(output, output_code) |
| 1460 |
|
| 1461 |
def test_bracketed_paste(self): |
| 1462 |
"""Test that bracketed paste using \x1b[200~ and \x1b[201~ works.""" |
| 1463 |
# fmt: off |
| 1464 |
input_code = ( |
| 1465 |
"def a():\n" |
| 1466 |
" for x in range(10):\n" |
| 1467 |
"\n" |
| 1468 |
" if x%2:\n" |
| 1469 |
" print(x)\n" |
| 1470 |
"\n" |
| 1471 |
" else:\n" |
| 1472 |
" pass\n" |
| 1473 |
) |
| 1474 |
|
| 1475 |
output_code = ( |
| 1476 |
"def a():\n" |
| 1477 |
" for x in range(10):\n" |
| 1478 |
"\n" |
| 1479 |
" if x%2:\n" |
| 1480 |
" print(x)\n" |
| 1481 |
"\n" |
| 1482 |
" else:\n" |
| 1483 |
" pass\n" |
| 1484 |
) |
| 1485 |
# fmt: on |
| 1486 |
|
| 1487 |
paste_start = "\x1b[200~" |
| 1488 |
paste_end = "\x1b[201~" |
| 1489 |
|
| 1490 |
events = itertools.chain( |
| 1491 |
code_to_events(paste_start), |
| 1492 |
code_to_events(input_code), |
| 1493 |
code_to_events(paste_end), |
| 1494 |
code_to_events("\n"), |
| 1495 |
) |
| 1496 |
reader = self.prepare_reader(events) |
| 1497 |
output = multiline_input(reader) |
| 1498 |
self.assertEqual(output, output_code) |
| 1499 |
|
| 1500 |
def test_bracketed_paste_single_line(self): |
| 1501 |
input_code = "oneline" |
| 1502 |
|
| 1503 |
paste_start = "\x1b[200~" |
| 1504 |
paste_end = "\x1b[201~" |
| 1505 |
|
| 1506 |
events = itertools.chain( |
| 1507 |
code_to_events(paste_start), |
| 1508 |
code_to_events(input_code), |
| 1509 |
code_to_events(paste_end), |
| 1510 |
code_to_events("\n"), |
| 1511 |
) |
| 1512 |
reader = self.prepare_reader(events) |
| 1513 |
output = multiline_input(reader) |
| 1514 |
self.assertEqual(output, input_code) |
| 1515 |
|
| 1516 |
|
| 1517 |
@skipUnless(pty, "requires pty") |
| 1518 |
class TestDumbTerminal(ReplTestCase): |
| 1519 |
def test_dumb_terminal_exits_cleanly(self): |
| 1520 |
env = os.environ.copy() |
| 1521 |
env.pop('PYTHON_BASIC_REPL', None) |
| 1522 |
# Ignore PYTHONSTARTUP to not pollute the output |
| 1523 |
# with an unrelated traceback. See GH-137568. |
| 1524 |
env.pop('PYTHONSTARTUP', None) |
| 1525 |
env.update({"TERM": "dumb"}) |
| 1526 |
output, exit_code = self.run_repl("exit()\n", env=env) |
| 1527 |
self.assertEqual(exit_code, 0) |
| 1528 |
self.assertIn("warning: can't use pyrepl", output) |
| 1529 |
self.assertNotIn("Exception", output) |
| 1530 |
self.assertNotIn("Traceback", output) |
| 1531 |
|
| 1532 |
|
| 1533 |
@skipUnless(pty, "requires pty") |
| 1534 |
@skipIf((os.environ.get("TERM") or "dumb") == "dumb", "can't use pyrepl in dumb terminal") |
| 1535 |
class TestMain(ReplTestCase): |
| 1536 |
def setUp(self): |
| 1537 |
# Cleanup from PYTHON* variables to isolate from local |
| 1538 |
# user settings, see #121359. Such variables should be |
| 1539 |
# added later in test methods to patched os.environ. |
| 1540 |
super().setUp() |
| 1541 |
patcher = patch('os.environ', new=make_clean_env()) |
| 1542 |
self.addCleanup(patcher.stop) |
| 1543 |
patcher.start() |
| 1544 |
|
| 1545 |
@force_not_colorized |
| 1546 |
def test_exposed_globals_in_repl(self): |
| 1547 |
pre = "['__builtins__'" |
| 1548 |
post = "'__loader__', '__name__', '__package__', '__spec__']" |
| 1549 |
output, exit_code = self.run_repl(["sorted(dir())", "exit()"], skip=True) |
| 1550 |
self.assertEqual(exit_code, 0) |
| 1551 |
|
| 1552 |
# if `__main__` is not a file (impossible with pyrepl) |
| 1553 |
case1 = f"{pre}, '__doc__', {post}" in output |
| 1554 |
|
| 1555 |
# if `__main__` is an uncached .py file (no .pyc) |
| 1556 |
case2 = f"{pre}, '__doc__', '__file__', {post}" in output |
| 1557 |
|
| 1558 |
# if `__main__` is a cached .pyc file and the .py source exists |
| 1559 |
case3 = f"{pre}, '__cached__', '__doc__', '__file__', {post}" in output |
| 1560 |
|
| 1561 |
# if `__main__` is a cached .pyc file but there's no .py source file |
| 1562 |
case4 = f"{pre}, '__cached__', '__doc__', {post}" in output |
| 1563 |
|
| 1564 |
self.assertTrue(case1 or case2 or case3 or case4, output) |
| 1565 |
|
| 1566 |
def _assertMatchOK( |
| 1567 |
self, var: str, expected: str | re.Pattern, actual: str |
| 1568 |
) -> None: |
| 1569 |
if isinstance(expected, re.Pattern): |
| 1570 |
self.assertTrue( |
| 1571 |
expected.match(actual), |
| 1572 |
f"{var}={actual} does not match {expected.pattern}", |
| 1573 |
) |
| 1574 |
else: |
| 1575 |
self.assertEqual( |
| 1576 |
actual, |
| 1577 |
expected, |
| 1578 |
f"expected {var}={expected}, got {var}={actual}", |
| 1579 |
) |
| 1580 |
|
| 1581 |
@force_not_colorized |
| 1582 |
def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False, pythonstartup=False): |
| 1583 |
clean_env = make_clean_env() |
| 1584 |
clean_env["NO_COLOR"] = "1" # force_not_colorized doesn't touch subprocesses |
| 1585 |
|
| 1586 |
with tempfile.TemporaryDirectory() as td: |
| 1587 |
blue = pathlib.Path(td) / "blue" |
| 1588 |
blue.mkdir() |
| 1589 |
mod = blue / "calx.py" |
| 1590 |
mod.write_text("FOO = 42", encoding="utf-8") |
| 1591 |
startup = blue / "startup.py" |
| 1592 |
startup.write_text("BAR = 64", encoding="utf-8") |
| 1593 |
commands = [ |
| 1594 |
"print(f'^{" + var + "=}')" for var in expectations |
| 1595 |
] + ["exit()"] |
| 1596 |
if pythonstartup: |
| 1597 |
clean_env["PYTHONSTARTUP"] = str(startup) |
| 1598 |
if as_file and as_module: |
| 1599 |
self.fail("as_file and as_module are mutually exclusive") |
| 1600 |
elif as_file: |
| 1601 |
output, exit_code = self.run_repl( |
| 1602 |
commands, |
| 1603 |
cmdline_args=[str(mod)], |
| 1604 |
env=clean_env, |
| 1605 |
skip=True, |
| 1606 |
) |
| 1607 |
elif as_module: |
| 1608 |
output, exit_code = self.run_repl( |
| 1609 |
commands, |
| 1610 |
cmdline_args=["-m", "blue.calx"], |
| 1611 |
env=clean_env, |
| 1612 |
cwd=td, |
| 1613 |
skip=True, |
| 1614 |
) |
| 1615 |
else: |
| 1616 |
output, exit_code = self.run_repl( |
| 1617 |
commands, |
| 1618 |
cmdline_args=[], |
| 1619 |
env=clean_env, |
| 1620 |
cwd=td, |
| 1621 |
skip=True, |
| 1622 |
) |
| 1623 |
|
| 1624 |
self.assertEqual(exit_code, 0) |
| 1625 |
for var, expected in expectations.items(): |
| 1626 |
with self.subTest(var=var, expected=expected): |
| 1627 |
if m := re.search(rf"\^{var}=(.+?)[\r\n]", output): |
| 1628 |
self._assertMatchOK(var, expected, actual=m.group(1)) |
| 1629 |
else: |
| 1630 |
self.fail(f"{var}= not found in output: {output!r}\n\n{output}") |
| 1631 |
|
| 1632 |
self.assertNotIn("Exception", output) |
| 1633 |
self.assertNotIn("Traceback", output) |
| 1634 |
|
| 1635 |
def test_globals_initialized_as_default(self): |
| 1636 |
expectations = { |
| 1637 |
"__name__": "'__main__'", |
| 1638 |
"__package__": "None", |
| 1639 |
# "__file__" is missing in -i, like in the basic REPL |
| 1640 |
} |
| 1641 |
self._run_repl_globals_test(expectations) |
| 1642 |
|
| 1643 |
def test_globals_initialized_from_pythonstartup(self): |
| 1644 |
expectations = { |
| 1645 |
"BAR": "64", |
| 1646 |
"__name__": "'__main__'", |
| 1647 |
"__package__": "None", |
| 1648 |
# "__file__" is missing in -i, like in the basic REPL |
| 1649 |
} |
| 1650 |
self._run_repl_globals_test(expectations, pythonstartup=True) |
| 1651 |
|
| 1652 |
def test_inspect_keeps_globals_from_inspected_file(self): |
| 1653 |
expectations = { |
| 1654 |
"FOO": "42", |
| 1655 |
"__name__": "'__main__'", |
| 1656 |
"__package__": "None", |
| 1657 |
# "__file__" is missing in -i, like in the basic REPL |
| 1658 |
} |
| 1659 |
self._run_repl_globals_test(expectations, as_file=True) |
| 1660 |
|
| 1661 |
def test_inspect_keeps_globals_from_inspected_file_with_pythonstartup(self): |
| 1662 |
expectations = { |
| 1663 |
"FOO": "42", |
| 1664 |
"BAR": "64", |
| 1665 |
"__name__": "'__main__'", |
| 1666 |
"__package__": "None", |
| 1667 |
# "__file__" is missing in -i, like in the basic REPL |
| 1668 |
} |
| 1669 |
self._run_repl_globals_test(expectations, as_file=True, pythonstartup=True) |
| 1670 |
|
| 1671 |
def test_inspect_keeps_globals_from_inspected_module(self): |
| 1672 |
expectations = { |
| 1673 |
"FOO": "42", |
| 1674 |
"__name__": "'__main__'", |
| 1675 |
"__package__": "'blue'", |
| 1676 |
"__file__": re.compile(r"^'.*calx.py'$"), |
| 1677 |
} |
| 1678 |
self._run_repl_globals_test(expectations, as_module=True) |
| 1679 |
|
| 1680 |
def test_inspect_keeps_globals_from_inspected_module_with_pythonstartup(self): |
| 1681 |
expectations = { |
| 1682 |
"FOO": "42", |
| 1683 |
"BAR": "64", |
| 1684 |
"__name__": "'__main__'", |
| 1685 |
"__package__": "'blue'", |
| 1686 |
"__file__": re.compile(r"^'.*calx.py'$"), |
| 1687 |
} |
| 1688 |
self._run_repl_globals_test(expectations, as_module=True, pythonstartup=True) |
| 1689 |
|
| 1690 |
@force_not_colorized |
| 1691 |
def test_python_basic_repl(self): |
| 1692 |
env = os.environ.copy() |
| 1693 |
pyrepl_commands = "clear\nexit()\n" |
| 1694 |
env.pop("PYTHON_BASIC_REPL", None) |
| 1695 |
output, exit_code = self.run_repl(pyrepl_commands, env=env, skip=True) |
| 1696 |
self.assertEqual(exit_code, 0) |
| 1697 |
self.assertNotIn("Exception", output) |
| 1698 |
self.assertNotIn("NameError", output) |
| 1699 |
self.assertNotIn("Traceback", output) |
| 1700 |
|
| 1701 |
basic_commands = "help\nexit()\n" |
| 1702 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1703 |
output, exit_code = self.run_repl(basic_commands, env=env) |
| 1704 |
self.assertEqual(exit_code, 0) |
| 1705 |
self.assertIn("Type help() for interactive help", output) |
| 1706 |
self.assertNotIn("Exception", output) |
| 1707 |
self.assertNotIn("Traceback", output) |
| 1708 |
|
| 1709 |
# The site module must not load _pyrepl if PYTHON_BASIC_REPL is set |
| 1710 |
commands = ("import sys\n" |
| 1711 |
"print('_pyrepl' in sys.modules)\n" |
| 1712 |
"exit()\n") |
| 1713 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1714 |
output, exit_code = self.run_repl(commands, env=env) |
| 1715 |
self.assertEqual(exit_code, 0) |
| 1716 |
self.assertIn("False", output) |
| 1717 |
self.assertNotIn("True", output) |
| 1718 |
self.assertNotIn("Exception", output) |
| 1719 |
self.assertNotIn("Traceback", output) |
| 1720 |
|
| 1721 |
@force_not_colorized |
| 1722 |
def test_no_pyrepl_source_in_exc(self): |
| 1723 |
# Avoid using _pyrepl/__main__.py in traceback reports |
| 1724 |
# See https://github.com/python/cpython/issues/129098. |
| 1725 |
pyrepl_main_file = os.path.join(STDLIB_DIR, "_pyrepl", "__main__.py") |
| 1726 |
self.assertTrue(os.path.exists(pyrepl_main_file), pyrepl_main_file) |
| 1727 |
with open(pyrepl_main_file) as fp: |
| 1728 |
excluded_lines = fp.readlines() |
| 1729 |
excluded_lines = list(filter(None, map(str.strip, excluded_lines))) |
| 1730 |
|
| 1731 |
for filename in ['?', 'unknown-filename', '<foo>', '<...>']: |
| 1732 |
self._test_no_pyrepl_source_in_exc(filename, excluded_lines) |
| 1733 |
|
| 1734 |
def _test_no_pyrepl_source_in_exc(self, filename, excluded_lines): |
| 1735 |
with EnvironmentVarGuard() as env, self.subTest(filename=filename): |
| 1736 |
env.unset("PYTHON_BASIC_REPL") |
| 1737 |
commands = (f"eval(compile('spam', {filename!r}, 'eval'))\n" |
| 1738 |
f"exit()\n") |
| 1739 |
output, _ = self.run_repl(commands, env=env) |
| 1740 |
self.assertIn("Traceback (most recent call last)", output) |
| 1741 |
self.assertIn("NameError: name 'spam' is not defined", output) |
| 1742 |
for line in excluded_lines: |
| 1743 |
with self.subTest(line=line): |
| 1744 |
self.assertNotIn(line, output) |
| 1745 |
|
| 1746 |
@force_not_colorized |
| 1747 |
def test_bad_sys_excepthook_doesnt_crash_pyrepl(self): |
| 1748 |
env = os.environ.copy() |
| 1749 |
commands = ("import sys\n" |
| 1750 |
"sys.excepthook = 1\n" |
| 1751 |
"1/0\n" |
| 1752 |
"exit()\n") |
| 1753 |
|
| 1754 |
def check(output, exitcode): |
| 1755 |
self.assertIn("Error in sys.excepthook:", output) |
| 1756 |
self.assertEqual(output.count("'int' object is not callable"), 1) |
| 1757 |
self.assertIn("Original exception was:", output) |
| 1758 |
self.assertIn("division by zero", output) |
| 1759 |
self.assertEqual(exitcode, 0) |
| 1760 |
env.pop("PYTHON_BASIC_REPL", None) |
| 1761 |
output, exit_code = self.run_repl(commands, env=env, skip=True) |
| 1762 |
check(output, exit_code) |
| 1763 |
|
| 1764 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1765 |
output, exit_code = self.run_repl(commands, env=env) |
| 1766 |
check(output, exit_code) |
| 1767 |
|
| 1768 |
def test_not_wiping_history_file(self): |
| 1769 |
# skip, if readline module is not available |
| 1770 |
import_module('readline') |
| 1771 |
|
| 1772 |
hfile = tempfile.NamedTemporaryFile(delete=False) |
| 1773 |
self.addCleanup(unlink, hfile.name) |
| 1774 |
env = os.environ.copy() |
| 1775 |
env["PYTHON_HISTORY"] = hfile.name |
| 1776 |
commands = "123\nspam\nexit()\n" |
| 1777 |
|
| 1778 |
env.pop("PYTHON_BASIC_REPL", None) |
| 1779 |
output, exit_code = self.run_repl(commands, env=env) |
| 1780 |
self.assertEqual(exit_code, 0) |
| 1781 |
self.assertIn("123", output) |
| 1782 |
self.assertIn("spam", output) |
| 1783 |
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0) |
| 1784 |
|
| 1785 |
hfile.file.truncate() |
| 1786 |
hfile.close() |
| 1787 |
|
| 1788 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1789 |
output, exit_code = self.run_repl(commands, env=env) |
| 1790 |
self.assertEqual(exit_code, 0) |
| 1791 |
self.assertIn("123", output) |
| 1792 |
self.assertIn("spam", output) |
| 1793 |
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0) |
| 1794 |
|
| 1795 |
@force_not_colorized |
| 1796 |
def test_correct_filename_in_syntaxerrors(self): |
| 1797 |
env = os.environ.copy() |
| 1798 |
commands = "a b c\nexit()\n" |
| 1799 |
output, exit_code = self.run_repl(commands, env=env, skip=True) |
| 1800 |
self.assertIn("SyntaxError: invalid syntax", output) |
| 1801 |
self.assertIn("<python-input-0>", output) |
| 1802 |
commands = " b\nexit()\n" |
| 1803 |
output, exit_code = self.run_repl(commands, env=env) |
| 1804 |
self.assertIn("IndentationError: unexpected indent", output) |
| 1805 |
self.assertIn("<python-input-0>", output) |
| 1806 |
|
| 1807 |
@force_not_colorized |
| 1808 |
def test_proper_tracebacklimit(self): |
| 1809 |
env = os.environ.copy() |
| 1810 |
for set_tracebacklimit in [True, False]: |
| 1811 |
commands = ("import sys\n" + |
| 1812 |
("sys.tracebacklimit = 1\n" if set_tracebacklimit else "") + |
| 1813 |
"def x1(): 1/0\n\n" |
| 1814 |
"def x2(): x1()\n\n" |
| 1815 |
"def x3(): x2()\n\n" |
| 1816 |
"x3()\n" |
| 1817 |
"exit()\n") |
| 1818 |
|
| 1819 |
for basic_repl in [True, False]: |
| 1820 |
if basic_repl: |
| 1821 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1822 |
else: |
| 1823 |
env.pop("PYTHON_BASIC_REPL", None) |
| 1824 |
with self.subTest(set_tracebacklimit=set_tracebacklimit, |
| 1825 |
basic_repl=basic_repl): |
| 1826 |
output, exit_code = self.run_repl(commands, env=env, skip=True) |
| 1827 |
self.assertIn("in x1", output) |
| 1828 |
if set_tracebacklimit: |
| 1829 |
self.assertNotIn("in x2", output) |
| 1830 |
self.assertNotIn("in x3", output) |
| 1831 |
self.assertNotIn("in <module>", output) |
| 1832 |
else: |
| 1833 |
self.assertIn("in x2", output) |
| 1834 |
self.assertIn("in x3", output) |
| 1835 |
self.assertIn("in <module>", output) |
| 1836 |
|
| 1837 |
def test_null_byte(self): |
| 1838 |
output, exit_code = self.run_repl("\x00\nexit()\n") |
| 1839 |
self.assertEqual(exit_code, 0) |
| 1840 |
self.assertNotIn("TypeError", output) |
| 1841 |
|
| 1842 |
@force_not_colorized |
| 1843 |
def test_non_string_suggestion_candidates(self): |
| 1844 |
commands = ("import runpy\n" |
| 1845 |
"runpy._run_module_code('blech', {0: '', 'bluch': ''}, '')\n" |
| 1846 |
"exit()\n") |
| 1847 |
|
| 1848 |
output, exit_code = self.run_repl(commands) |
| 1849 |
self.assertEqual(exit_code, 0) |
| 1850 |
self.assertNotIn("all elements in 'candidates' must be strings", output) |
| 1851 |
self.assertIn("bluch", output) |
| 1852 |
|
| 1853 |
def test_readline_history_file(self): |
| 1854 |
# skip, if readline module is not available |
| 1855 |
readline = import_module('readline') |
| 1856 |
if readline.backend != "editline": |
| 1857 |
self.skipTest("GNU readline is not affected by this issue") |
| 1858 |
|
| 1859 |
with tempfile.NamedTemporaryFile() as hfile: |
| 1860 |
env = os.environ.copy() |
| 1861 |
env["PYTHON_HISTORY"] = hfile.name |
| 1862 |
|
| 1863 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1864 |
output, exit_code = self.run_repl("spam \nexit()\n", env=env) |
| 1865 |
self.assertEqual(exit_code, 0) |
| 1866 |
self.assertIn("spam ", output) |
| 1867 |
self.assertNotEqual(pathlib.Path(hfile.name).stat().st_size, 0) |
| 1868 |
self.assertIn("spam\\040", pathlib.Path(hfile.name).read_text()) |
| 1869 |
|
| 1870 |
env.pop("PYTHON_BASIC_REPL", None) |
| 1871 |
output, exit_code = self.run_repl("exit\n", env=env) |
| 1872 |
self.assertEqual(exit_code, 0) |
| 1873 |
self.assertNotIn("\\040", pathlib.Path(hfile.name).read_text()) |
| 1874 |
|
| 1875 |
def test_history_survive_crash(self): |
| 1876 |
env = os.environ.copy() |
| 1877 |
|
| 1878 |
with tempfile.NamedTemporaryFile() as hfile: |
| 1879 |
env["PYTHON_HISTORY"] = hfile.name |
| 1880 |
|
| 1881 |
commands = "1\n2\n3\nexit()\n" |
| 1882 |
output, exit_code = self.run_repl(commands, env=env, skip=True) |
| 1883 |
self.assertEqual(exit_code, 0) |
| 1884 |
|
| 1885 |
# Run until "0xcafe" is printed (as "51966") and then kill the |
| 1886 |
# process to simulate a crash. Note that the output also includes |
| 1887 |
# the echoed input commands. |
| 1888 |
commands = "spam\nimport time\n0xcafe\ntime.sleep(1000)\nquit\n" |
| 1889 |
output, exit_code = self.run_repl(commands, env=env, |
| 1890 |
exit_on_output="51966") |
| 1891 |
self.assertNotEqual(exit_code, 0) |
| 1892 |
|
| 1893 |
history = pathlib.Path(hfile.name).read_text() |
| 1894 |
self.assertIn("2", history) |
| 1895 |
self.assertIn("exit()", history) |
| 1896 |
self.assertIn("spam", history) |
| 1897 |
self.assertIn("import time", history) |
| 1898 |
# History is written after each command's output is printed to the |
| 1899 |
# console, so depending on how quickly the process is killed, |
| 1900 |
# the last command may or may not be written to the history file. |
| 1901 |
self.assertNotIn("sleep", history) |
| 1902 |
self.assertNotIn("quit", history) |
| 1903 |
|
| 1904 |
def test_keyboard_interrupt_after_isearch(self): |
| 1905 |
output, exit_code = self.run_repl("\x12\x03exit\n") |
| 1906 |
self.assertEqual(exit_code, 0) |
| 1907 |
|
| 1908 |
def test_prompt_after_help(self): |
| 1909 |
output, exit_code = self.run_repl(["help", "q", "exit"]) |
| 1910 |
|
| 1911 |
# Regex pattern to remove ANSI escape sequences |
| 1912 |
ansi_escape = re.compile(r"(\x1B(=|>|(\[)[0-?]*[ -\/]*[@-~]))") |
| 1913 |
cleaned_output = ansi_escape.sub("", output) |
| 1914 |
self.assertEqual(exit_code, 0) |
| 1915 |
|
| 1916 |
# Ensure that we don't see multiple prompts after exiting `help` |
| 1917 |
# Extra stuff (newline and `exit` rewrites) are necessary |
| 1918 |
# because of how run_repl works. |
| 1919 |
self.assertNotIn(">>> \n>>> >>>", cleaned_output) |
| 1920 |
|
| 1921 |
@skipUnless(Py_DEBUG, '-X showrefcount requires a Python debug build') |
| 1922 |
def test_showrefcount(self): |
| 1923 |
env = os.environ.copy() |
| 1924 |
env.pop("PYTHON_BASIC_REPL", "") |
| 1925 |
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env) |
| 1926 |
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output) |
| 1927 |
self.assertEqual(len(matches), 3) |
| 1928 |
|
| 1929 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1930 |
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env) |
| 1931 |
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output) |
| 1932 |
self.assertEqual(len(matches), 3) |
| 1933 |
|
| 1934 |
|
| 1935 |
@force_not_colorized |
| 1936 |
def test_no_newline(self): |
| 1937 |
env = os.environ.copy() |
| 1938 |
env.pop("PYTHON_BASIC_REPL", "") |
| 1939 |
env["PYTHON_BASIC_REPL"] = "1" |
| 1940 |
|
| 1941 |
commands = "print('Something pretty long', end='')\nexit()\n" |
| 1942 |
expected_output_sequence = "Something pretty long>>> exit()" |
| 1943 |
|
| 1944 |
# gh-143394: The basic REPL needs the readline module to turn off |
| 1945 |
# ECHO terminal attribute. |
| 1946 |
if readline_module is not None: |
| 1947 |
basic_output, basic_exit_code = self.run_repl(commands, env=env) |
| 1948 |
self.assertEqual(basic_exit_code, 0) |
| 1949 |
self.assertIn(expected_output_sequence, basic_output) |
| 1950 |
|
| 1951 |
output, exit_code = self.run_repl(commands) |
| 1952 |
self.assertEqual(exit_code, 0) |
| 1953 |
|
| 1954 |
# Build patterns for escape sequences that don't affect cursor position |
| 1955 |
# or visual output. Use terminfo to get platform-specific sequences, |
| 1956 |
# falling back to hard-coded patterns for capabilities not in terminfo. |
| 1957 |
from _pyrepl.terminfo import TermInfo |
| 1958 |
ti = TermInfo(os.environ.get("TERM", "")) |
| 1959 |
|
| 1960 |
safe_patterns = [] |
| 1961 |
|
| 1962 |
# smkx/rmkx - application cursor keys and keypad mode |
| 1963 |
smkx = ti.get("smkx") |
| 1964 |
rmkx = ti.get("rmkx") |
| 1965 |
if smkx: |
| 1966 |
safe_patterns.append(re.escape(smkx.decode("ascii"))) |
| 1967 |
if rmkx: |
| 1968 |
safe_patterns.append(re.escape(rmkx.decode("ascii"))) |
| 1969 |
if not smkx and not rmkx: |
| 1970 |
safe_patterns.append(r'\x1b\[\?1[hl]') # application cursor keys |
| 1971 |
safe_patterns.append(r'\x1b[=>]') # application keypad mode |
| 1972 |
|
| 1973 |
# ich1 - insert character (only safe form that inserts exactly 1 char) |
| 1974 |
ich1 = ti.get("ich1") |
| 1975 |
if ich1: |
| 1976 |
safe_patterns.append(re.escape(ich1.decode("ascii")) + r'(?=[ -~])') |
| 1977 |
else: |
| 1978 |
safe_patterns.append(r'\x1b\[(?:1)?@(?=[ -~])') |
| 1979 |
|
| 1980 |
# civis/cnorm - cursor visibility (may include cursor blinking control) |
| 1981 |
civis = ti.get("civis") |
| 1982 |
cnorm = ti.get("cnorm") |
| 1983 |
if civis: |
| 1984 |
safe_patterns.append(re.escape(civis.decode("ascii"))) |
| 1985 |
if cnorm: |
| 1986 |
safe_patterns.append(re.escape(cnorm.decode("ascii"))) |
| 1987 |
if not civis and not cnorm: |
| 1988 |
safe_patterns.append(r'\x1b\[\?25[hl]') # cursor visibility |
| 1989 |
safe_patterns.append(r'\x1b\[\?12[hl]') # cursor blinking |
| 1990 |
|
| 1991 |
# rmam / smam - automatic margins |
| 1992 |
rmam = ti.get("rmam") |
| 1993 |
smam = ti.get("smam") |
| 1994 |
if rmam: |
| 1995 |
safe_patterns.append(re.escape(rmam.decode("ascii"))) |
| 1996 |
if smam: |
| 1997 |
safe_patterns.append(re.escape(smam.decode("ascii"))) |
| 1998 |
if not rmam and not smam: |
| 1999 |
safe_patterns.append(r'\x1b\[\?7l') # turn off automatic margins |
| 2000 |
safe_patterns.append(r'\x1b\[\?7h') # turn on automatic margins |
| 2001 |
|
| 2002 |
# Modern extensions not in standard terminfo - always use patterns |
| 2003 |
safe_patterns.append(r'\x1b\[\?2004[hl]') # bracketed paste mode |
| 2004 |
safe_patterns.append(r'\x1b\[\?12[hl]') # cursor blinking (may be separate) |
| 2005 |
safe_patterns.append(r'\x1b\[\?[01]c') # device attributes |
| 2006 |
|
| 2007 |
safe_escapes = re.compile('|'.join(safe_patterns)) |
| 2008 |
cleaned_output = safe_escapes.sub('', output) |
| 2009 |
self.assertIn(expected_output_sequence, cleaned_output) |
| 2010 |
|
| 2011 |
|
| 2012 |
@skipUnless(sys.platform == "darwin", "macOS only") |
| 2013 |
class TestMainAppleTerminal(TestMain): |
| 2014 |
"""Test the REPL with Apple Terminal's TERM_PROGRAM set.""" |
| 2015 |
|
| 2016 |
def run_repl(self, repl_input, env=None, **kwargs): |
| 2017 |
if env is None: |
| 2018 |
env = os.environ.copy() |
| 2019 |
env["TERM_PROGRAM"] = "Apple_Terminal" |
| 2020 |
return super().run_repl(repl_input, env=env, **kwargs) |
| 2021 |
|
| 2022 |
|
| 2023 |
class TestPyReplCtrlD(TestCase): |
| 2024 |
"""Test Ctrl+D behavior in _pyrepl to match old pre-3.13 REPL behavior. |
| 2025 |
|
| 2026 |
Ctrl+D should: |
| 2027 |
- Exit on empty buffer (raises EOFError) |
| 2028 |
- Delete character when cursor is in middle of line |
| 2029 |
- Perform no operation when cursor is at end of line without newline |
| 2030 |
- Exit multiline mode when cursor is at end with trailing newline |
| 2031 |
- Run code up to that point when pressed on blank line with preceding lines |
| 2032 |
""" |
| 2033 |
def prepare_reader(self, events): |
| 2034 |
console = FakeConsole(events) |
| 2035 |
config = ReadlineConfig(readline_completer=None) |
| 2036 |
reader = ReadlineAlikeReader(console=console, config=config) |
| 2037 |
return reader |
| 2038 |
|
| 2039 |
def test_ctrl_d_empty_line(self): |
| 2040 |
"""Test that pressing Ctrl+D on empty line exits the program""" |
| 2041 |
events = [ |
| 2042 |
Event(evt="key", data="\x04", raw=bytearray(b"\x04")), # Ctrl+D |
| 2043 |
] |
| 2044 |
reader = self.prepare_reader(events) |
| 2045 |
with self.assertRaises(EOFError): |
| 2046 |
multiline_input(reader) |
| 2047 |
|
| 2048 |
def test_ctrl_d_multiline_with_new_line(self): |
| 2049 |
"""Test that pressing Ctrl+D in multiline mode with trailing newline exits multiline mode""" |
| 2050 |
events = itertools.chain( |
| 2051 |
code_to_events("def f():\n pass\n"), # Enter multiline mode with trailing newline |
| 2052 |
[ |
| 2053 |
Event(evt="key", data="\x04", raw=bytearray(b"\x04")), # Ctrl+D |
| 2054 |
], |
| 2055 |
) |
| 2056 |
reader, _ = handle_all_events(events) |
| 2057 |
self.assertTrue(reader.finished) |
| 2058 |
self.assertEqual("def f():\n pass\n", "".join(reader.buffer)) |
| 2059 |
|
| 2060 |
def test_ctrl_d_multiline_middle_of_line(self): |
| 2061 |
"""Test that pressing Ctrl+D in multiline mode with cursor in middle deletes character""" |
| 2062 |
events = itertools.chain( |
| 2063 |
code_to_events("def f():\n hello world"), # Enter multiline mode |
| 2064 |
[ |
| 2065 |
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")) |
| 2066 |
] * 5, # move cursor to 'w' in "world" |
| 2067 |
[ |
| 2068 |
Event(evt="key", data="\x04", raw=bytearray(b"\x04")) |
| 2069 |
], # Ctrl+D should delete 'w' |
| 2070 |
) |
| 2071 |
reader, _ = handle_all_events(events) |
| 2072 |
self.assertFalse(reader.finished) |
| 2073 |
self.assertEqual("def f():\n hello orld", "".join(reader.buffer)) |
| 2074 |
|
| 2075 |
def test_ctrl_d_multiline_end_of_line_no_newline(self): |
| 2076 |
"""Test that pressing Ctrl+D at end of line without newline performs no operation""" |
| 2077 |
events = itertools.chain( |
| 2078 |
code_to_events("def f():\n hello"), # Enter multiline mode, no trailing newline |
| 2079 |
[ |
| 2080 |
Event(evt="key", data="\x04", raw=bytearray(b"\x04")) |
| 2081 |
], # Ctrl+D should be no-op |
| 2082 |
) |
| 2083 |
reader, _ = handle_all_events(events) |
| 2084 |
self.assertFalse(reader.finished) |
| 2085 |
self.assertEqual("def f():\n hello", "".join(reader.buffer)) |
| 2086 |
|
| 2087 |
def test_ctrl_d_single_line_middle_of_line(self): |
| 2088 |
"""Test that pressing Ctrl+D in single line mode deletes current character""" |
| 2089 |
events = itertools.chain( |
| 2090 |
code_to_events("hello"), |
| 2091 |
[Event(evt="key", data="left", raw=bytearray(b"\x1bOD"))], # move left |
| 2092 |
[Event(evt="key", data="\x04", raw=bytearray(b"\x04"))], # Ctrl+D |
| 2093 |
) |
| 2094 |
reader, _ = handle_all_events(events) |
| 2095 |
self.assertEqual("hell", "".join(reader.buffer)) |
| 2096 |
|
| 2097 |
def test_ctrl_d_single_line_end_no_newline(self): |
| 2098 |
"""Test that pressing Ctrl+D at end of single line without newline does nothing""" |
| 2099 |
events = itertools.chain( |
| 2100 |
code_to_events("hello"), # cursor at end of line |
| 2101 |
[Event(evt="key", data="\x04", raw=bytearray(b"\x04"))], # Ctrl+D |
| 2102 |
) |
| 2103 |
reader, _ = handle_all_events(events) |
| 2104 |
self.assertEqual("hello", "".join(reader.buffer)) |
| 2105 |
|
| 2106 |
|
| 2107 |
@skipUnless(sys.platform == "win32", "windows console only") |
| 2108 |
class TestWindowsConsoleEolWrap(TestCase): |
| 2109 |
def _make_mock_console(self, width=80): |
| 2110 |
from _pyrepl import windows_console as wc |
| 2111 |
|
| 2112 |
console = object.__new__(wc.WindowsConsole) |
| 2113 |
|
| 2114 |
console.width = width |
| 2115 |
console.posxy = (0, 0) |
| 2116 |
console.screen = [""] |
| 2117 |
|
| 2118 |
console._hide_cursor = Mock() |
| 2119 |
console._show_cursor = Mock() |
| 2120 |
console._erase_to_end = Mock() |
| 2121 |
console._move_relative = Mock() |
| 2122 |
console.move_cursor = Mock() |
| 2123 |
console._WindowsConsole__write = Mock() |
| 2124 |
|
| 2125 |
return console, wc |
| 2126 |
|
| 2127 |
def test_short_line_sets_posxy_normally(self): |
| 2128 |
width = 10 |
| 2129 |
y = 3 |
| 2130 |
console, wc = self._make_mock_console(width=width) |
| 2131 |
old_line = "" |
| 2132 |
new_line = "a" * 3 |
| 2133 |
wc.WindowsConsole._WindowsConsole__write_changed_line( |
| 2134 |
console, y, old_line, new_line, 0 |
| 2135 |
) |
| 2136 |
self.assertEqual(console.posxy, (3, y)) |
| 2137 |
|
| 2138 |
def test_exact_width_line_does_not_wrap(self): |
| 2139 |
width = 10 |
| 2140 |
y = 3 |
| 2141 |
console, wc = self._make_mock_console(width=width) |
| 2142 |
old_line = "" |
| 2143 |
new_line = "a" * width |
| 2144 |
|
| 2145 |
wc.WindowsConsole._WindowsConsole__write_changed_line( |
| 2146 |
console, y, old_line, new_line, 0 |
| 2147 |
) |
| 2148 |
self.assertEqual(console.posxy, (width - 1, y)) |