classification
Title: IDLE: user code 'import tkinter; tkinter.font' should fail
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: markroseman, python-dev, terry.reedy
Priority: normal Keywords: patch

Created on 2015-10-29 10:10 by terry.reedy, last changed 2016-11-11 00:10 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
tkimports.py terry.reedy, 2016-07-15 21:03
run_autocomplete.diff terry.reedy, 2016-07-16 00:10 review
tkimports.py terry.reedy, 2016-07-17 00:17
Messages (22)
msg253671 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-10-29 10:10
In python, 'import tkinter; tkinter.font' fails with AttributeError.  In IDLE, it does not, which is a bug,  This lead to a Stackoverflow question 'Why does my code run in IDLE but not with Python'?

The issue is that importing modules in a package has the side-effect of adding the module name to the package namespace.  IDLE's user process runs idlelib/run.py.  While *run* does not import tkinter submodules, it imports other modules that do, and the net effect is to add colorchooser, commondialog, dialog, filedialog, font, messagebox, and simpledialog to the tkinter namespace, linked to the corresponding module objects.  None are needed in normal operation.

My first thought was to refactor so that the additions, which run does not need, are not added.  My second thought seemed simpler.  Delete them (in run.py) after the imports.  But it turns out that after deleting a submodule attribute re-import does not work right; the name addition only happens when a module is created, not when found in the sys.modules cache.

>>> import tkinter; import tkinter.font  # imagine this in run.py
>>> tkinter.font
<module tkinter.font...>
>>> del tkinter.font  # and this in run also, after all imports
>>> import tkinter.font  # imagine this in user code
>>> tkinter.font  # and then this
Traceback (most recent call last):  # it does not work as it should
  File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'font'

Scratch that idea, and return to refactoring.  An obvious culprit in run.py is the import of PyShell.  This leads to the import of nearly all of idlelib.  However, there are only 4 shared objects actually used, and I believe they could just as well be defined in run (or even in rpc.py or something) and imported from run into PyShell.  Then the PyShell import could be deleted.  I still need to look at the other imports.

On startup, user sys.modules also has about 50 other (non-idlelib) stdlib modules not imported by python itself.  Not importing PyShell in the 2nd process should reduce this number and speed up IDLE startup, which takes several seconds, first time after boot up, on Windows.  It would be good to only import into the user process what is actually needed.

(Initially importing into the idle process only what is needed to start would also be good, but a separate issue.)

In 2.7, Tkinter is not a package, so I do not believe it is directly affected by this issue.  On the other hand, it also imports too much.  So backporting changes to keep things mostly synchronized should benefit 2.7 startup time also.
msg253683 - (view) Author: Mark Roseman (markroseman) * Date: 2015-10-29 16:08
This (restructuring/refactoring to minimize the subprocess imports) does definitely sound like the right approach. There will be other benefits to breaking up PyShell a bit too..
msg253716 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-10-30 04:15
#8231 will benefit if indirect imports of configHandler and idleConf into the user process can be eliminated.  I do not currently believe it is needed.  Not importing PyShell will go part way.

AutoComplete imports idleConf to get the popup delay. run imports AutoComplete to access fetch_completions -- in particular, the part after 'else', after the logic to determine which process the code is in and if in the idle process, whether to fetch in the idle process (-n) or user process.  Fetch_completions is a function and not properly a method -- it only uses self to access get_entity.  Get_entity is a two line function the also should not be a method.  It is only called at the one place and the only reason to not put in inline would be for testing.  The code after 'else' could be made a function in a separate module and the AutoComplete import eliminated.

CallTip does not import idleConf, nor does CallTipWindow or HyperParser or PyParse.
msg253717 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-10-30 05:25
To continue: CallTips.Calltips has a similar here-or-there logic that decides where to execute the actual get-calltip-logic.  In this case,  get_entity and get_argspec are already functions rather than methods (I changed get_entity from method to function in 02b4c62ce393.) The functions could be moved into a different module with the completions function.

RemoteDebugger imports Debugger which imports WindowList, ScrolledList, and macosxSupport, all without idleConf imports.

RemoteObjectBrowser import rpc, both without idleConf.  run also imports rpc.

StackViewer imports TreeWidget and ObjectBrowser, which are not issues.  It also imports PyShell for PyShellFileList, which would be an issue, except that this is only for the htest function _stack_viewer.  I will move the test-specific import into the function, as is my current policy.

IOBinding is imported for IOBinding.encoding. The code that calculates this could be put anywhere.  This is fortunate because IOBinding imports idleConf and three tkinter modules.

