Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fe4513

Browse files
authored
bpo-36405: IDLE - Restore __main__ and add tests (#12518)
Fix error in commit 2b75155 noticed by Serhiy Storchaka.
1 parent 6661c17 commit 0fe4513

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

Lib/idlelib/NEWS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Released on 2019-10-20?
33
======================================
44

55

6-
bpo-36405: Use dict unpacking in idlelib and remove unneeded __main__ imports.
6+
bpo-36405: Use dict unpacking in idlelib.
77

88
bpo-36396: Remove fgBg param of idlelib.config.GetHighlight().
99
This param was only used twice and changed the return type.

Lib/idlelib/autocomplete.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Either on demand or after a user-selected delay after a key character,
44
pop up a list of candidates.
55
"""
6+
import __main__
67
import os
78
import string
89
import sys
@@ -181,7 +182,8 @@ def fetch_completions(self, what, mode):
181182
else:
182183
if mode == COMPLETE_ATTRIBUTES:
183184
if what == "":
184-
namespace = {**__builtins__.__dict__, **globals()}
185+
namespace = {**__main__.__builtins__.__dict__,
186+
**__main__.__dict__}
185187
bigl = eval("dir()", namespace)
186188
bigl.sort()
187189
if "__all__" in bigl:
@@ -216,8 +218,8 @@ def fetch_completions(self, what, mode):
216218
return smalll, bigl
217219

218220
def get_entity(self, name):
219-
"Lookup name in a namespace spanning sys.modules and globals()."
220-
return eval(name, {**sys.modules, **globals()})
221+
"Lookup name in a namespace spanning sys.modules and __main.dict__."
222+
return eval(name, {**sys.modules, **__main__.__dict__})
221223

222224

223225
AutoComplete.reload()

Lib/idlelib/calltip.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
parameter and docstring information when you type an opening parenthesis, and
55
which disappear when you type a closing parenthesis.
66
"""
7+
import __main__
78
import inspect
89
import re
910
import sys
@@ -99,10 +100,10 @@ def fetch_tip(self, expression):
99100

100101
def get_entity(expression):
101102
"""Return the object corresponding to expression evaluated
102-
in a namespace spanning sys.modules and globals().
103+
in a namespace spanning sys.modules and __main.dict__.
103104
"""
104105
if expression:
105-
namespace = {**sys.modules, **globals()}
106+
namespace = {**sys.modules, **__main__.__dict__}
106107
try:
107108
return eval(expression, namespace) # Only protect user code.
108109
except BaseException:

Lib/idlelib/idle_test/test_autocomplete.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from test.support import requires
55
from tkinter import Tk, Text
6+
import __main__
67

78
import idlelib.autocomplete as ac
89
import idlelib.autocomplete_w as acw
@@ -35,7 +36,7 @@ def tearDownClass(cls):
3536
del cls.root
3637

3738
def setUp(self):
38-
self.editor.text.delete('1.0', 'end')
39+
self.text.delete('1.0', 'end')
3940
self.autocomplete = ac.AutoComplete(self.editor)
4041

4142
def test_init(self):
@@ -132,12 +133,16 @@ def test_fetch_completions(self):
132133
# a small list containing non-private variables.
133134
# For file completion, a large list containing all files in the path,
134135
# and a small list containing files that do not start with '.'
135-
pass
136+
small, large = self.autocomplete.fetch_completions(
137+
'', ac.COMPLETE_ATTRIBUTES)
138+
self.assertLess(len(small), len(large))
139+
if __main__.__file__ != ac.__file__:
140+
self.assertNotIn('AutoComplete', small) # See issue 36405.
136141

137142
def test_get_entity(self):
138143
# Test that a name is in the namespace of sys.modules and
139144
# __main__.__dict__
140-
pass
145+
self.assertEqual(self.autocomplete.get_entity('int'), int)
141146

142147

143148
if __name__ == '__main__':
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Use dict unpacking in idlelib and remove unneeded __main__ imports.
1+
Use dict unpacking in idlelib.

0 commit comments

Comments
 (0)