-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Conversation
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.
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 |
The current code degrades #121054 . Will fix it. |
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 |
1 similar comment
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 |
…) 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.
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 |
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 |
Just wanted to mention that the import autocomplete feature currently assumes that the namespace is Here's the code that does that: cpython/Lib/_pyrepl/_module_completer.py Lines 20 to 21 in e1f8914
|
@tomasr8 good catch, how would you change this PR to make it work then? What test are we missing that should be failing now? |
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 |
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 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
Thank you @tomasr8 for the pointer and the snippet. I will take a look into it as well. |
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 |
@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'> |
…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]>
GH-134473 is a backport of this pull request to the 3.14 branch. |
… 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]>
…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]>
from . import *
within the REPL will import the_pyrepl
package, But executing with the-c
argument will result in an error. #127960