Title: Fix idlelib.PyShell.build_subprocess_arglist use of __import__
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.7, Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: roger.serwy, terry.reedy
Priority: normal Keywords:

Created on 2013-06-29 22:09 by terry.reedy, last changed 2020-06-06 23:09 by terry.reedy. This issue is now closed.

Messages (5)
msg192053 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-06-29 22:09
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 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__('').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__('') returns idlelib, not 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/

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.
msg223281 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-16 21:20
@Terry just a reminder for yourself and your band of merry men and women.
msg223294 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-07-16 23:12
The problem with touching this stuff is that there is not only not an automatec test, there is not even a script for a manual test.
msg243333 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-05-16 16:54
Since filing this, I have decided that starting Idle directly with PyShell should be deprecated.  That would affect any patch for this.  In the meanwhile, there are other priorities.
msg370858 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-06-06 23:09
In #36429, the 'else' branch that did not work was deleted.

The reason __import__ is discouraged is the reason for the repetition of 'run', in the line kept, as explained above.  However, since the line works, it is not a bug, and I now prefer to leave it alone.  If someone disables it, IDLE will not start.

del_exitf was added for Visual IDLE -- VIDLE (sp?)  Investigating whether it is still needed and possible deleting it is another issue.  So is deprecating starting with PyShell, or having pyshell as main running tests in a development setting.
Date User Action Args
2020-06-06 23:09:26terry.reedysetstatus: open -> closed
resolution: not a bug
messages: + msg370858

stage: needs patch -> resolved
2017-06-19 21:46:27BreamoreBoysetnosy: - BreamoreBoy
2017-06-19 21:45:19terry.reedysetassignee: terry.reedy
components: + IDLE
versions: + Python 3.6, Python 3.7, - Python 2.7, Python 3.4, Python 3.5
2015-05-16 16:54:34terry.reedysetmessages: + msg243333
2014-07-16 23:12:02terry.reedysetmessages: + msg223294
2014-07-16 21:20:29BreamoreBoysetnosy: + BreamoreBoy

messages: + msg223281
versions: + Python 3.5, - Python 3.3
2013-06-29 22:09:24terry.reedycreate