Conclusion: moving about 2-300 lines of code (about 6 objects) from PyShell, AutoComplete, and IOBinding into run (currently about 400 lines) would eliminate idleConf from the user process.  I did not check tkinter imports for all the above, but it might also fix the tkinter namespace problem.
msg253718 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-10-30 05:47
The subprocess command is built in PyShell.ModifiedInterpreter method build_subprocess_arglist (l.404). It imports run and calls run.main(args).

The test for this issue would be to import run and check that sys.modules does not contain configHandler and that tkinter does not contain any of its possible submodules.
msg253720 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-10-30 06:48
New changeset a37ea1d56e98 by Terry Jan Reedy in branch '2.7':
Issue #25507: move test-specific imports to test function (idlelib.IOBinding).
https://hg.python.org/cpython/rev/a37ea1d56e98

New changeset 38b6b7253ba1 by Terry Jan Reedy in branch '3.4':
Issue #25507: move test-specific imports to test function (idlelib.IOBinding).
https://hg.python.org/cpython/rev/38b6b7253ba1
msg259032 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-01-27 16:49
Ouch.  Moving the idleConf import was a blunder.  It disabled printing in 2.7.11, 3.4.4, and 3.5.1.  When I revert, I will also augment the htest to test the printing and save-as functions.  Still have to remember to run it though.

