Fix #26039: REPL prompt disappears when builtins/globals shadowed under PYTHONSTARTUP - #26045
Conversation
|
Eleanor Boyd (@eleanorjboyd) hey I think Icannot add label in this |
|
Eleanor Boyd (@eleanorjboyd) all cehcks are passed |
|
Hello Mohit Yadav (@mohityadav8) |
|
🔒 Automated review in progress — Bill Schnurr (@bschnurr) is auto-reviewing this PR. |
| # str will get called for every prompt with exit code to show success/failure | ||
| def __str__(self): | ||
| exit_code = int(bool(self.hooks.failure_flag)) | ||
| exit_code = _int(_bool(self.hooks.failure_flag)) |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
The private aliases remain mutable entries in the user's __main__ namespace, so assigning _int, _sys, _str, _bool, or _original_ps1 can still break or alter prompt rendering. Capture these dependencies in closure cells or function defaults, and cover shadowing the private aliases too.
[verified]
| # __main__.__dict__. That means PS1.__str__.__globals__ IS the user's | ||
| # namespace: if the user later shadows a name we rely on at prompt-render | ||
| # time (e.g. `int = 20`, `sys = 1`, `original_ps1 = ...`), a plain global | ||
| # lookup would resolve to the user's value instead of ours and raise, |
There was a problem hiding this comment.
Issue · Please address or respond
Remove the issue reference from this production-code comment. docs/pylancewiki/review/issue-references.md prohibits issue links in production-code comments, with no applicable exception here.
[verified]
| print.assert_any_call("Ctrl click to launch VS Code Native REPL") | ||
|
|
||
|
|
||
| def test_prompt_survives_shadowed_builtins_under_pythonstartup(): |
There was a problem hiding this comment.
Issue · Please address or respond
Remove the issue reference from this test docstring and keep the linkage in PR or commit metadata. Committed regression tests are explicitly covered by docs/pylancewiki/review/issue-references.md.
[verified]
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.