[New-bugs-announce] [issue18330] Fix idlelib.PyShell.build_subprocess_arglist use of __import__

Terry J. Reedy report at bugs.python.org
Sun Jun 30 00:09:24 CEST 2013

New submission from Terry J. Reedy:

The purpose of the function is to create a command line for the user subprocess. Most of its body:
# Maybe IDLE is installed and is being accessed via sys.path,
# or maybe it's not installed and the idle.py script is being
# run from the IDLE source directory.
del_exitf = idleConf.GetOption('main', 'General', 'delete-exitfunc',
                               default=False, type='bool')
if __name__ == 'idlelib.PyShell':
    command = "__import__('idlelib.run').run.main(%r)" % (del_exitf,)
    command = "__import__('run').main(%r)" % (del_exitf,)
return [sys.executable] + w + ["-c", command, str(self.port)]
Question: is it really important to avoid binding the run module to 'run' in the user process? If so, maybe we should use importlib.import_module, as 'direct use of __import__ is entirely discouraged".

The first command looks 'funny' because of the repetition of 'run'. The reason is that __import__('idlelib.run') returns idlelib, not idlelib.run. Perhaps it would work to delete .run in the import, or to use importlib.import_module.

The second command incorrectly assumes that if __name__ == '__main__' (the alternative to 'idlelib.PyShell'), then the directory containing PyShell and run is the current working directory. This is true if PyShell is run from that directory, but

F:\Python\dev\py33\PCbuild>python_d -m idlelib.PyShell
F:\Python\dev\py33\PCbuild>python_d ../Lib/idlelib/PyShell.py

both report "ImportError: No module named 'run'" and open a shell window and error message box a few seconds later. The shell closes when the messagebox is dismissed.

It seems to me that the 'else' caters to a non-existent or impossible use case. PyShell has several 'from idlelib.X import Y' statements. If those work, then  "from idlelib import run' must work, and so too must the function equivalent.

messages: 192053
nosy: roger.serwy, terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: Fix idlelib.PyShell.build_subprocess_arglist  use of __import__
type: behavior
versions: Python 2.7, Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list