This sort of functional test is not the main intended use of htests.  When refactoring for this issue, automated tests should be added, with mocks used to avoid consequential actions that cannot be part of a buildbot test.  For print_window, '''pipe = os.popen(command, "r")''' (https://hg.python.org/cpython/file/tip/Lib/idlelib/IOBinding.py#l463) should be replaced by '''pipe = runcommand(command)''' and 'def runcommand(command): return os.pipe(command, 'r')''' (with subprocess used instead?) added at module level.  Then runcommand can be replaced by a mock when testing, and the value of the passed command checked.
msg259033 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-01-27 16:52
New changeset 34ca24fa1b4a by Terry Jan Reedy in branch '2.7':
Issue #25507: revert incorrect movement of idleConf import in a37ea1d56e98.
https://hg.python.org/cpython/rev/34ca24fa1b4a

New changeset 86105a109830 by Terry Jan Reedy in branch '3.5':
Issue #25507: revert incorrect movement of idleConf import in c548ad75160c.
https://hg.python.org/cpython/rev/86105a109830
msg260884 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-02-25 23:25
'import tkinter; tkinter.messagebox' should also fail, but currently work in IDLE.  See https://stackoverflow.com/questions/35619027/sub-import-issues-with-tkinter-messagebox, and my answer there.
msg269957 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-07 19:15
According to https://stackoverflow.com/questions/38249755/tkinter-not-working-in-cmd-working-in-idle, this code

from tkinter import filedialog
root = Tk()

ran in 3.4.3.  After the patch for 3.5.1 (and 3.4.4, 2.7.11) it raises NameError in 3.5.2.
msg270067 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-09 20:50
https://stackoverflow.com/questions/38276691/tkinter-nameerror-only-when-running-script-from-shell  same problem with filedialog.
msg270453 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-15 01:24
The SO question and that brought the problem to my attention was in June 2015. https://stackoverflow.com/questions/30877112/tkinter-code-using-font-module-cant-run-from-command-line.  The message I just unlinked had the wrong url.
msg270466 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-07-15 06:43
New changeset 93d325c64104 by Terry Jan Reedy in branch 'default':
Issue #25507: Move 4 objects from pyshell to run and switch inports.
https://hg.python.org/cpython/rev/93d325c64104
msg270468 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-15 06:51
First step.  Patch moves 4 objects from pyshell to run and reverses run importing pyshell to pyshell importing run.  This initially failed because run imports stackbrowser and the stackbrowser import of something from pyshell failed.  Since the import is only needed for htest, I just moved it into the htest code.  Removing 'import pyshell' from run avoids 'import tkinter.messagebox' in pyshell.  It also reduces len(sys.modules) in the user process by 37 from 193 to 156.
msg270511 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-15 21:03
In #27515, Nick Coughlin said that 'del a.b', would work if sys.modules('a.b') is also deleted -- unless a.b objects to being reloaded.  This seems not a problem for the current 8 tkinter submodules.  The attached tkimports.py runs without error.

I am not going to patch 2.7, which does not have the bug, just for the import reduction.  The last patch could be applied to 3.5 since it does not remove anything from PyShell.  Putting the three warning functions in a class and making the warning global a class attribute (a future issue) would be a different matter.
msg270522 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-16 00:10
run_autocomplete.diff is a preliminary patch for moving the completion list function into run.py and reversing the imports.  However, the assert in test_autocomplete, line 103 newly fails because the call to open_completions in autocomplete_event (line 85) returns None.  The None comes from l. 155.
        comp_lists = self.fetch_completions(comp_what, mode)
        if not comp_lists[0]:
            return None
I do not yet know why moving the one function causes the completion lists for re to be blank. I am going to shelve this approach and return to deletion.
msg270590 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-07-16 22:27
New changeset 8df5200064c4 by Terry Jan Reedy in branch '3.5':
Issue #25507: IDLE no longer runs buggy code because of its tkinter imports.
https://hg.python.org/cpython/rev/8df5200064c4

New changeset af602a891892 by Terry Jan Reedy in branch 'default':
Issue #25507: Merge from 3.5 with ttk replacing colorchooser.
https://hg.python.org/cpython/rev/af602a891892
msg270595 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-17 00:17
I decided to fix this issue for both 3.5 and 3.6 by deleting the submodules both from tkinter and sys.modules (as discussed in #27515).
I used a new version of tkimports.py to check the result of user imports after the patch.  When this file loaded into IDLE and run with F5, the dir listing for each submodule, after previous import and deletion in run startup, is the same as when the file is run with "<python-path> tkimports".

I opened #27534 to continue run import reduction.  I consider 93d325c64104 and run_autocomplete.diff to be part of that issue.
msg270596 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-17 00:18
PS: the test suite run without error on my Win10, 32bit build.
msg270598 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-07-17 00:43
I am thinking about addin a test.
msg280553 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-11-10 23:43
New changeset 137c7b92360e by Terry Jan Reedy in branch '2.7':
Issue #25507: Add back import needed for 2.x encoding warning box.
https://hg.python.org/cpython/rev/137c7b92360e
msg280554 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-11-11 00:09
Import is only needed for the warning issued when someone using IDLE 2.x puts a non-ascii character into code being edited and tries to save without adding an encoding declaration.  https://stackoverflow.com/questions/40523932/idle-2-7-11-12-nameerror-global-name-toplevel-is-not-defined
History
Date User Action Args
2016-11-11 00:10:20terry.reedysetstatus: open -> closed
2016-11-11 00:09:03terry.reedysetmessages: + msg280554
2016-11-10 23:43:16python-devsetstatus: pending -> open

messages: + msg280553
2016-07-17 00:43:47terry.reedysetstatus: closed -> pending

messages: + msg270598
2016-07-17 00:18:59terry.reedysetmessages: + msg270596
2016-07-17 00:17:31terry.reedysetstatus: open -> closed
files: + tkimports.py
messages: + msg270595

resolution: fixed
stage: needs patch -> resolved
2016-07-16 22:27:26python-devsetmessages: + msg270590
2016-07-16 00:10:30terry.reedysetfiles: + run_autocomplete.diff
keywords: + patch
messages: + msg270522
2016-07-15 21:03:35terry.reedysetfiles: + tkimports.py

messages: + msg270511
versions: - Python 2.7
2016-07-15 06:51:42terry.reedysetassignee: terry.reedy
messages: + msg270468
stage: test needed -> needs patch
2016-07-15 06:43:24python-devsetmessages: + msg270466
2016-07-15 01:24:41terry.reedysetmessages: + msg270453
2016-07-15 01:20:05terry.reedysetmessages: - msg259039
2016-07-09 20:50:53terry.reedysetmessages: + msg270067
2016-07-07 19:15:58terry.reedysetmessages: + msg269957
2016-02-25 23:25:40terry.reedysetmessages: + msg260884
2016-01-27 18:09:32terry.reedysetmessages: + msg259039
2016-01-27 16:52:37python-devsetnosy: + python-dev
messages: + msg259033
2016-01-27 16:49:24terry.reedysetversions: - Python 3.4
2016-01-27 16:49:13terry.reedysetnosy: - python-dev
messages: + msg259032
2015-10-30 06:48:17python-devsetnosy: + python-dev
messages: + msg253720
2015-10-30 05:47:55terry.reedysetmessages: + msg253718
stage: needs patch -> test needed
2015-10-30 05:25:52terry.reedysetmessages: + msg253717
2015-10-30 04:15:29terry.reedysetmessages: + msg253716
components: + IDLE
2015-10-30 04:00:00terry.reedylinkissue8231 dependencies
2015-10-29 16:08:42markrosemansetmessages: + msg253683
2015-10-29 10:10:42terry.reedycreate