8000 gh-120678: pyrepl: Include globals from modules passed with `-i` by AlexWaygood · Pull Request #120904 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120678: pyrepl: Include globals from modules passed with -i #120904

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

Merged
merged 9 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'main' into repl-globals
  • Loading branch information
ambv committed Jun 26, 2024
commit 5af30e36357b9136301c8b32b711a7092ed577f1
7 changes: 2 additions & 5 deletions Lib/_pyrepl/__main__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Important: put as few things as possible in the global namespace of this module,
# as it's easy for things in this module's `__globals__` to accidentally end up
# in the globals of the REPL

from ._main import interactive_console
# Important: don't add things to this module, as they will end up in the REPL's
# default globals. Use _pyrepl.main instead.

if __name__ == "__main__":
from .main import interactive_console as __pyrepl_interactive_console
Expand Down
17 changes: 0 additions & 17 deletions Lib/_pyrepl/simple_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,12 @@ def _clear_screen():
}


def default_namespace() -> dict[str, Any]:
ns = {}
for key, value in sys.modules["__main__"].__dict__.items():
# avoid `getattr(value, "__module__", "")`,
# as some objects raise `TypeError` on attribute access, etc.
try:
module = value.__module__
except Exception:
pass
else:
if module.split(".")[0] == "_pyrepl":
continue
ns[key] = value
return ns


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

if console is None:
Expand Down
37 changes: 30 additions & 7 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,8 @@ 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__']"
)
pre = "['__annotations__', '__builtins__'"
post = "'__loader__', '__name__', '__package__', '__spec__']"
output, exit_code = self.run_repl(["sorted(dir())", "exit"])
if "can't use pyrepl" in output:
self.skipTest("pyrepl not available")
Expand Down Expand Up @@ -892,6 +890,31 @@ def test_dumb_terminal_exits_cleanly(self):
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)
8000
@force_not_colorized
def test_python_basic_repl(self):
env = os.environ.copy()
commands = ("from test.support import initialized_with_pyrepl\n"
"initialized_with_pyrepl()\n"
"exit()\n")

env.pop("PYTHON_BASIC_REPL", None)
output, exit_code = self.run_repl(commands, env=env)
if "can\'t use pyrepl" in output:
self.skipTest("pyrepl not available")
self.assertEqual(exit_code, 0)
self.assertIn("True", output)
self.assertNotIn("False", output)
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

env["PYTHON_BASIC_REPL"] = "1"
output, exit_code = self.run_repl(commands, env=env)
self.assertEqual(exit_code, 0)
self.assertIn("False", output)
self.assertNotIn("True", output)
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

def run_repl(
self,
repl_input: str | list[str],
Expand All @@ -900,11 +923,11 @@ def run_repl(
main_module: str | None = None,
) -> tuple[str, int]:
master_fd, slave_fd = pty.openpty()
repl_args = [sys.executable, "-u", "-i"]
repl_cmdline = [sys.executable, "-u", "-i"]
if main_module is not None:
repl_args.append(main_module)
repl_cmdline.append(main_module)
process = subprocess.Popen(
repl_args,
repl_cmdline,
stdin=slave_fd,
stdout=slave_fd,
stderr=slave_fd,
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0