Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-121790: Fix interactive console initialization #121793

Merged
merged 6 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/_pyrepl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
if not CAN_USE_PYREPL:
if not os.environ.get('PYTHON_BASIC_REPL', None) and FAIL_REASON:
if not os.getenv('PYTHON_BASIC_REPL') and FAIL_REASON:
from .trace import trace
trace(FAIL_REASON)
print(FAIL_REASON, file=sys.stderr)
Expand Down Expand Up @@ -51,5 +51,8 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
if not hasattr(sys, "ps2"):
sys.ps2 = "... "

from .console import InteractiveColoredConsole
from .simple_interact import run_multiline_interactive_console
run_multiline_interactive_console(namespace)
console = InteractiveColoredConsole(namespace, filename="<stdin>")
run_multiline_interactive_console(console)

4 changes: 2 additions & 2 deletions Lib/_pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
TYPE_CHECKING = False

if TYPE_CHECKING:
from typing import Any
from typing import Any, Mapping


MoreLinesCallable = Callable[[str], bool]
Expand Down Expand Up @@ -559,7 +559,7 @@ def stub(*args: object, **kwds: object) -> None:
# ____________________________________________________________


def _setup(namespace: dict[str, Any]) -> None:
def _setup(namespace: Mapping[str, Any]) -> None:
global raw_input
if raw_input is not None:
return # don't run _setup twice
Expand Down
14 changes: 3 additions & 11 deletions Lib/_pyrepl/simple_interact.py
ambv marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@

import _sitebuiltins
import linecache
import builtins
import sys
import code
from types import ModuleType

from .console import InteractiveColoredConsole
from .readline import _get_reader, multiline_input

TYPE_CHECKING = False
Expand Down Expand Up @@ -82,17 +79,12 @@ def _clear_screen():


def run_multiline_interactive_console(
namespace: dict[str, Any],
console: code.InteractiveConsole,
*,
future_flags: int = 0,
console: code.InteractiveConsole | None = None,
) -> None:
from .readline import _setup
_setup(namespace)

if console is None:
console = InteractiveColoredConsole(
namespace, filename="<stdin>"
)
ambv marked this conversation as resolved.
Show resolved Hide resolved
_setup(console.locals)
if future_flags:
console.compile.compiler.flags |= future_flags

Expand Down
27 changes: 9 additions & 18 deletions Lib/asyncio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,16 @@ def run(self):
exec(startup_code, console.locals)

ps1 = getattr(sys, "ps1", ">>> ")
if can_colorize():
if can_colorize() and CAN_USE_PYREPL:
ps1 = f"{ANSIColors.BOLD_MAGENTA}{ps1}{ANSIColors.RESET}"
console.write(f"{ps1}import asyncio\n")

try:
import errno
if os.getenv("PYTHON_BASIC_REPL"):
raise RuntimeError("user environment requested basic REPL")
if not os.isatty(sys.stdin.fileno()):
return_code = errno.ENOTTY
raise OSError(return_code, "tty required", "stdin")

# This import will fail on operating systems with no termios.
ambv marked this conversation as resolved.
Show resolved Hide resolved
if CAN_USE_PYREPL:
from _pyrepl.simple_interact import (
check,
run_multiline_interactive_console,
)
if err := check():
raise RuntimeError(err)
except Exception as e:
console.interact(banner="", exitmsg="")
else:
try:
run_multiline_interactive_console(console=console)
run_multiline_interactive_console(console)
except SystemExit:
# expected via the `exit` and `quit` commands
pass
Expand All @@ -129,6 +115,8 @@ def run(self):
console.showtraceback()
console.write("Internal error, ")
return_code = 1
else:
console.interact(banner="", exitmsg="")
finally:
warnings.filterwarnings(
'ignore',
Expand All @@ -139,7 +127,10 @@ def run(self):


if __name__ == '__main__':
CAN_USE_PYREPL = True
if os.getenv('PYTHON_BASIC_REPL'):
CAN_USE_PYREPL = False
else:
from _pyrepl.main import CAN_USE_PYREPL

return_code = 0
loop = asyncio.new_event_loop()
Expand Down
5 changes: 1 addition & 4 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,7 @@ def register_readline():
pass

if readline.get_current_history_length() == 0:
try:
from _pyrepl.main import CAN_USE_PYREPL
except ImportError:
CAN_USE_PYREPL = False
Comment on lines -520 to -523
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplify this here because in the current state of the code there is no reason for this import to ever fail.

from _pyrepl.main import CAN_USE_PYREPL
# If no history was loaded, default to .python_history,
# or PYTHON_HISTORY.
# The guard is necessary to avoid doubling history size at
Expand Down
63 changes: 60 additions & 3 deletions Lib/test/test_repl.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
"""Test the interactive interpreter."""

from io import BytesIO
import os
from re import sub
import tempfile
import select
import subprocess
import sys
import unittest
from textwrap import dedent
from test import support
from test.support import cpython_only, has_subprocess_support, SuppressCrashReport
from test.support import (
cpython_only,
has_subprocess_support,
SuppressCrashReport,
SHORT_TIMEOUT,
)
from test.support.script_helper import assert_python_failure, kill_python, assert_python_ok
from test.support.import_helper import import_module

try:
import pty
except ImportError:
pty = None


if not has_subprocess_support:
raise unittest.SkipTest("test module requires subprocess")
Expand Down Expand Up @@ -195,9 +209,52 @@ def bar(x):
expected = "(30, None, [\'def foo(x):\\n\', \' return x + 1\\n\', \'\\n\'], \'<stdin>\')"
self.assertIn(expected, output, expected)

def test_asyncio_repl_no_tty_fails(self):
assert assert_python_failure("-m", "asyncio")
Comment on lines -198 to -199
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test used to be test_asyncio_repl_is_ok before we broke it

def test_asyncio_repl_reaches_python_startup_script(self):
with tempfile.TemporaryDirectory() as tmpdir:
script = os.path.join(tmpdir, "script.py")
with open(script, "w") as f:
f.write("exit(0)")

subprocess.check_call(
[sys.executable, "-m", "asyncio"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env={"PYTHONSTARTUP": script}
)

@unittest.skipUnless(pty, "requires pty")
def test_asyncio_repl_is_ok(self):
m, s = pty.openpty()
cmd = [sys.executable, "-m", "asyncio"]
proc = subprocess.Popen(
cmd,
stdin=s,
stdout=s,
stderr=s,
text=True,
close_fds=True,
env=os.environ,
)
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)

class TestInteractiveModeSyntaxErrors(unittest.TestCase):

Expand Down
Loading