This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: tkinter.ttk._val_or_dict assumes tkinter._default_root exists
Type: behavior Stage: resolved
Components: Library (Lib), Tkinter Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: gpolo, python-dev, serhiy.storchaka, terry.reedy, zach.ware
Priority: normal Keywords: patch

Created on 2014-04-30 19:22 by Zero, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ttk_val_or_dict.patch serhiy.storchaka, 2014-05-24 20:31 review
Messages (13)
msg217644 - (view) Author: Stephen Paul Chappell (Zero) Date: 2014-04-30 19:22
If a call is made to tkinter.NoDefaultRoot, then calls to tkinter.ttk._val_or_dict may fail. NoDefaultRoot ends with "del _default_root" (line 174) and removes the variable from the module's namespace. _val_or_dict can try to access this variable but assumes that it exists (line 319). If _default_root does not exist, the function will raise an AttributeError: 'module' object has no attribute '_default_root'.
msg217645 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-04-30 19:26
Can you provide an example of when this happens?  Note that tkinter.ttk._val_or_dict is a private function and should not be called by user code.
msg217656 - (view) Author: Stephen Paul Chappell (Zero) Date: 2014-04-30 21:14
I discovered the problem when trying to run the program listed at
http://code.activestate.com/recipes/577633/ (Directory Pruner 2).
msg217807 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-05-02 23:06
A reference to a non-existent attribute fails no matter who calls the function. Stephen, can you suggest a patch, perhaps based on code in other functions that try to access _default_root?
msg218485 - (view) Author: Stephen Paul Chappell (Zero) Date: 2014-05-13 21:07
It seems that most functions and methods do not bother checking if _default_root exists before trying to access it. However, upon seeing line 366 in ttk (if tkinter._support_default_root:), my recommendation would be to change line 319 to "if tkinter._support_default_root and tkinter._default_root:" to fix the problem. Several similar lines appear in tkinter\__init__.py at locations 1835, 1845, and 2050.
msg218505 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-05-14 06:44
This is not so easy, because it does not work with wantobject=False and can not work with future versions of Tk. Actually we should use self.tk instead of _default_root, but this will change the signature of _list_from_layouttuple() and _val_or_dict(). Due to the risk of breaking existing user code which uses these private functions, they should be left with old signature and new functions should be added.
msg218527 - (view) Author: Stephen Paul Chappell (Zero) Date: 2014-05-14 13:20
There is discussion of removing wantobjects in issue3015. If it gets removed, a better patch might be created for future versions of tkinter. However, accessing self.tk would probably be a good replacement for anywhere tkinter._default_root can be found (except in cases where a widget is not given a parent).
msg218561 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-05-14 18:24
>Due to the risk of breaking existing user code which uses these private functions, they should be left with old signature and new functions should be added.

The reason we make things private is so we don't have to do this. Why would you make an exception here?
msg218563 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-05-14 18:41
Because there are explicit tests for these private functions.
msg218564 - (view) Author: Stephen Paul Chappell (Zero) Date: 2014-05-14 18:44
>Because there are explicit tests for these private functions.

Does that not mean that both the functions and their explicit tests should be changed?
msg219053 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-05-24 20:31
Well, if we allow to change signature of theses functions, here is a patch which fixes _list_from_layouttuple and _val_or_dict and rewrites tests. Mocked TkApp is used because test_functions should run without the gui resource enabled.
msg219280 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-05-28 15:44
New changeset 8e5c65719c12 by Serhiy Storchaka in branch '2.7':
Issue #21402: tkinter.ttk now works when default root window is not set.
http://hg.python.org/cpython/rev/8e5c65719c12

New changeset 4a0987b8f630 by Serhiy Storchaka in branch '3.4':
Issue #21402: tkinter.ttk now works when default root window is not set.
http://hg.python.org/cpython/rev/4a0987b8f630

New changeset c112a91ad609 by Serhiy Storchaka in branch 'default':
Issue #21402: tkinter.ttk now works when default root window is not set.
http://hg.python.org/cpython/rev/c112a91ad609
msg219282 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-05-28 15:51
Thank you for your report Stephen.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65601
2019-10-31 14:23:06Zerosetnosy: - Zero
2014-05-28 15:51:56serhiy.storchakasetmessages: + msg219282
2014-05-28 15:51:18serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2014-05-28 15:44:16python-devsetnosy: + python-dev
messages: + msg219280
2014-05-24 20:32:02serhiy.storchakasetstage: test needed -> patch review
2014-05-24 20:31:30serhiy.storchakasetfiles: + ttk_val_or_dict.patch
keywords: + patch
messages: + msg219053
2014-05-14 18:44:43Zerosetmessages: + msg218564
2014-05-14 18:41:01serhiy.storchakasetmessages: + msg218563
2014-05-14 18:24:56terry.reedysetmessages: + msg218561
2014-05-14 13:20:45Zerosetmessages: + msg218527
2014-05-14 06:44:40serhiy.storchakasetassignee: serhiy.storchaka
messages: + msg218505
versions: + Python 2.7, Python 3.5
2014-05-13 21:07:56Zerosetmessages: + msg218485
2014-05-02 23:06:39terry.reedysetnosy: + terry.reedy
messages: + msg217807
2014-04-30 21:14:00Zerosetmessages: + msg217656
2014-04-30 19:26:45zach.waresetnosy: + gpolo, serhiy.storchaka, zach.ware
messages: + msg217645

type: crash -> behavior
stage: test needed
2014-04-30 19:22:43Zerocreate