Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python_files/pythonrc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# PYTHONSTARTUP runs in __main__; clear its metadata at module scope so linecache
# cannot use this file's loader for later interactive tracebacks.
# CPython issue: https://github.com/python/cpython/issues/129098
# CPython fix: https://github.com/python/cpython/pull/130721
__spec__ = __loader__ = None

Copilot AI Dec 12, 2025

Copy link

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.


def _initialize():
import platform
import sys
Expand Down
19 changes: 19 additions & 0 deletions python_files/tests/test_shell_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import importlib
import linecache
import platform
import sys
from importlib.machinery import ModuleSpec, SourceFileLoader
from pathlib import Path
from typing import Any, Protocol, cast
from unittest.mock import Mock
Expand Down Expand Up @@ -81,6 +83,23 @@ def test_does_not_pollute_namespace():
assert not [name for name in vars(pythonrc) if not name.startswith("__")]


def test_pythonstartup_clears_loader_metadata_for_tracebacks(monkeypatch, tmp_path):
source = PYTHONRC_PATH.read_text(encoding="utf-8")
loader = SourceFileLoader("__main__", str(PYTHONRC_PATH))
namespace: dict[str, Any] = {
"__name__": "__main__",
"__loader__": loader,
"__spec__": ModuleSpec("__main__", loader),
}
monkeypatch.setattr(linecache, "cache", {})

exec(compile(source, str(PYTHONRC_PATH), "exec"), namespace)

assert linecache.getline(str(tmp_path / "missing.py"), 1, namespace) == ""
assert namespace["__loader__"] is None
assert namespace["__spec__"] is None


def test_replacement_regex_removes_bel_terminated_osc():
if sys.version_info < (3, 13):
return
Expand Down
Loading