Skip to content
Open
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 Lib/idlelib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@

Run IDLE as python -m idlelib
"""
import sys

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]
Comment on lines +8 to +11

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.


import idlelib.pyshell
idlelib.pyshell.main()
8 changes: 7 additions & 1 deletion Lib/idlelib/idle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import os.path
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.
Expand Down
23 changes: 22 additions & 1 deletion Lib/idlelib/idle_test/test_pyshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

from idlelib import pyshell
import os
import sys
import unittest
from test.support import requires
from unittest import mock
from test.support import os_helper, requires
from test.support.script_helper import assert_python_ok
from tkinter import Tk


Expand Down Expand Up @@ -37,6 +40,24 @@ def test_fix_user_path(self):
eq(pyshell.fix_user_path(['/a', '/b']), ['/a', '/b'])
eq(pyshell.fix_user_path([idlelib_dir]), [])

def test_shadowed_stdlib(self):
# gh-70331: user files in the current directory must not shadow
# the stdlib modules imported by IDLE.
with os_helper.temp_dir() as cwd:
for name in ('os', 'random', 'tkinter'):
os_helper.create_empty_file(os.path.join(cwd, f'{name}.py'))
for module in 'idlelib', 'idlelib.idle':
with self.subTest(module=module):
assert_python_ok('-m', module, '-h',
__isolated=False, __cwd=cwd)

def test_build_subprocess_arglist(self):
interp = mock.Mock(port=1234)
args = pyshell.ModifiedInterpreter.build_subprocess_arglist(interp)
# gh-70331: -P keeps the current directory out of sys.path.
self.assertEqual(args[:2], [sys.executable, '-P'])
self.assertEqual(args[-1], '1234')


class PyShellFileListTest(unittest.TestCase):

Expand Down
5 changes: 4 additions & 1 deletion Lib/idlelib/pyshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,10 @@ def build_subprocess_arglist(self):
del_exitf = idleConf.GetOption('main', 'General', 'delete-exitfunc',
default=False, type='bool')
command = f"__import__('idlelib.run').run.main({del_exitf!r})"
return [sys.executable] + w + ["-c", command, str(self.port)]
# -P keeps the current directory off sys.path, so that user files
# do not shadow run's imports (gh-70331). transfer_path() sets
# sys.path later.
return [sys.executable, '-P'] + w + ["-c", command, str(self.port)]

def start_subprocess(self):
addr = (HOST, self.port)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
IDLE no longer fails to start with ``python -m idlelib``, and its user process
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``.
Loading