Repository navigation
Fix mismatched traceback in pythonrc.py injection - #25480
Conversation
cab4d9f to
9af5a49
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes a traceback mismatch issue in the Python interactive console caused by VS Code's pythonrc.py injection. When PYTHONSTARTUP points to pythonrc.py, Python exposes the module's __loader__ in the global namespace, which causes linecache to incorrectly display tracebacks pointing to pythonrc.py instead of the actual source files.
Key Changes:
- Sets
__spec__and__loader__to None inpythonrc.pyto prevent linecache from caching this file - Adds an explanatory comment documenting why these attributes are set to None
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import readline | ||
|
|
||
| # Avoid caching this file by linecache and incorrectly report tracebacks. | ||
| __spec__ = __loader__ = None |
There was a problem hiding this comment.
The new behavior of setting __spec__ and __loader__ to None should have test coverage to ensure that these attributes are properly set and that they prevent linecache from incorrectly caching this file. Consider adding a test that verifies these attributes are None after module import.
|
Hi Muspi Merol (@CNSeniorious000)! Sorry for the delay on getting around to this, two items:
|
|
closing as stale |
|
I've added #26234 with an end-to-end repro in VS Code 1.141.2, Python extension 2026.10.0, and Python 3.14.8. The dynamic |
|
Hello, yes. This should be reopened. Taking a look :) |
Eduardo Villalpando Mello (edvilme)
left a comment
There was a problem hiding this comment.
Changes look good, and reflect the fix from upstream cpython. Added references to the cpython link and pr that are related to this.
Thanks for the fix :)
|
Related python/cpython#137576 |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
7ea2321
into
microsoft:main
Since vscode-python injects
PYTHONSTARTUPintopythonrc.pyto show the "Ctrl click to launch VS Code Native REPL" message, traceback always shows lines insidepythonrc.pyin the interactive console.Example:
That's because python sets
__main__to thepythonrc.pymodule, which has a__loader__attribute. The__loader__object is then exposed in the global namespace of the interactive console. However, linecache uses__loader__to get the source code, causing the mismatch.After this PR, the traceback points to the right source now:
In the
_pyrepl.__main__module, there was some comment mentioning this behavior and they set__loader__ = Noneto avoid this. See python/cpython#129098 for more information