Skip to content
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

New REPL does not include globals from executed module when used with -i #120678

Closed
AlexWaygood opened this issue Jun 18, 2024 · 4 comments
Closed
Assignees
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 18, 2024

Bug report

Bug description:

If I have a module called foo.py with the following contents in the root of my CPython local clone:

class Foo: pass

Then running PYTHON_BASIC_REPL=1 ./python.exe -i foo.py, I get the following behaviour: foo.py is executed, and the Foo class is available in the globals of the REPL:

~/dev/cpython (main)⚡ % PYTHON_BASIC_REPL=1 ./python.exe -i foo.py 
>>> Foo
<class '__main__.Foo'>

But with the new REPL, the Foo class isn't available!

~/dev/cpython (main)⚡ % ./python.exe -i foo.py 
>>> Foo
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    Foo
NameError: name 'Foo' is not defined
>>> 

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@AlexWaygood AlexWaygood added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell labels Jun 18, 2024
@eryksun
Copy link
Contributor

eryksun commented Jun 18, 2024

It seems the new REPL doesn't get executed in the __main__ namespace. This can be worked around manually:

$ python -ic "class Foo: pass"
>>> Foo
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    Foo
NameError: name 'Foo' is not defined
>>> import sys
>>> from _pyrepl.simple_interact import run_multiline_interactive_console
>>> run_multiline_interactive_console(sys.modules['__main__'])
>>> Foo
<class '__main__.Foo'>
>>> dir()
['CAN_USE_PYREPL', 'Foo', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'interactive_console', 'os', 'sys']

This can be addressed in "Lib/_pyrepl/__main__.py". All imports and assignments in that module (e.g. CAN_USE_PYREPL, interactive_console, os, and sys) should be uniquely prefixed to avoid collisions and to allow clearing them before calling run_interactive(mainmodule).

@vstinner
Copy link
Member

I marked #120798 as a duplicate of this issue.

@AlexWaygood
Copy link
Member Author

This is a regression that was caused by #119547.

@AlexWaygood AlexWaygood self-assigned this Jun 22, 2024
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Jun 22, 2024
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Jun 22, 2024
ambv added a commit that referenced this issue Jul 17, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 17, 2024
pythonGH-120904)

(cherry picked from commit ac07451)

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
ambv added a commit that referenced this issue Jul 17, 2024
…i` (GH-120904) (#121916)

(cherry picked from commit ac07451)

Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
ambv added a commit to ambv/cpython that referenced this issue Jul 17, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 17, 2024
Yhg1s pushed a commit that referenced this issue Jul 17, 2024
…121924) (#121929)

gh-120678: Guard against stdin.fileno() being unavailable (GH-121924)
(cherry picked from commit 19cbf8f)

Co-authored-by: Łukasz Langa <[email protected]>
@ambv
Copy link
Contributor

ambv commented Jul 22, 2024

This is solved now, thanks everyone!

@ambv ambv closed this as completed Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants