Skip to content

Fix #26039: REPL prompt disappears when builtins/globals shadowed under PYTHONSTARTUP#26045

Open
mohityadav8 wants to merge 2 commits into
microsoft:mainfrom
mohityadav8:fix-pythonrc-prompt-shadowed-builtins
Open

Fix #26039: REPL prompt disappears when builtins/globals shadowed under PYTHONSTARTUP#26045
mohityadav8 wants to merge 2 commits into
microsoft:mainfrom
mohityadav8:fix-pythonrc-prompt-shadowed-builtins

Conversation

@mohityadav8

@mohityadav8 mohityadav8 commented Jul 19, 2026

Copy link
Copy Markdown

Fixes #26039

PYTHONSTARTUP executes pythonrc.py's code directly inside the user's __main__ namespace rather than importing it as a module. That means PS1.__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) breaks str(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:

  • Reproduced by exec-ing pythonrc.py's source into a synthetic __main__ dict (mirroring the real PYTHONSTARTUP path) and shadowing int/sys/str/bool/original_ps1/get_last_command — confirmed it broke on the original file and is fixed on the patched one.
  • Added test_prompt_survives_shadowed_builtins_under_pythonstartup, which encodes that reproduction as a regression test. The existing tests import pythonrc as a normal module, which gives PS1 its 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.ts only ever copies the single pythonrc.py file to the PYTHONSTARTUP location — splitting into two files would require extension-side deployment changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terminal REPL: >>> prompt disappears after assigning to int (pythonrc.py PS1 breaks when builtins are shadowed)

1 participant