Skip to content

gh-127960 Fix the REPL to set the correct namespace by setting the correct __main__ module #134275

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 10 commits into from
May 22, 2025

Conversation

whitphx
Copy link
Contributor

@whitphx whitphx commented May 19, 2025

whitphx added 3 commits May 19, 2025 16:05
The `__main__` module imported in the `_pyrepl` module points to the `_pyrepl` module itself when the interpreter was launched without `-m` option and didn't execute a module,
while it's an unexpected behavior that `__main__` can be `_pyrepl` and relative imports such as `from . import *` works based on the `_pyrepl` module.
… REPL.

The Python-based REPL (_pyrepl.main.interactive_console) no longer loads the real __main__ module so it should be passed from its caller.
@python-cla-bot
Copy link

python-cla-bot bot commented May 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented May 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@whitphx
Copy link
Contributor Author

whitphx commented May 19, 2025

The current code degrades #121054 . Will fix it.

@bedevere-app
Copy link

bedevere-app bot commented May 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

1 similar comment
@bedevere-app
Copy link

bedevere-app bot commented May 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

…) instead of pymain_run_module(L"_pyrepl", 0)

This change is an equivalent to the one done in https://github.com/python/cpython/pull/120904/files#diff-79e40dbd94b164b5f42a960224cc7496e33c189b4c66a6810904eda7d703b6f2R600
Two callers of `pymain_start_pyrepl` differs in whether the PYTHONSTARTUP script is already executed or not, so pythonstartup argument is added to `pymain_start_pyrepl` to control whether it should be executed in it or not.
@whitphx whitphx force-pushed the fix-issue-127960 branch from 711d724 to 3d76ed8 Compare May 19, 2025 22:06
@bedevere-app
Copy link

bedevere-app bot commented May 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link

bedevere-app bot commented May 21, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@ambv ambv marked this pull request as ready for review May 21, 2025 16:00
@ambv ambv added the needs backport to 3.14 bugs and security fixes label May 21, 2025
@tomasr8
Copy link
Member

tomasr8 commented May 21, 2025

Just wanted to mention that the import autocomplete feature currently assumes that the namespace is _pyrepl (to match what the import would actually do). This might also need to be changed.

Here's the code that does that:

# Inside pyrepl, __package__ is set to '_pyrepl'
return ModuleCompleter(namespace={'__package__': '_pyrepl'})

@ambv
Copy link
Contributor

ambv commented May 21, 2025

@tomasr8 good catch, how would you change this PR to make it work then? What test are we missing that should be failing now?

@tomasr8
Copy link
Member

tomasr8 commented May 21, 2025

I'm surprised no test caught it, I'll have a look

I shouldn't be surprised, for the purposes of import completion we hardcode so any change to pyrepl itself would not affect it (and neither the tests)

I think we can simply set __package__ to None here as well. If relative imports are not to be supported by pyrepl we might even be able to remove the whole code path for that in the completer

@tomasr8
Copy link
Member

tomasr8 commented May 21, 2025

Something like this would be a minimal change to align the completer behaviour but we might be able to remove the entire namespace handling inside ModuleCompleter if this is always going to be None:

diff --git a/Lib/_pyrepl/_module_completer.py b/Lib/_pyrepl/_module_completer.py
index 9aafb55090e..effba0ba23f 100644
--- a/Lib/_pyrepl/_module_completer.py
+++ b/Lib/_pyrepl/_module_completer.py
@@ -17,8 +17,8 @@
 
 
 def make_default_module_completer() -> ModuleCompleter:
-    # Inside pyrepl, __package__ is set to '_pyrepl'
-    return ModuleCompleter(namespace={'__package__': '_pyrepl'})
+    # Inside pyrepl, __package__ is set to None
+    return ModuleCompleter(namespace={'__package__': None})

The added test cases assert
* __package__ is None when PyREPL is launched without options
* PYTHONSTARTUP works as expected in all the cases; the PyREPL is launched with -m, without -m, and with a file path
@whitphx
Copy link
Contributor Author

whitphx commented May 21, 2025

Thank you @tomasr8 for the pointer and the snippet.

I will take a look into it as well.
What makes it complicated is the case with -m. In the case, PyREPL is launched in the namespace of the module specified by -m so relative imports should work based on it?

@whitphx
Copy link
Contributor Author

whitphx commented May 21, 2025

What about changes like this?

diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py
index 560a9db192..572eee520e 100644
--- a/Lib/_pyrepl/readline.py
+++ b/Lib/_pyrepl/readline.py
@@ -606,6 +606,7 @@ def _setup(namespace: Mapping[str, Any]) -> None:
     # set up namespace in rlcompleter, which requires it to be a bona fide dict
     if not isinstance(namespace, dict):
         namespace = dict(namespace)
+    _wrapper.config.module_completer = ModuleCompleter(namespace)
     _wrapper.config.readline_completer = RLCompleter(namespace).complete

     # this is not really what readline.c does.  Better than nothing I guess

@ambv
Copy link
Contributor

ambv commented May 21, 2025

@whitphx, yes, this is OK and in fact how the old REPL used to behave:

❯ $PYTHON_BASIC_REPL=1 $PYTHONSTARTUP="" ./python.exe -i -m sysconfig
Platform: "macosx-15.5-arm64"
Python version: "3.15"
...
>>> from . import __main__ as m
>>> m
<module 'sysconfig.__main__' from '/Volumes/RAMDisk/cpython-main/Lib/sysconfig/__main__.py'>

And this is PyREPL with the current PR applied, which is the same (correct) behavior:

❯ $PYTHONSTARTUP="" ./python.exe -i -m sysconfig
Platform: "macosx-15.5-arm64"
Python version: "3.15"
...
>>> __file__
'/Volumes/RAMDisk/cpython-main/Lib/sysconfig/__main__.py'
>>> from . import __main__ as m
>>> m
<module 'sysconfig.__main__' from '/Volumes/RAMDisk/cpython-main/Lib/sysconfig/__main__.py'>

@ambv ambv merged commit b1b8962 into python:main May 22, 2025
47 checks passed
@miss-islington-app
Copy link

Thanks @whitphx for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 22, 2025
…the correct `__main__` module (pythongh-134275)

The `__main__` module imported in the `_pyrepl` module points to the `_pyrepl` module itself when the interpreter was launched without `-m` option and didn't execute a module,
while it's an unexpected behavior that `__main__` can be `_pyrepl` and relative imports such as `from . import *` works based on the `_pyrepl` module.
(cherry picked from commit b1b8962)

Co-authored-by: Yuichiro Tachibana (Tsuchiya) <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented May 22, 2025

GH-134473 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label May 22, 2025
@whitphx whitphx deleted the fix-issue-127960 branch May 22, 2025 00:50
ambv added a commit that referenced this pull request May 22, 2025
… the correct `__main__` module (gh-134275) (gh-134473)

The `__main__` module imported in the `_pyrepl` module points to the `_pyrepl` module itself when the interpreter was launched without `-m` option and didn't execute a module,
while it's an unexpected behavior that `__main__` can be `_pyrepl` and relative imports such as `from . import *` works based on the `_pyrepl` module.
(cherry picked from commit b1b8962)

Co-authored-by: Yuichiro Tachibana (Tsuchiya) <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
lkollar pushed a commit to lkollar/cpython that referenced this pull request May 26, 2025
…the correct `__main__` module (pythongh-134275)

The `__main__` module imported in the `_pyrepl` module points to the `_pyrepl` module itself when the interpreter was launched without `-m` option and didn't execute a module,
while it's an unexpected behavior that `__main__` can be `_pyrepl` and relative imports such as `from . import *` works based on the `_pyrepl` module.

Co-authored-by: Łukasz Langa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants