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

gh-118908: Limit exposed globals from internal imports and definitions on new REPL startup #119547

Merged
merged 19 commits into from
Jun 11, 2024

Conversation

eugenetriguba
Copy link
Contributor

@eugenetriguba eugenetriguba commented May 25, 2024

Currently, we expose some of the internal imports and definitions on REPL startup in the new REPL.

% ./python.exe
Python 3.14.0a0 (heads/main:de19694cfb, May 25 2024, 07:36:35) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['CAN_USE_PYREPL', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'interactive_console', 'os', 'sys']

This PR addresses that by moving the implementation of main into its own separate module and only importing in what is needed to get it running. Now, we see there is only interactive_console in addition to the dunder attributes shown in the global scope on startup.

% ./python.exe
Python 3.14.0a0 (heads/main:de19694cfb, May 25 2024, 07:36:35) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'interactive_console']

@eugenetriguba eugenetriguba changed the title gh-118908: Limit exposed globals in new REPL gh-118908: Limit exposed globals from internal imports and definitions on new REPL startup May 25, 2024
Lib/site.py Outdated Show resolved Hide resolved
Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @eugenetriguba but unfortunately I don't think this is the proper fix for this issue. The proper fix is to properly filter the namespace here:

mainmodule = mainmodule or __main__

And to use a clean one when it makes sense (when not running with -i) or filtering the stuff we don't want.

@bedevere-app
Copy link

bedevere-app bot commented May 28, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ambv ambv added the topic-repl Related to the interactive shell label May 31, 2024
@pablogsal pablogsal added the needs backport to 3.13 bugs and security fixes label Jun 11, 2024
Lib/_pyrepl/main.py Outdated Show resolved Hide resolved
Copy link
Contributor

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 🎉

Just a couple of comments.

Lib/test/test_pyrepl/test_pyrepl.py Outdated Show resolved Hide resolved
Lib/test/test_pyrepl/test_pyrepl.py Outdated Show resolved Hide resolved
Copy link
Contributor

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one final comment and this is good to go! Thanks both!

Lib/test/test_pyrepl/test_pyrepl.py Outdated Show resolved Hide resolved
Copy link
Contributor

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@pablogsal pablogsal merged commit 86a8a1c into python:main Jun 11, 2024
40 of 41 checks passed
@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 11, 2024
…nitions on new REPL startup (pythonGH-119547)

(cherry picked from commit 86a8a1c)

Co-authored-by: Eugene Triguba <eugenetriguba@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Jun 11, 2024

GH-120362 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 11, 2024
pablogsal pushed a commit that referenced this pull request Jun 11, 2024
@eugenetriguba
Copy link
Contributor Author

@pablogsal Been a bit busy and was unable to get back to this, thank you for finishing it off and the feedback 🙂

@encukou
Copy link
Member

encukou commented Jun 18, 2024

Since this commit, the "AMD64 Debian PGO 3.x" buildbot has started reporting a runaway process, e.g. here:

1:17:07 load avg: 20.12 [453/478/1] test_pyrepl failed (env changed) (37.4 sec)
test_empty (test.test_pyrepl.test_input.KeymapTranslatorTests.test_empty) ... ok
test_push_character_key (test.test_pyrepl.test_input.KeymapTranslatorTests.test_push_character_key) ... ok
test_push_character_key_with_stack (test.test_pyrepl.test_input.KeymapTranslatorTests.test_push_character_key_with_stack) ... ok
[...]
test_exposed_globals_in_repl (test.test_pyrepl.test_pyrepl.TestMain.test_exposed_globals_in_repl) ... skipped 'pyrepl not available'
[...]

----------------------------------------------------------------------
Ran 116 tests in 35.634s

OK (skipped=2)
Warning -- reap_children() reaped child process 3457256

== Tests result: ENV CHANGED then ENV CHANGED ==

mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-repl Related to the interactive shell
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants