cpython/Lib/idlelib/idle.py
Miss Islington (bot) 7a563e3f98
[3.15] gh-70331: Protect IDLE's imports from user files in the current directory (GH-157643) (#158034)
gh-70331: Protect IDLE's imports from user files in the current directory (GH-157643)

Start the user process with -P, so that the current directory is not on
sys.path while idlelib.run and its dependencies are imported.  sys.path
is set later by transfer_path().  Protect __main__, idle, and pyshell entry points.
(cherry picked from commit 6893326350)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2026-10-09 17:22:45 -04:00

20 lines
657 B
Python

import sys
if __spec__ is not None and not sys.flags.safe_path:
# Remove the current directory, prepended by "python -m", so that
# user files do not shadow IDLE's imports (gh-70331).
del sys.path[0]
import os.path
# Enable running IDLE with idlelib in a non-standard location.
# This was once used to run development versions of IDLE.
# Because PEP 434 declared idle.py a public interface,
# removal should require deprecation.
idlelib_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if idlelib_dir not in sys.path:
sys.path.insert(0, idlelib_dir)
from idlelib.pyshell import main # This is subject to change
main()