| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
The current code degrades #121054 . Will fix it. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
…) 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 skip news label instead. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
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: cpython/Lib/_pyrepl/_module_completer.py Lines 20 to 21 in e1f8914 |
Sorry, something went wrong.
|
@tomasr8 good catch, how would you change this PR to make it work then? What test are we missing that should be failing now? |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
|
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}) |
Sorry, something went wrong.
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. |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
|
@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'> |
Sorry, something went wrong.
…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) <t.yic.yt@gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
|
GH-134473 is a backport of this pull request to the 3.14 branch. |
Sorry, something went wrong.
… 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) <t.yic.yt@gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
…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 <lukasz@langa.pl>
…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 <lukasz@langa.pl>
…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 <lukasz@langa.pl>
…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 <lukasz@langa.pl>
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.