Repository navigation
Commit 922c2bf
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.
---------
Co-authored-by: Eduardo Villalpando Mello <eduardo.villalpando.mello@gmail.com>
1 parent 710b93b commit 922c2bf
2 files changed
Lines changed: 56 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
12 | 26 | | |
13 | 27 | | |
14 | 28 | | |
| |||
53 | 67 | | |
54 | 68 | | |
55 | 69 | | |
56 | | - | |
| 70 | + | |
57 | 71 | | |
58 | 72 | | |
59 | 73 | | |
| |||
64 | 78 | | |
65 | 79 | | |
66 | 80 | | |
67 | | - | |
| 81 | + | |
68 | 82 | | |
69 | 83 | | |
70 | | - | |
| 84 | + | |
71 | 85 | | |
72 | 86 | | |
73 | 87 | | |
| |||
76 | 90 | | |
77 | 91 | | |
78 | 92 | | |
79 | | - | |
| 93 | + | |
80 | 94 | | |
81 | 95 | | |
82 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
70 | 73 | | |
71 | 74 | | |
72 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
73 | 110 | | |
74 | 111 | | |
75 | 112 | | |
| |||
0 commit comments