Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit baefbb2

Browse filesBrowse files
authored
gh-103737: IDLE - Remove unneeded .keys() for dict iteration (#110960)
Add comments where .keys() is needed. Leave debugger usages along because situation is unclear as indicated in expanded comment. Most testing is manual.
1 parent 77dbd95 commit baefbb2
Copy full SHA for baefbb2

File tree

Expand file treeCollapse file tree

8 files changed

+28
-26
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+28
-26
lines changed

‎Lib/idlelib/config.py

Copy file name to clipboardExpand all lines: Lib/idlelib/config.py
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ def GetCoreKeys(self, keySetName=None):
597597
problem getting any core binding there will be an 'ultimate last
598598
resort fallback' to the CUA-ish bindings defined here.
599599
"""
600+
# TODO: = dict(sorted([(v-event, keys), ...]))?
600601
keyBindings={
602+
# vitual-event: list of key events.
601603
'<<copy>>': ['<Control-c>', '<Control-C>'],
602604
'<<cut>>': ['<Control-x>', '<Control-X>'],
603605
'<<paste>>': ['<Control-v>', '<Control-V>'],
@@ -880,7 +882,7 @@ def _dump(): # htest # (not really, but ignore in coverage)
880882
line, crc = 0, 0
881883

882884
def sprint(obj):
883-
global line, crc
885+
nonlocal line, crc
884886
txt = str(obj)
885887
line += 1
886888
crc = crc32(txt.encode(encoding='utf-8'), crc)
@@ -889,7 +891,7 @@ def sprint(obj):
889891

890892
def dumpCfg(cfg):
891893
print('\n', cfg, '\n') # Cfg has variable '0xnnnnnnnn' address.
892-
for key in sorted(cfg.keys()):
894+
for key in sorted(cfg):
893895
sections = cfg[key].sections()
894896
sprint(key)
895897
sprint(sections)
@@ -908,4 +910,6 @@ def dumpCfg(cfg):
908910
from unittest import main
909911
main('idlelib.idle_test.test_config', verbosity=2, exit=False)
910912

911-
# Run revised _dump() as htest?
913+
_dump()
914+
# Run revised _dump() (700+ lines) as htest? More sorting.
915+
# Perhaps as window with tabs for textviews, making it config viewer.

‎Lib/idlelib/configdialog.py

Copy file name to clipboardExpand all lines: Lib/idlelib/configdialog.py
+9-13Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,8 @@ def help(self):
211211
contents=help_common+help_pages.get(page, ''))
212212

213213
def deactivate_current_config(self):
214-
"""Remove current key bindings.
215-
Iterate over window instances defined in parent and remove
216-
the keybindings.
217-
"""
218-
# Before a config is saved, some cleanup of current
219-
# config must be done - remove the previous keybindings.
220-
win_instances = self.parent.instance_dict.keys()
221-
for instance in win_instances:
214+
"""Remove current key bindings in current windows."""
215+
for instance in self.parent.instance_dict:
222216
instance.RemoveKeybindings()
223217

224218
def activate_config_changes(self):
@@ -227,8 +221,7 @@ def activate_config_changes(self):
227221
Dynamically update the current parent window instances
228222
with some of the configuration changes.
229223
"""
230-
win_instances = self.parent.instance_dict.keys()
231-
for instance in win_instances:
224+
for instance in self.parent.instance_dict:
232225
instance.ResetColorizer()
233226
instance.ResetFont()
234227
instance.set_notabs_indentwidth()
@@ -583,6 +576,8 @@ def create_page_highlight(self):
583576
(*)theme_message: Label
584577
"""
585578
self.theme_elements = {
579+
# Display_name: ('internal_name, sort_number').
580+
# TODO: remove sort_number unneeded with dict ordering.
586581
'Normal Code or Text': ('normal', '00'),
587582
'Code Context': ('context', '01'),
588583
'Python Keywords': ('keyword', '02'),
@@ -765,7 +760,7 @@ def load_theme_cfg(self):
765760
self.builtinlist.SetMenu(item_list, item_list[0])
766761
self.set_theme_type()
767762
# Load theme element option menu.
768-
theme_names = list(self.theme_elements.keys())
763+
theme_names = list(self.theme_elements)
769764
theme_names.sort(key=lambda x: self.theme_elements[x][1])
770765
self.targetlist.SetMenu(theme_names, theme_names[0])
771766
self.paint_theme_sample()
@@ -1477,12 +1472,13 @@ def load_keys_list(self, keyset_name):
14771472
reselect = True
14781473
list_index = self.bindingslist.index(ANCHOR)
14791474
keyset = idleConf.GetKeySet(keyset_name)
1480-
bind_names = list(keyset.keys())
1475+
# 'set' is dict mapping virtual event to list of key events.
1476+
bind_names = list(keyset)
14811477
bind_names.sort()
14821478
self.bindingslist.delete(0, END)
14831479
for bind_name in bind_names:
14841480
key = ' '.join(keyset[bind_name])
1485-
bind_name = bind_name[2:-2] # Trim off the angle brackets.
1481+
bind_name = bind_name[2:-2] # Trim double angle brackets.
14861482
if keyset_name in changes['keys']:
14871483
# Handle any unsaved changes to this key set.
14881484
if bind_name in changes['keys'][keyset_name]:

‎Lib/idlelib/debugger.py

Copy file name to clipboardExpand all lines: Lib/idlelib/debugger.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def load_dict(self, dict, force=0, rpc_client=None):
509509
# There is also an obscure bug in sorted(dict) where the
510510
# interpreter gets into a loop requesting non-existing dict[0],
511511
# dict[1], dict[2], etc from the debugger_r.DictProxy.
512-
###
512+
# TODO recheck above; see debugger_r 159ff, debugobj 60.
513513
keys_list = dict.keys()
514514
names = sorted(keys_list)
515515
###

‎Lib/idlelib/debugobj.py

Copy file name to clipboardExpand all lines: Lib/idlelib/debugobj.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def setfunction(value, key=key, object=self.object):
9393

9494
class DictTreeItem(SequenceTreeItem):
9595
def keys(self):
96-
keys = list(self.object.keys())
96+
# TODO return sorted(self.object)
97+
keys = list(self.object)
9798
try:
9899
keys.sort()
99100
except:

‎Lib/idlelib/idle_test/test_config.py

Copy file name to clipboardExpand all lines: Lib/idlelib/idle_test/test_config.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ def test_create_config_handlers(self):
274274
conf.CreateConfigHandlers()
275275

276276
# Check keys are equal
277-
self.assertCountEqual(conf.defaultCfg.keys(), conf.config_types)
278-
self.assertCountEqual(conf.userCfg.keys(), conf.config_types)
277+
self.assertCountEqual(conf.defaultCfg, conf.config_types)
278+
self.assertCountEqual(conf.userCfg, conf.config_types)
279279

280280
# Check conf parser are correct type
281281
for default_parser in conf.defaultCfg.values():

‎Lib/idlelib/idle_test/test_debugobj.py

Copy file name to clipboardExpand all lines: Lib/idlelib/idle_test/test_debugobj.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_isexpandable(self):
3737

3838
def test_keys(self):
3939
ti = debugobj.SequenceTreeItem('label', 'abc')
40-
self.assertEqual(list(ti.keys()), [0, 1, 2])
40+
self.assertEqual(list(ti.keys()), [0, 1, 2]) # keys() is a range.
4141

4242

4343
class DictTreeItemTest(unittest.TestCase):
@@ -50,7 +50,7 @@ def test_isexpandable(self):
5050

5151
def test_keys(self):
5252
ti = debugobj.DictTreeItem('label', {1:1, 0:0, 2:2})
53-
self.assertEqual(ti.keys(), [0, 1, 2])
53+
self.assertEqual(ti.keys(), [0, 1, 2]) # keys() is a sorted list.
5454

5555

5656
if __name__ == '__main__':

‎Lib/idlelib/pyshell.py

Copy file name to clipboardExpand all lines: Lib/idlelib/pyshell.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,11 @@ def showtraceback(self):
747747
self.tkconsole.open_stack_viewer()
748748

749749
def checklinecache(self):
750-
c = linecache.cache
751-
for key in list(c.keys()):
750+
"Remove keys other than '<pyshell#n>'."
751+
cache = linecache.cache
752+
for key in list(cache): # Iterate list because mutate cache.
752753
if key[:1] + key[-1:] != "<>":
753-
del c[key]
754+
del cache[key]
754755

755756
def runcommand(self, code):
756757
"Run the code without invoking the debugger"

‎Lib/idlelib/stackviewer.py

Copy file name to clipboardExpand all lines: Lib/idlelib/stackviewer.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def IsExpandable(self):
9999

100100
def GetSubList(self):
101101
sublist = []
102-
for key in self.object.keys():
102+
for key in self.object.keys(): # self.object not necessarily dict.
103103
try:
104104
value = self.object[key]
105105
except KeyError:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.