Fix #26039: REPL prompt disappears when builtins/globals shadowed under PYTHONSTARTUP#26045
Open
mohityadav8 wants to merge 2 commits into
Open
Fix #26039: REPL prompt disappears when builtins/globals shadowed under PYTHONSTARTUP#26045mohityadav8 wants to merge 2 commits into
mohityadav8 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #26039
PYTHONSTARTUPexecutespythonrc.py's code directly inside the user's__main__namespace rather than importing it as a module. That meansPS1.__str__.__globals__is the user's namespace — so shadowing any name it relies on at prompt-render time (int,sys,str,bool,original_ps1,get_last_command) breaksstr(sys.ps1)and silently kills the prompt.This captures the real objects into private
_-prefixed aliases right after they're defined, before any user code runs, so later reassignment of those names in__main__can't affect the prompt anymore.How I tested:
exec-ingpythonrc.py's source into a synthetic__main__dict (mirroring the realPYTHONSTARTUPpath) and shadowingint/sys/str/bool/original_ps1/get_last_command— confirmed it broke on the original file and is fixed on the patched one.test_prompt_survives_shadowed_builtins_under_pythonstartup, which encodes that reproduction as a regression test. The existing testsimport pythonrcas a normal module, which givesPS1its own module namespace instead of__main__— that's why they never caught this bug.npm run check-python(ruff check, ruff format --check, pyright) — clean, 0 errors.python -m pytest python_files/tests/test_shell_integration.py -v— 5/5 passed.Notes for reviewers: the fix is intentionally minimal (capture at definition time) rather than moving the class into a separate module, since
pythonStartup.tsonly ever copies the singlepythonrc.pyfile to the PYTHONSTARTUP location — splitting into two files would require extension-side deployment changes.