Issue 18330: Fix idlelib.PyShell.build_subprocess_arglist use of import (original) (raw)

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,) else: 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.

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.