mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
* Preserve original main VM through C API execution
Run and finalize the original main VM while keeping the registered attachment template available without holding its mutex across Python callbacks.
Assisted-by: OpenAI Codex:model version unavailable
* Make unraisable reporting robust at hook boundaries
Keep the existing message API and traceback formatting while handling missing, disabled, audited and failing hooks without panicking.
Assisted-by: OpenAI Codex:model version unavailable
* Separate interactive warning collection from compilation
Retain ordinary warning behavior and typed module APIs while exposing record-local parser warnings and post-warning code generation. Add a source-only lazy future-import guard.
Assisted-by: OpenAI Codex:model version unavailable
* Evaluate native stdin with shared VM record semantics
Use one evaluator for basic REPL and root transports, preserve effective inspect snapshots and future flags, and carry owned host exit status through original-main finalization.
Assisted-by: OpenAI Codex:model version unavailable
* Refresh canonical REPL tests and retire fixed failures
Run update_lib migrate from pinned CPython 3.15.0, preserving three remaining platform/runtime markers and retiring the two traceback/startup failures verified on the native evaluator.
Assisted-by: OpenAI Codex:model version unavailable
* Keep the root compiler dependency for development targets
The native evaluator now compiles through the VM. Only root benchmarks and the parse_folder example use rustpython-compiler directly, so declare it as a dev dependency while preserving VM compiler features.
Assisted-by: OpenAI Codex:model version unavailable
* Validate explicit conditional failure marker retirement
Require exact selector and original decorator evidence for each conditional expected-failure marker retired by migrate. Preserve undeclared guards and reject missing, duplicate, modified or unconsumed declarations.
Assisted-by: OpenAI Codex:model version unavailable
* Refresh canonical stdin and exception audit tests
Migrate the pinned CPython 3.15.0 modules and their canonical helper fixtures through update_lib. Retire exactly four failure markers proven obsolete by hosted CI, preserving all other existing guards.
Assisted-by: OpenAI Codex:model version unavailable
* Synchronize runpy warning attribution and document remaining capabilities
Migrate whole canonical runpy.py to pass module names to compile. Use update_lib to annotate only reproduced missing-capability cases: absent _testcapi import-event tests, unsupported startup/import-time features, and the deferred frozen-loader module-attribution caller.
Assisted-by: OpenAI Codex:model version unavailable
* Propagate interrupted native stdin reads to signal dispatch
Preserve the shared input buffer and partial bytes while returning EINTR to the VM before retrying. Exercise the transport deterministically and verify a returning SIGINT handler while pipe stdin stays open.
Assisted-by: OpenAI Codex:model version unavailable
* Reject statements after a compound statement in single mode
single_mode_multiple_statements_error now reports "invalid syntax" at the
statement that follows a compound first statement, and interactive_input
runs it right after parsing as compile() does.
The lazy `from __future__` import check moved from lazy_future_import_error
into unsupported_grammar_error, so it applies to every compile mode.
Assisted-by: Claude Code:claude-opus-5-5
* Remove passing expected failure from test_runpy
test_run_module_filter_syntax_warnings_by_module passes.
Assisted-by: Claude Code:claude-opus-5-5
* Restructure stdin execution after Modules/main.c and pythonrun.c
- src/lib.rs follows pymain_run_python: pymain_run_stdin, pymain_repl,
pymain_run_startup, pymain_run_interactive_hook and pymain_run_pyrepl
return exit codes instead of raising SystemExit.
- VirtualMachine gains run_any_stdin (_PyRun_AnyFile),
run_interactive_loop (_PyRun_InteractiveLoop) and run_simple_stdin;
run_shell feeds rustyline lines into run_interactive_loop, and
sys._baserepl calls run_any_stdin.
- print_uncaught_exception (PyErr_Print) replaces
report_uncaught_exception, and handle_system_exit_and_keyboard_interrupt
replaces handle_reported_exception.
- An unhandled KeyboardInterrupt is recorded and the process exits
through SIGINT after finalization.
- Interpreter::run_main replaces run_with_exit_status and InterpreterExit;
rustpython_capi::run_main_interpreter takes an exit-code callback.
- config_set_inspect audits cpython.PyConfig_Set and rebuilds sys.flags
through set_flag_obj (_PySys_SetFlagObj); the inspect state is an
AtomicBool and dict replace_by_identity is removed.
- add_main_module (PyImport_AddModuleRef("__main__")) is used by the
interactive and simple-file paths.
- The interactive parser warning helpers take their state directly.
Assisted-by: Claude Code:claude-opus-5-5
* Exit through Py_Exit from PyErr_Print and run the basic REPL via stdin
- print_uncaught_exception (PyErr_Print) calls exit_process (Py_Exit) for a
SystemExit when inspect mode is off, instead of returning the exception.
run_interactive_loop no longer checks SystemExit itself and flushes after
printing an error; sys._baserepl returns None.
- exit_process finalizes through VirtualMachine::finalize_ex, which holds
the Py_FinalizeEx steps split out of Interpreter::finalize, and exits
through host_env::os::exit_with_cleanup (C exit()).
- The basic REPL on a terminal (PYTHON_BASIC_REPL, or _pyrepl missing)
runs run_any_stdin instead of the rustyline shell.
- run_shell saves the history after every line.
Assisted-by: Claude Code:claude-opus-5-5
* Display exceptions through PyErr_Display and track the main thread running __main__
- display_exception (PyErr_Display) and display_exception_to (_PyErr_Display)
try traceback._print_exception_bltin before the native printer and flush
the file. PyErr_Print's excepthook fallback, print_exception and
sys.excepthook use them. A missing sys.stderr is reported with
dump_object (PyObject_Dump); a None sys.stderr prints nothing.
- Paths gains program_name (argv[0]) from getpath. The "can't open file"
message uses it with strerror(errno) and goes to sys.stderr.
- running_main holds the ident of the thread running __main__.
pymain_run_python sets it around running the main code, Py_Exit clears it,
the fork child reinitializes it, and _interpreters.is_running reads it
instead of treating the main interpreter as always running.
- Interpreter::run_main clears the unhandled KeyboardInterrupt flag before
running and applies exit_sigint after finalizing; finalize no longer
does.
- run_interactive_loop returns a SystemExit raised outside inspect mode
again and no longer flushes after printing an error; run_shell saves the
history once after the loop.
- pymain_set_inspect reports its error through print_uncaught_exception.
Assisted-by: Claude Code:claude-opus-5-5
* Treat a parse error at the end of the source as incomplete input
A parser error that starts at the end of the source is reported as
_IncompleteInputError when incomplete input is allowed, so the
interactive loop and codeop keep reading after `try:` and its block or
after a decorator line.
Assisted-by: Claude Code:claude-opus-5-5
* Place syntax errors on tokens made up at the end of the input
A parse error on the NEWLINE, DEDENT or ENDMARKER the tokenizer makes up
at the end of the input now carries an EofToken: the number of lines
read and the last line. SyntaxError takes its lineno and text from it,
and its offset is that line's length with end_offset -1, or 0 for both
when the error is raised at the token itself (the generic syntax error
and a missing forced `:` or `(`). Exec input that does not end with a
newline is checked with one appended.
Assisted-by: Claude Code:claude-opus-5-5
* Slice the source with get when checking an indented block error at EOF
Assisted-by: Claude Code:claude-opus-5-5
* Rename the host exits after C exit() and fix wasi and Windows builds
- host_env::os::exit now calls the C exit() on every platform; on Windows
std::process::exit ends in ExitProcess, so it flushes the Rust stdout
and calls libc::exit there. The former exit, which calls _exit(), is
now exit_immediately, used by os._exit, faulthandler and
posixsubprocess.
- errno::strerror_string is available on wasi.
- PyGlobalState::reinit_running_main is built only where its fork caller
is.
Assisted-by: Claude Code:claude-opus-5-5
---------
Co-authored-by: Jeong YunWon <jeong@youknowone.org>
492 lines
17 KiB
Python
Vendored
492 lines
17 KiB
Python
Vendored
"""Test the interactive interpreter."""
|
|
|
|
import os
|
|
import select
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
from contextlib import contextmanager
|
|
from functools import partial
|
|
from textwrap import dedent
|
|
from test import support
|
|
from test.support import (
|
|
cpython_only,
|
|
has_subprocess_support,
|
|
os_helper,
|
|
SuppressCrashReport,
|
|
SHORT_TIMEOUT,
|
|
)
|
|
from test.support.script_helper import kill_python
|
|
|
|
try:
|
|
import pty
|
|
except ImportError:
|
|
pty = None
|
|
|
|
|
|
if not has_subprocess_support:
|
|
raise unittest.SkipTest("test module requires subprocess")
|
|
|
|
|
|
def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, custom=False, isolated=True, **kw):
|
|
"""Run the Python REPL with the given arguments.
|
|
|
|
kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
|
|
object.
|
|
"""
|
|
|
|
# To run the REPL without using a terminal, spawn python with the command
|
|
# line option '-i' and the process name set to '<stdin>'.
|
|
# The directory of argv[0] must match the directory of the Python
|
|
# executable for the Popen() call to python to succeed as the directory
|
|
# path may be used by PyConfig_Get("module_search_paths") to build the
|
|
# default module search path.
|
|
stdin_fname = os.path.join(os.path.dirname(sys.executable), "<stdin>")
|
|
cmd_line = [stdin_fname]
|
|
# Isolated mode implies -EPs and ignores PYTHON* variables.
|
|
if isolated:
|
|
cmd_line.append('-I')
|
|
# Don't re-run the built-in REPL from interactive mode
|
|
# if we're testing a custom REPL (such as the asyncio REPL).
|
|
if not custom:
|
|
cmd_line.append('-i')
|
|
cmd_line.extend(args)
|
|
|
|
# Set TERM=vt100, for the rationale see the comments in spawn_python() of
|
|
# test.support.script_helper.
|
|
env = kw.setdefault('env', dict(os.environ))
|
|
env['TERM'] = 'vt100'
|
|
return subprocess.Popen(cmd_line,
|
|
executable=sys.executable,
|
|
text=True,
|
|
stdin=subprocess.PIPE,
|
|
stdout=stdout, stderr=stderr,
|
|
**kw)
|
|
|
|
|
|
spawn_asyncio_repl = partial(spawn_repl, "-m", "asyncio", custom=True)
|
|
|
|
|
|
@contextmanager
|
|
def temp_pythonstartup(*, source: str, histfile: str = ".pythonhist"):
|
|
"""Create environment variables for a PYTHONSTARTUP script in a temporary directory."""
|
|
with os_helper.temp_dir() as tmpdir:
|
|
filename = os.path.join(tmpdir, "pythonstartup.py")
|
|
with open(filename, "w") as f:
|
|
f.write(source)
|
|
yield {
|
|
"PYTHONSTARTUP": filename,
|
|
"PYTHON_HISTORY": os.path.join(tmpdir, histfile)
|
|
}
|
|
|
|
|
|
def run_on_interactive_mode(source):
|
|
"""Spawn a new Python interpreter, pass the given
|
|
input source code from the stdin and return the
|
|
result back. If the interpreter exits non-zero, it
|
|
raises a ValueError."""
|
|
|
|
process = spawn_repl()
|
|
process.stdin.write(source)
|
|
output = kill_python(process)
|
|
|
|
if process.returncode != 0:
|
|
raise ValueError("Process didn't exit properly.")
|
|
return output
|
|
|
|
|
|
@support.force_not_colorized_test_class
|
|
class TestInteractiveInterpreter(unittest.TestCase):
|
|
|
|
@support.nomemtest
|
|
def test_no_memory(self):
|
|
# Issue #30696: Fix the interactive interpreter looping endlessly when
|
|
# no memory. Check also that the fix does not break the interactive
|
|
# loop when an exception is raised.
|
|
user_input = """
|
|
import sys, _testcapi
|
|
1/0
|
|
print('After the exception.')
|
|
_testcapi.set_nomemory(0)
|
|
sys.exit(0)
|
|
"""
|
|
user_input = dedent(user_input)
|
|
p = spawn_repl()
|
|
with SuppressCrashReport():
|
|
p.stdin.write(user_input)
|
|
output = kill_python(p)
|
|
self.assertIn('After the exception.', output)
|
|
# Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr.
|
|
self.assertIn(p.returncode, (1, 120))
|
|
|
|
@cpython_only
|
|
def test_multiline_string_parsing(self):
|
|
# bpo-39209: Multiline string tokens need to be handled in the tokenizer
|
|
# in two places: the interactive path and the non-interactive path.
|
|
user_input = '''\
|
|
x = """<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<test>
|
|
<Users>
|
|
<fun25>
|
|
<limits>
|
|
<total>0KiB</total>
|
|
<kbps>0</kbps>
|
|
<rps>1.3</rps>
|
|
<connections>0</connections>
|
|
</limits>
|
|
<usages>
|
|
<total>16738211KiB</total>
|
|
<kbps>237.15</kbps>
|
|
<rps>1.3</rps>
|
|
<connections>0</connections>
|
|
</usages>
|
|
<time_to_refresh>never</time_to_refresh>
|
|
<limit_exceeded_URL>none</limit_exceeded_URL>
|
|
</fun25>
|
|
</Users>
|
|
</test>"""
|
|
'''
|
|
user_input = dedent(user_input)
|
|
p = spawn_repl()
|
|
p.stdin.write(user_input)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
|
|
@cpython_only
|
|
def test_lexer_buffer_realloc_with_null_start(self):
|
|
# gh-144759: NULL pointer arithmetic in the lexer when start and
|
|
# multi_line_start are NULL (uninitialized in tok_mode_stack[0])
|
|
# and the lexer buffer is reallocated while parsing long input.
|
|
long_value = "a" * 2000
|
|
user_input = dedent(f"""\
|
|
x = f'{{{long_value!r}}}'
|
|
print(x)
|
|
""")
|
|
p = spawn_repl()
|
|
p.stdin.write(user_input)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
self.assertIn(long_value, output)
|
|
|
|
@unittest.expectedFailureIf(sys.platform == "android", "TODO: RUSTPYTHON; AssertionError: 101 != 0")
|
|
def test_close_stdin(self):
|
|
user_input = dedent('''
|
|
import os
|
|
print("before close")
|
|
os.close(0)
|
|
''')
|
|
prepare_repl = dedent('''
|
|
from test.support import suppress_msvcrt_asserts
|
|
suppress_msvcrt_asserts()
|
|
''')
|
|
process = spawn_repl('-c', prepare_repl)
|
|
output = process.communicate(user_input)[0]
|
|
self.assertEqual(process.returncode, 0)
|
|
self.assertIn('before close', output)
|
|
|
|
def test_interactive_traceback_reporting(self):
|
|
user_input = "1 / 0 / 3 / 4"
|
|
p = spawn_repl()
|
|
p.stdin.write(user_input)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
|
|
traceback_lines = output.splitlines()[-6:-1]
|
|
expected_lines = [
|
|
"Traceback (most recent call last):",
|
|
" File \"<stdin>\", line 1, in <module>",
|
|
" 1 / 0 / 3 / 4",
|
|
" ~~^~~",
|
|
"ZeroDivisionError: division by zero",
|
|
]
|
|
self.assertEqual(traceback_lines, expected_lines)
|
|
|
|
def test_interactive_traceback_reporting_multiple_input(self):
|
|
user_input1 = dedent("""
|
|
def foo(x):
|
|
1 / x
|
|
|
|
""")
|
|
p = spawn_repl()
|
|
p.stdin.write(user_input1)
|
|
user_input2 = "foo(0)"
|
|
p.stdin.write(user_input2)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
|
|
traceback_lines = output.splitlines()[-8:-1]
|
|
expected_lines = [
|
|
' File "<stdin>", line 1, in <module>',
|
|
' foo(0)',
|
|
' ~~~^^^',
|
|
' File "<stdin>", line 2, in foo',
|
|
' 1 / x',
|
|
' ~~^~~',
|
|
'ZeroDivisionError: division by zero'
|
|
]
|
|
self.assertEqual(traceback_lines, expected_lines)
|
|
|
|
def test_pythonstartup_error_reporting(self):
|
|
# errors based on https://github.com/python/cpython/issues/137576
|
|
|
|
def make_repl(env):
|
|
return subprocess.Popen(
|
|
[os.path.join(os.path.dirname(sys.executable), '<stdin>'), "-i"],
|
|
executable=sys.executable,
|
|
text=True,
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
env=env,
|
|
)
|
|
|
|
# case 1: error in user input, but PYTHONSTARTUP is fine
|
|
with os_helper.temp_dir() as tmpdir:
|
|
script = os.path.join(tmpdir, "pythonstartup.py")
|
|
with open(script, "w") as f:
|
|
f.write("print('from pythonstartup')\n")
|
|
|
|
env = os.environ.copy()
|
|
env['PYTHONSTARTUP'] = script
|
|
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist")
|
|
p = make_repl(env)
|
|
p.stdin.write("1/0")
|
|
output = kill_python(p)
|
|
expected = dedent("""
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in <module>
|
|
1/0
|
|
~^~
|
|
ZeroDivisionError: division by zero
|
|
""")
|
|
self.assertIn("from pythonstartup", output)
|
|
self.assertIn(expected, output)
|
|
|
|
# case 2: error in PYTHONSTARTUP triggered by user input
|
|
with os_helper.temp_dir() as tmpdir:
|
|
script = os.path.join(tmpdir, "pythonstartup.py")
|
|
with open(script, "w") as f:
|
|
f.write("def foo():\n 1/0\n")
|
|
|
|
env = os.environ.copy()
|
|
env['PYTHONSTARTUP'] = script
|
|
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".pythonhist")
|
|
p = make_repl(env)
|
|
p.stdin.write('foo()')
|
|
output = kill_python(p)
|
|
expected = dedent("""
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in <module>
|
|
foo()
|
|
~~~^^
|
|
File "%s", line 2, in foo
|
|
1/0
|
|
~^~
|
|
ZeroDivisionError: division by zero
|
|
""") % script
|
|
self.assertIn(expected, output)
|
|
|
|
@unittest.expectedFailure # TODO: RUSTPYTHON
|
|
def test_runsource_show_syntax_error_location(self):
|
|
user_input = dedent("""def f(x, x): ...
|
|
""")
|
|
p = spawn_repl()
|
|
p.stdin.write(user_input)
|
|
output = kill_python(p)
|
|
expected_lines = [
|
|
' def f(x, x): ...',
|
|
' ^',
|
|
"SyntaxError: duplicate parameter 'x' in function definition"
|
|
]
|
|
self.assertEqual(output.splitlines()[4:-1], expected_lines)
|
|
|
|
def test_interactive_source_is_in_linecache(self):
|
|
user_input = dedent("""
|
|
def foo(x):
|
|
return x + 1
|
|
|
|
def bar(x):
|
|
return foo(x) + 2
|
|
""")
|
|
p = spawn_repl()
|
|
p.stdin.write(user_input)
|
|
user_input2 = dedent("""
|
|
import linecache
|
|
print(linecache._interactive_cache[linecache._make_key(foo.__code__)])
|
|
""")
|
|
p.stdin.write(user_input2)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
expected = "(30, None, [\'def foo(x):\\n\', \' return x + 1\\n\', \'\\n\'], \'<stdin>\')"
|
|
self.assertIn(expected, output, expected)
|
|
|
|
def test_asyncio_repl_reaches_python_startup_script(self):
|
|
with os_helper.temp_dir() as tmpdir:
|
|
script = os.path.join(tmpdir, "pythonstartup.py")
|
|
with open(script, "w") as f:
|
|
f.write("print('pythonstartup done!')\n")
|
|
env = os.environ.copy()
|
|
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history")
|
|
env["PYTHONSTARTUP"] = script
|
|
p = spawn_asyncio_repl(isolated=False, env=env)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
self.assertIn("pythonstartup done!", output)
|
|
|
|
def test_asyncio_repl_respects_isolated_mode(self):
|
|
with os_helper.temp_dir() as tmpdir:
|
|
script = os.path.join(tmpdir, "pythonstartup.py")
|
|
with open(script, "w") as f:
|
|
f.write("print('should not print')\n")
|
|
env = os.environ.copy()
|
|
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history")
|
|
env["PYTHONSTARTUP"] = script
|
|
p = spawn_asyncio_repl(isolated=True, env=env)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
self.assertNotIn("should not print", output)
|
|
|
|
@unittest.skipUnless(pty, "requires pty")
|
|
def test_asyncio_repl_is_ok(self):
|
|
m, s = pty.openpty()
|
|
cmd = [sys.executable, "-I", "-m", "asyncio"]
|
|
env = os.environ.copy()
|
|
proc = subprocess.Popen(
|
|
cmd,
|
|
stdin=s,
|
|
stdout=s,
|
|
stderr=s,
|
|
text=True,
|
|
close_fds=True,
|
|
env=env,
|
|
)
|
|
os.close(s)
|
|
os.write(m, b"await asyncio.sleep(0)\n")
|
|
os.write(m, b"exit()\n")
|
|
output = []
|
|
while select.select([m], [], [], SHORT_TIMEOUT)[0]:
|
|
try:
|
|
data = os.read(m, 1024).decode("utf-8")
|
|
if not data:
|
|
break
|
|
except OSError:
|
|
break
|
|
output.append(data)
|
|
os.close(m)
|
|
try:
|
|
exit_code = proc.wait(timeout=SHORT_TIMEOUT)
|
|
except subprocess.TimeoutExpired:
|
|
proc.kill()
|
|
exit_code = proc.wait()
|
|
|
|
self.assertEqual(exit_code, 0, "".join(output))
|
|
|
|
|
|
@support.force_not_colorized_test_class
|
|
class TestInteractiveModeSyntaxErrors(unittest.TestCase):
|
|
|
|
@unittest.expectedFailure # TODO: RUSTPYTHON; ValueError: Process didn't exit properly.
|
|
def test_interactive_syntax_error_correct_line(self):
|
|
output = run_on_interactive_mode(dedent("""\
|
|
def f():
|
|
print(0)
|
|
return yield 42
|
|
"""))
|
|
|
|
traceback_lines = output.splitlines()[-4:-1]
|
|
expected_lines = [
|
|
' return yield 42',
|
|
' ^^^^^',
|
|
'SyntaxError: invalid syntax'
|
|
]
|
|
self.assertEqual(traceback_lines, expected_lines)
|
|
|
|
|
|
class TestAsyncioREPL(unittest.TestCase):
|
|
def test_multiple_statements_fail_early(self):
|
|
user_input = "1 / 0; print(f'afterwards: {1+1}')"
|
|
p = spawn_asyncio_repl()
|
|
p.stdin.write(user_input)
|
|
output = kill_python(p)
|
|
self.assertIn("ZeroDivisionError", output)
|
|
self.assertNotIn("afterwards: 2", output)
|
|
|
|
def test_toplevel_contextvars_sync(self):
|
|
user_input = dedent("""\
|
|
from contextvars import ContextVar
|
|
var = ContextVar("var", default="failed")
|
|
var.set("ok")
|
|
""")
|
|
p = spawn_asyncio_repl()
|
|
p.stdin.write(user_input)
|
|
user_input2 = dedent("""
|
|
print(f"toplevel contextvar test: {var.get()}")
|
|
""")
|
|
p.stdin.write(user_input2)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
expected = "toplevel contextvar test: ok"
|
|
self.assertIn(expected, output, expected)
|
|
|
|
def test_toplevel_contextvars_async(self):
|
|
user_input = dedent("""\
|
|
from contextvars import ContextVar
|
|
var = ContextVar('var', default='failed')
|
|
""")
|
|
p = spawn_asyncio_repl()
|
|
p.stdin.write(user_input)
|
|
user_input2 = "async def set_var(): var.set('ok')\n"
|
|
try:
|
|
import _pyrepl # noqa: F401
|
|
except ModuleNotFoundError:
|
|
# If we're going to be forced into the regular REPL, then we need an
|
|
# extra newline here. Omit it by default to catch any breakage to
|
|
# the new REPL's behavior.
|
|
user_input2 += "\n"
|
|
p.stdin.write(user_input2)
|
|
user_input3 = "await set_var()\n"
|
|
p.stdin.write(user_input3)
|
|
user_input4 = "print(f'toplevel contextvar test: {var.get()}')\n"
|
|
p.stdin.write(user_input4)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
expected = "toplevel contextvar test: ok"
|
|
self.assertIn(expected, output, expected)
|
|
|
|
def test_quiet_mode(self):
|
|
p = spawn_repl("-q", "-m", "asyncio", custom=True)
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
self.assertEqual(output[:3], ">>>")
|
|
|
|
@support.force_not_colorized
|
|
@support.subTests(
|
|
("startup_code", "expected_error"),
|
|
[
|
|
("some invalid syntax\n", "SyntaxError: invalid syntax"),
|
|
("1/0\n", "ZeroDivisionError: division by zero"),
|
|
],
|
|
)
|
|
def test_pythonstartup_failure(self, startup_code, expected_error):
|
|
startup_env = self.enterContext(
|
|
temp_pythonstartup(source=startup_code, histfile=".asyncio_history"))
|
|
|
|
p = spawn_repl(
|
|
"-qm", "asyncio",
|
|
env=os.environ | startup_env,
|
|
isolated=False,
|
|
custom=True)
|
|
p.stdin.write("print('user code', 'executed')\n")
|
|
output = kill_python(p)
|
|
self.assertEqual(p.returncode, 0)
|
|
|
|
tb_hint = f'File "{startup_env["PYTHONSTARTUP"]}", line 1'
|
|
self.assertIn(tb_hint, output)
|
|
self.assertIn(expected_error, output)
|
|
|
|
self.assertIn("user code executed", output)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|