Skip to content

gh-70331: Protect IDLE's imports from user files in the current directory - #157643

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-70331-shadow-stdlib
Open

serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-70331-shadow-stdlib

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

A user file such as random.py, threading.py or tkinter.py in the directory from which IDLE was started (on Windows, "Edit with IDLE" starts it in the file's directory; on macOS, IDLE.app starts in ~/Documents) shadowed the stdlib modules imported by IDLE, in the IDLE process (python -m idlelib) or in the user process (started with python -c), and IDLE failed to start.

Now the user process is started with -P, so the current directory is not on sys.path while idlelib.run and its dependencies are imported. sys.path of the user process is set by transfer_path() later anyway, so user code sees the same sys.path as before. python -m idlelib removes the current directory from sys.path before importing idlelib.pyshell, which imports almost all of IDLE.

pyshell.main() still adds the current directory or the directories of the files to be edited to sys.path of the IDLE process, so the few modules imported later (pydoc) can still be shadowed. Changing this would change sys.path seen by user code, so it is left for a separate change.

🤖 Generated with Claude Code

… directory

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().

"python -m idlelib" now removes the current directory from sys.path.
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 16, 2026

@terryjreedy terryjreedy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I once did some research on on what sys.path looks like after various methods of starting python without IDLE. Probably on some other issue. I should repeat and save somewhere in idlelib.

Comment on lines +1 to +3
IDLE no longer fails to start when the current directory contains user files
with the same names as standard library modules that IDLE imports, such as
``random.py`` or ``tkinter.py``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not true for the current patch, which only applies when IDLE is started with python -m idlelib or if the problem occurs when starting the subprocess, due to masking files alongside python.exe. The latter is only an issue if one starts with Shell. There are people who start with an editor, which I believe is the default.

Comment thread Lib/idlelib/__main__.py
Comment on lines +8 to +11
if 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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This code should be either duplicated in idlelib.idle, idlelib.idlew, and pyshell, or moved to the top of pyshell or a new idlestartup file. If moved to 1 place, the name could be potentially masked, but making it a longish name unlikely to be accidentally masked would be ok since it would only be imported from the other entry points. Perhaps we should make idle.py the main entry point after removing the junk currently there.

@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants