-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-118908: Use __main__ for the default PyREPL namespace #121054
Conversation
This allows for PYTHONSTARTUP to execute in the right namespace.
import tokenize | ||
with tokenize.open(startup_path) as f: | ||
startup_code = compile(f.read(), startup_path, "exec") | ||
exec(startup_code, namespace) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: manual PYTHONSTARTUP also executes in our custom namespace, otherwise it doesn't work. That's why namespace extraction from the optional mainmodule passed happens here already.
# if `__main__` is a cached .pyc file but there's no .py source file | ||
case4 = f"{pre}, '__cached__', '__doc__', {post}" in output | ||
|
||
self.assertTrue(case1 or case2 or case3 or case4, output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LMK if you know a more elegant solution for "one of these needs to pass".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is what I would have done, but I like yours better.
cases = [
f"{pre}, '__doc__', {post}", # if `__main__` is not a file (impossible with pyrepl)
...
]
self.assertTrue(any(case in output for case in cases), output)
# if `__main__` is a cached .pyc file but there's no .py source file | ||
case4 = f"{pre}, '__cached__', '__doc__', {post}" in output | ||
|
||
self.assertTrue(case1 or case2 or case3 or case4, output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is what I would have done, but I like yours better.
cases = [
f"{pre}, '__doc__', {post}", # if `__main__` is not a file (impossible with pyrepl)
...
]
self.assertTrue(any(case in output for case in cases), output)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thanks @ambv for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…nGH-121054) (cherry picked from commit d611c4c) Co-authored-by: Łukasz Langa <lukasz@langa.pl>
GH-121059 is a backport of this pull request to the 3.13 branch. |
|
|
This allows for PYTHONSTARTUP to execute in the same namespace that the interpreter later operates in.
Looks like Eugene was right in his initial implementation of the fix. We can't create a synthetic namespace because then
Modules/main.c:pymain_run_startup
is running in a namespace that isn't used later. There's probably more edge cases that using a synthetic namespace created; in Python__main__
is clearly expected as the namespace for interaction.Since
-i
exposes a different module as__main__
, I see no reason why regular REPL execution shouldn't expose__file__
pointing at_pyrepl.__main__
. Therefore, I adapted the test.