gh-70331: Protect IDLE's imports from user files in the current directory - #157643
serhiy-storchaka wants to merge 1 commit into
Conversation
… 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.
terryjreedy
left a comment
There was a problem hiding this comment.
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.
| 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``. |
There was a problem hiding this comment.
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.
| 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] |
There was a problem hiding this comment.
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.
|
When you're done making the requested changes, leave the comment: |
A user file such as
random.py,threading.pyortkinter.pyin 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 withpython -c), and IDLE failed to start.Now the user process is started with
-P, so the current directory is not onsys.pathwhileidlelib.runand its dependencies are imported.sys.pathof the user process is set bytransfer_path()later anyway, so user code sees the samesys.pathas before.python -m idlelibremoves the current directory fromsys.pathbefore importingidlelib.pyshell, which imports almost all of IDLE.pyshell.main()still adds the current directory or the directories of the files to be edited tosys.pathof the IDLE process, so the few modules imported later (pydoc) can still be shadowed. Changing this would changesys.pathseen by user code, so it is left for a separate change.🤖 Generated with Claude Code