WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@CNSeniorious000
Copy link

@CNSeniorious000 CNSeniorious000 commented Sep 22, 2025

Since vscode-python injects PYTHONSTARTUP into pythonrc.py to show the "Ctrl click to launch VS Code Native REPL" message, traceback always shows lines inside pythonrc.py in the interactive console.

Example:

image

That's because python sets __main__ to the pythonrc.py module, 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:

image

In the _pyrepl.__main__ module, there was some comment mentioning this behavior and they set __loader__ = None to avoid this. See python/cpython#129098 for more information

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in pythonrc.py to 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.

if sys.platform != "win32":
import readline

# Avoid caching this file by linecache and incorrectly report tracebacks.
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has a grammatical error. It should say "incorrectly reporting tracebacks" instead of "incorrectly report tracebacks".

Suggested change
# Avoid caching this file by linecache and incorrectly report tracebacks.
# Avoid caching this file by linecache and incorrectly reporting tracebacks.

Copilot uses AI. Check for mistakes.
import readline

# Avoid caching this file by linecache and incorrectly report tracebacks.
__spec__ = __loader__ = None
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@eleanorjboyd
Copy link
Member

Hi @CNSeniorious000! Sorry for the delay on getting around to this, two items:

  1. can you create an issue and tag it in this PR, that way we can better track the fix through our release process
  2. the failing smoke tests should no longer appear after this new run as that was a flaky test we skipped for now

@eleanorjboyd eleanorjboyd added the bug Issue identified by VS Code Team member as probable bug label Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants