Skip to content

Commit

Permalink
Simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed Jun 11, 2024
1 parent 5c5c496 commit a9c0854
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 12 additions & 3 deletions Lib/_pyrepl/simple_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import _sitebuiltins
import linecache
import builtins
import sys
import code
from types import ModuleType
Expand Down Expand Up @@ -73,20 +74,28 @@ def _clear_screen():
"clear": _clear_screen,
}

DEFAULT_NAMESPACE = {
'__name__': '__main__',
'__doc__': None,
'__package__': None,
'__loader__': None,
'__spec__': None,
'__annotations__': {},
'__builtins__': builtins,
}

def run_multiline_interactive_console(
mainmodule: ModuleType | None = None,
future_flags: int = 0,
console: code.InteractiveConsole | None = None,
) -> None:
import __main__
from .readline import _setup
_setup()

mainmodule = mainmodule or __main__
namespace = mainmodule.__dict__ if mainmodule else DEFAULT_NAMESPACE
if console is None:
console = InteractiveColoredConsole(
mainmodule.__dict__, filename="<stdin>"
namespace, filename="<stdin>"
)
if future_flags:
console.compile.compiler.flags |= future_flags
Expand Down
11 changes: 7 additions & 4 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import termios
from unittest import TestCase
from unittest.mock import patch
from test.support import force_not_colorized

from .support import (
FakeConsole,
Expand Down Expand Up @@ -837,13 +838,15 @@ def test_bracketed_paste_single_line(self):


class TestMain(TestCase):
@force_not_colorized
def test_exposed_globals_in_repl(self):
expected_output = (
"['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', "
"'__loader__', '__name__', '__package__', '__spec__', 'interactive_console']"
'["__annotations__", "__builtins__", "__doc__", "__loader__", '
'"__name__", "__package__", "__spec__"]'
)
output, exit_code = self.run_repl(["dir()", "exit"])
output, exit_code = self.run_repl(["sorted(dir())", "exit"])
self.assertEqual(exit_code, 0)
output = output.replace("\'", '"')
self.assertIn(expected_output, output)

def test_dumb_terminal_exits_cleanly(self):
Expand All @@ -865,7 +868,7 @@ def run_repl(self, repl_input: str | list[str], env: dict | None = None) -> tupl
text=True,
close_fds=True,
env=env if env else os.environ,
)
)
if isinstance(repl_input, list):
repl_input = "\n".join(repl_input) + "\n"
os.write(master_fd, repl_input.encode("utf-8"))
Expand Down

0 comments on commit a9c0854

Please sign in to comment.