| 1 |
"""Test the interactive interpreter.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import select |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
from contextlib import contextmanager |
| 9 |
from functools import partial |
| 10 |
from textwrap import dedent |
| 11 |
from test import support |
| 12 |
from test.support import ( |
| 13 |
cpython_only, |
| 14 |
has_subprocess_support, |
| 15 |
os_helper, |
| 16 |
SuppressCrashReport, |
| 17 |
SHORT_TIMEOUT, |
| 18 |
) |
| 19 |
from test.support.script_helper import kill_python |
| 20 |
from test.support.import_helper import import_module |
| 21 |
|
| 22 |
try: |
| 23 |
import pty |
| 24 |
except ImportError: |
| 25 |
pty = None |
| 26 |
|
| 27 |
|
| 28 |
if not has_subprocess_support: |
| 29 |
raise unittest.SkipTest("test module requires subprocess") |
| 30 |
|
| 31 |
|
| 32 |
def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, custom=False, isolated=True, **kw): |
| 33 |
"""Run the Python REPL with the given arguments. |
| 34 |
|
| 35 |
kw is extra keyword args to pass to subprocess.Popen. Returns a Popen |
| 36 |
object. |
| 37 |
""" |
| 38 |
|
| 39 |
# To run the REPL without using a terminal, spawn python with the command |
| 40 |
# line option '-i' and the process name set to '<stdin>'. |
| 41 |
# The directory of argv[0] must match the directory of the Python |
| 42 |
# executable for the Popen() call to python to succeed as the directory |
| 43 |
# path may be used by Py_GetPath() to build the default module search |
| 44 |
# path. |
| 45 |
stdin_fname = os.path.join(os.path.dirname(sys.executable), "<stdin>") |
| 46 |
cmd_line = [stdin_fname] |
| 47 |
# Isolated mode implies -EPs and ignores PYTHON* variables. |
| 48 |
if isolated: |
| 49 |
cmd_line.append('-I') |
| 50 |
# Don't re-run the built-in REPL from interactive mode |
| 51 |
# if we're testing a custom REPL (such as the asyncio REPL). |
| 52 |
if not custom: |
| 53 |
cmd_line.append('-i') |
| 54 |
cmd_line.extend(args) |
| 55 |
|
| 56 |
# Set TERM=vt100, for the rationale see the comments in spawn_python() of |
| 57 |
# test.support.script_helper. |
| 58 |
env = kw.setdefault('env', dict(os.environ)) |
| 59 |
env['TERM'] = 'vt100' |
| 60 |
return subprocess.Popen(cmd_line, |
| 61 |
executable=sys.executable, |
| 62 |
text=True, |
| 63 |
stdin=subprocess.PIPE, |
| 64 |
stdout=stdout, stderr=stderr, |
| 65 |
**kw) |
| 66 |
|
| 67 |
|
| 68 |
spawn_asyncio_repl = partial(spawn_repl, "-m", "asyncio", custom=True) |
| 69 |
|
| 70 |
|
| 71 |
@contextmanager |
| 72 |
def temp_pythonstartup(*, source: str, histfile: str = ".pythonhist"): |
| 73 |
"""Create environment variables for a PYTHONSTARTUP script in a temporary directory.""" |
| 74 |
with os_helper.temp_dir() as tmpdir: |
| 75 |
filename = os.path.join(tmpdir, "pythonstartup.py") |
| 76 |
with open(filename, "w") as f: |
| 77 |
f.write(source) |
| 78 |
yield { |
| 79 |
"PYTHONSTARTUP": filename, |
| 80 |
"PYTHON_HISTORY": os.path.join(tmpdir, histfile) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
def run_on_interactive_mode(source): |
| 85 |
"""Spawn a new Python interpreter, pass the given |
| 86 |
input source code from the stdin and return the |
| 87 |
result back. If the interpreter exits non-zero, it |
| 88 |
raises a ValueError.""" |
| 89 |
|
| 90 |
process = spawn_repl() |
| 91 |
process.stdin.write(source) |
| 92 |
output = kill_python(process) |
| 93 |
|
| 94 |
if process.returncode != 0: |
| 95 |
raise ValueError("Process didn't exit properly.") |
| 96 |
return output |
| 97 |
|
| 98 |
|
| 99 |
@support.force_not_colorized_test_class |
| 100 |
class TestInteractiveInterpreter(unittest.TestCase): |
| 101 |
|
| 102 |
@cpython_only |
| 103 |
# Python built with Py_TRACE_REFS fail with a fatal error in |
| 104 |
# _PyRefchain_Trace() on memory allocation error. |
| 105 |
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') |
| 106 |
def test_no_memory(self): |
| 107 |
import_module("_testcapi") |
| 108 |
# Issue #30696: Fix the interactive interpreter looping endlessly when |
| 109 |
# no memory. Check also that the fix does not break the interactive |
| 110 |
# loop when an exception is raised. |
| 111 |
user_input = """ |
| 112 |
import sys, _testcapi |
| 113 |
1/0 |
| 114 |
print('After the exception.') |
| 115 |
_testcapi.set_nomemory(0) |
| 116 |
sys.exit(0) |
| 117 |
""" |
| 118 |
user_input = dedent(user_input) |
| 119 |
p = spawn_repl() |
| 120 |
with SuppressCrashReport(): |
| 121 |
p.stdin.write(user_input) |
| 122 |
output = kill_python(p) |
| 123 |
self.assertIn('After the exception.', output) |
| 124 |
# Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr. |
| 125 |
self.assertIn(p.returncode, (1, 120)) |
| 126 |
|
| 127 |
@cpython_only |
| 128 |
def test_multiline_string_parsing(self): |
| 129 |
# bpo-39209: Multiline string tokens need to be handled in the tokenizer |
| 130 |
# in two places: the interactive path and the non-interactive path. |
| 131 |
user_input = '''\ |
| 132 |
x = """<?xml version="1.0" encoding="iso-8859-1"?> |
| 133 |
<test> |
| 134 |
<Users> |
| 135 |
<fun25> |
| 136 |
<limits> |
| 137 |
<total>0KiB</total> |
| 138 |
<kbps>0</kbps> |
| 139 |
<rps>1.3</rps> |
| 140 |
<connections>0</connections> |
| 141 |
</limits> |
| 142 |
<usages> |
| 143 |
<total>16738211KiB</total> |
| 144 |
<kbps>237.15</kbps> |
| 145 |
<rps>1.3</rps> |
| 146 |
<connections>0</connections> |
| 147 |
</usages> |
| 148 |
<time_to_refresh>never</time_to_refresh> |
| 149 |
<limit_exceeded_URL>none</limit_exceeded_URL> |
| 150 |
</fun25> |
| 151 |
</Users> |
| 152 |
</test>""" |
| 153 |
''' |
| 154 |
user_input = dedent(user_input) |
| 155 |
p = spawn_repl() |
| 156 |
p.stdin.write(user_input) |
| 157 |
output = kill_python(p) |
| 158 |
self.assertEqual(p.returncode, 0) |
| 159 |
|
| 160 |
@cpython_only |
| 161 |
def test_lexer_buffer_realloc_with_null_start(self): |
| 162 |
# gh-144759: NULL pointer arithmetic in the lexer when start and |
| 163 |
# multi_line_start are NULL (uninitialized in tok_mode_stack[0]) |
| 164 |
# and the lexer buffer is reallocated while parsing long input. |
| 165 |
long_value = "a" * 2000 |
| 166 |
user_input = dedent(f"""\ |
| 167 |
x = f'{{{long_value!r}}}' |
| 168 |
print(x) |
| 169 |
""") |
| 170 |
p = spawn_repl() |
| 171 |
p.stdin.write(user_input) |
| 172 |
output = kill_python(p) |
| 173 |
self.assertEqual(p.returncode, 0) |
| 174 |
self.assertIn(long_value, output) |
| 175 |
|
| 176 |
def test_close_stdin(self): |
| 177 |
user_input = dedent(''' |
| 178 |
import os |
| 179 |
print("before close") |
| 180 |
os.close(0) |
| 181 |
''') |
| 182 |
prepare_repl = dedent(''' |
| 183 |
from test.support import suppress_msvcrt_asserts |
| 184 |
suppress_msvcrt_asserts() |
| 185 |
''') |
| 186 |
process = spawn_repl('-c', prepare_repl) |
| 187 |
output = process.communicate(user_input)[0] |
| 188 |
self.assertEqual(process.returncode, 0) |
| 189 |
self.assertIn('before close', output) |
| 190 |
|
| 191 |
def test_interactive_traceback_reporting(self): |
| 192 |
user_input = "1 / 0 / 3 / 4" |
| 193 |
p = spawn_repl() |
| 194 |
p.stdin.write(user_input) |
| 195 |
output = kill_python(p) |
| 196 |
self.assertEqual(p.returncode, 0) |
| 197 |
|
| 198 |
traceback_lines = output.splitlines()[-6:-1] |
| 199 |
expected_lines = [ |
| 200 |
"Traceback (most recent call last):", |
| 201 |
" File \"<stdin>\", line 1, in <module>", |
| 202 |
" 1 / 0 / 3 / 4", |
| 203 |
" ~~^~~", |
| 204 |
"ZeroDivisionError: division by zero", |
| 205 |
] |
| 206 |
self.assertEqual(traceback_lines, expected_lines) |
| 207 |
|
| 208 |
def test_interactive_traceback_reporting_multiple_input(self): |
| 209 |
user_input1 = dedent(""" |
| 210 |
def foo(x): |
| 211 |
1 / x |
| 212 |
|
| 213 |
""") |
| 214 |
p = spawn_repl() |
| 215 |
p.stdin.write(user_input1) |
| 216 |
user_input2 = "foo(0)" |
| 217 |
p.stdin.write(user_input2) |
| 218 |
output = kill_python(p) |
| 219 |
self.assertEqual(p.returncode, 0) |
| 220 |
|
| 221 |
traceback_lines = output.splitlines()[-8:-1] |
| 222 |
expected_lines = [ |
| 223 |
' File "<stdin>", line 1, in <module>', |
| 224 |
' foo(0)', |
| 225 |
' ~~~^^^', |
| 226 |
' File "<stdin>", line 2, in foo', |
| 227 |
' 1 / x', |
| 228 |
' ~~^~~', |
| 229 |
'ZeroDivisionError: division by zero' |
| 230 |
] |
| 231 |
self.assertEqual(traceback_lines, expected_lines) |
| 232 |
|
| 233 |
def test_pythonstartup_error_reporting(self): |
| 234 |
# errors based on https://github.com/python/cpython/issues/137576 |
| 235 |
|
| 236 |
def make_repl(env): |
| 237 |
return subprocess.Popen( |
| 238 |
[os.path.join(os.path.dirname(sys.executable), '<stdin>'), "-i"], |
| 239 |
executable=sys.executable, |
| 240 |
text=True, |
| 241 |
stdin=subprocess.PIPE, |
| 242 |
stdout=subprocess.PIPE, |
| 243 |
stderr=subprocess.STDOUT, |
| 244 |
env=env, |
| 245 |
) |
| 246 |
|
| 247 |
# case 1: error in user input, but PYTHONSTARTUP is fine |
| 248 |
with os_helper.temp_dir() as tmpdir: |
| 249 |
script = os.path.join(tmpdir, "pythonstartup.py") |
| 250 |
with open(script, "w") as f: |
| 251 |
f.write("print('from pythonstartup')\n") |
| 252 |
|
| 253 |
env = os.environ.copy() |
| 254 |
env['PYTHONSTARTUP'] = script |
| 255 |
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist") |
| 256 |
p = make_repl(env) |
| 257 |
p.stdin.write("1/0") |
| 258 |
output = kill_python(p) |
| 259 |
expected = dedent(""" |
| 260 |
Traceback (most recent call last): |
| 261 |
File "<stdin>", line 1, in <module> |
| 262 |
1/0 |
| 263 |
~^~ |
| 264 |
ZeroDivisionError: division by zero |
| 265 |
""") |
| 266 |
self.assertIn("from pythonstartup", output) |
| 267 |
self.assertIn(expected, output) |
| 268 |
|
| 269 |
# case 2: error in PYTHONSTARTUP triggered by user input |
| 270 |
with os_helper.temp_dir() as tmpdir: |
| 271 |
script = os.path.join(tmpdir, "pythonstartup.py") |
| 272 |
with open(script, "w") as f: |
| 273 |
f.write("def foo():\n 1/0\n") |
| 274 |
|
| 275 |
env = os.environ.copy() |
| 276 |
env['PYTHONSTARTUP'] = script |
| 277 |
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist") |
| 278 |
p = make_repl(env) |
| 279 |
p.stdin.write('foo()') |
| 280 |
output = kill_python(p) |
| 281 |
expected = dedent(""" |
| 282 |
Traceback (most recent call last): |
| 283 |
File "<stdin>", line 1, in <module> |
| 284 |
foo() |
| 285 |
~~~^^ |
| 286 |
File "%s", line 2, in foo |
| 287 |
1/0 |
| 288 |
~^~ |
| 289 |
ZeroDivisionError: division by zero |
| 290 |
""") % script |
| 291 |
self.assertIn(expected, output) |
| 292 |
|
| 293 |
def test_runsource_show_syntax_error_location(self): |
| 294 |
user_input = dedent("""def f(x, x): ... |
| 295 |
""") |
| 296 |
p = spawn_repl() |
| 297 |
p.stdin.write(user_input) |
| 298 |
output = kill_python(p) |
| 299 |
expected_lines = [ |
| 300 |
' def f(x, x): ...', |
| 301 |
' ^', |
| 302 |
"SyntaxError: duplicate argument 'x' in function definition" |
| 303 |
] |
| 304 |
self.assertEqual(output.splitlines()[4:-1], expected_lines) |
| 305 |
|
| 306 |
def test_interactive_source_is_in_linecache(self): |
| 307 |
user_input = dedent(""" |
| 308 |
def foo(x): |
| 309 |
return x + 1 |
| 310 |
|
| 311 |
def bar(x): |
| 312 |
return foo(x) + 2 |
| 313 |
""") |
| 314 |
p = spawn_repl() |
| 315 |
p.stdin.write(user_input) |
| 316 |
user_input2 = dedent(""" |
| 317 |
import linecache |
| 318 |
print(linecache._interactive_cache[linecache._make_key(foo.__code__)]) |
| 319 |
""") |
| 320 |
p.stdin.write(user_input2) |
| 321 |
output = kill_python(p) |
| 322 |
self.assertEqual(p.returncode, 0) |
| 323 |
expected = "(30, None, [\'def foo(x):\\n\', \' return x + 1\\n\', \'\\n\'], \'<stdin>\')" |
| 324 |
self.assertIn(expected, output, expected) |
| 325 |
|
| 326 |
def test_asyncio_repl_reaches_python_startup_script(self): |
| 327 |
with os_helper.temp_dir() as tmpdir: |
| 328 |
script = os.path.join(tmpdir, "pythonstartup.py") |
| 329 |
with open(script, "w") as f: |
| 330 |
f.write("print('pythonstartup done!')\n") |
| 331 |
env = os.environ.copy() |
| 332 |
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history") |
| 333 |
env["PYTHONSTARTUP"] = script |
| 334 |
p = spawn_asyncio_repl(isolated=False, env=env) |
| 335 |
output = kill_python(p) |
| 336 |
self.assertEqual(p.returncode, 0) |
| 337 |
self.assertIn("pythonstartup done!", output) |
| 338 |
|
| 339 |
def test_asyncio_repl_respects_isolated_mode(self): |
| 340 |
with os_helper.temp_dir() as tmpdir: |
| 341 |
script = os.path.join(tmpdir, "pythonstartup.py") |
| 342 |
with open(script, "w") as f: |
| 343 |
f.write("print('should not print')\n") |
| 344 |
env = os.environ.copy() |
| 345 |
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history") |
| 346 |
env["PYTHONSTARTUP"] = script |
| 347 |
p = spawn_asyncio_repl(isolated=True, env=env) |
| 348 |
output = kill_python(p) |
| 349 |
self.assertEqual(p.returncode, 0) |
| 350 |
self.assertNotIn("should not print", output) |
| 351 |
|
| 352 |
@unittest.skipUnless(pty, "requires pty") |
| 353 |
def test_asyncio_repl_is_ok(self): |
| 354 |
m, s = pty.openpty() |
| 355 |
cmd = [sys.executable, "-I", "-m", "asyncio"] |
| 356 |
env = os.environ.copy() |
| 357 |
proc = subprocess.Popen( |
| 358 |
cmd, |
| 359 |
stdin=s, |
| 360 |
stdout=s, |
| 361 |
stderr=s, |
| 362 |
text=True, |
| 363 |
close_fds=True, |
| 364 |
env=env, |
| 365 |
) |
| 366 |
os.close(s) |
| 367 |
os.write(m, b"await asyncio.sleep(0)\n") |
| 368 |
os.write(m, b"exit()\n") |
| 369 |
output = [] |
| 370 |
while select.select([m], [], [], SHORT_TIMEOUT)[0]: |
| 371 |
try: |
| 372 |
data = os.read(m, 1024).decode("utf-8") |
| 373 |
if not data: |
| 374 |
break |
| 375 |
except OSError: |
| 376 |
break |
| 377 |
output.append(data) |
| 378 |
os.close(m) |
| 379 |
try: |
| 380 |
exit_code = proc.wait(timeout=SHORT_TIMEOUT) |
| 381 |
except subprocess.TimeoutExpired: |
| 382 |
proc.kill() |
| 383 |
exit_code = proc.wait() |
| 384 |
|
| 385 |
self.assertEqual(exit_code, 0, "".join(output)) |
| 386 |
|
| 387 |
|
| 388 |
@support.force_not_colorized_test_class |
| 389 |
class TestInteractiveModeSyntaxErrors(unittest.TestCase): |
| 390 |
|
| 391 |
def test_interactive_syntax_error_correct_line(self): |
| 392 |
output = run_on_interactive_mode(dedent("""\ |
| 393 |
def f(): |
| 394 |
print(0) |
| 395 |
return yield 42 |
| 396 |
""")) |
| 397 |
|
| 398 |
traceback_lines = output.splitlines()[-4:-1] |
| 399 |
expected_lines = [ |
| 400 |
' return yield 42', |
| 401 |
' ^^^^^', |
| 402 |
'SyntaxError: invalid syntax' |
| 403 |
] |
| 404 |
self.assertEqual(traceback_lines, expected_lines) |
| 405 |
|
| 406 |
|
| 407 |
class TestAsyncioREPL(unittest.TestCase): |
| 408 |
def test_multiple_statements_fail_early(self): |
| 409 |
user_input = "1 / 0; print(f'afterwards: {1+1}')" |
| 410 |
p = spawn_asyncio_repl() |
| 411 |
p.stdin.write(user_input) |
| 412 |
output = kill_python(p) |
| 413 |
self.assertIn("ZeroDivisionError", output) |
| 414 |
self.assertNotIn("afterwards: 2", output) |
| 415 |
|
| 416 |
def test_toplevel_contextvars_sync(self): |
| 417 |
user_input = dedent("""\ |
| 418 |
from contextvars import ContextVar |
| 419 |
var = ContextVar("var", default="failed") |
| 420 |
var.set("ok") |
| 421 |
""") |
| 422 |
p = spawn_asyncio_repl() |
| 423 |
p.stdin.write(user_input) |
| 424 |
user_input2 = dedent(""" |
| 425 |
print(f"toplevel contextvar test: {var.get()}") |
| 426 |
""") |
| 427 |
p.stdin.write(user_input2) |
| 428 |
output = kill_python(p) |
| 429 |
self.assertEqual(p.returncode, 0) |
| 430 |
expected = "toplevel contextvar test: ok" |
| 431 |
self.assertIn(expected, output, expected) |
| 432 |
|
| 433 |
def test_toplevel_contextvars_async(self): |
| 434 |
user_input = dedent("""\ |
| 435 |
from contextvars import ContextVar |
| 436 |
var = ContextVar('var', default='failed') |
| 437 |
""") |
| 438 |
p = spawn_asyncio_repl() |
| 439 |
p.stdin.write(user_input) |
| 440 |
user_input2 = "async def set_var(): var.set('ok')\n" |
| 441 |
p.stdin.write(user_input2) |
| 442 |
user_input3 = "await set_var()\n" |
| 443 |
p.stdin.write(user_input3) |
| 444 |
user_input4 = "print(f'toplevel contextvar test: {var.get()}')\n" |
| 445 |
p.stdin.write(user_input4) |
| 446 |
output = kill_python(p) |
| 447 |
self.assertEqual(p.returncode, 0) |
| 448 |
expected = "toplevel contextvar test: ok" |
| 449 |
self.assertIn(expected, output, expected) |
| 450 |
|
| 451 |
def test_quiet_mode(self): |
| 452 |
p = spawn_repl("-q", "-m", "asyncio", custom=True) |
| 453 |
output = kill_python(p) |
| 454 |
self.assertEqual(p.returncode, 0) |
| 455 |
self.assertEqual(output[:3], ">>>") |
| 456 |
|
| 457 |
@support.force_not_colorized |
| 458 |
@support.subTests( |
| 459 |
("startup_code", "expected_error"), |
| 460 |
[ |
| 461 |
("some invalid syntax\n", "SyntaxError: invalid syntax"), |
| 462 |
("1/0\n", "ZeroDivisionError: division by zero"), |
| 463 |
], |
| 464 |
) |
| 465 |
def test_pythonstartup_failure(self, startup_code, expected_error): |
| 466 |
startup_env = self.enterContext( |
| 467 |
temp_pythonstartup(source=startup_code, histfile=".asyncio_history")) |
| 468 |
|
| 469 |
p = spawn_repl( |
| 470 |
"-qm", "asyncio", |
| 471 |
env=os.environ | startup_env, |
| 472 |
isolated=False, |
| 473 |
custom=True) |
| 474 |
p.stdin.write("print('user code', 'executed')\n") |
| 475 |
output = kill_python(p) |
| 476 |
self.assertEqual(p.returncode, 0) |
| 477 |
|
| 478 |
tb_hint = f'File "{startup_env["PYTHONSTARTUP"]}", line 1' |
| 479 |
self.assertIn(tb_hint, output) |
| 480 |
self.assertIn(expected_error, output) |
| 481 |
|
| 482 |
self.assertIn("user code executed", output) |
| 483 |
|
| 484 |
|
| 485 |
if __name__ == "__main__": |
| 486 |
unittest.main() |