[Python-checkins] bpo-36405: IDLE - Restore __main__ and add tests (GH-12518)

Miss Islington (bot) webhook-mailer at python.org
Sun Mar 24 17:32:43 EDT 2019


https://github.com/python/cpython/commit/2b580146a53311e4202b0be63040740cdc01f1f5
commit: 2b580146a53311e4202b0be63040740cdc01f1f5
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-03-24T14:32:40-07:00
summary:

bpo-36405: IDLE - Restore __main__ and add tests (GH-12518)


Fix error in commit 2b75155 noticed by Serhiy Storchaka.
(cherry picked from commit 0fe4513d9a5510ae91c0da7eb0433f79a6d4dda9)

Co-authored-by: Terry Jan Reedy <tjreedy at udel.edu>

files:
M Lib/idlelib/NEWS.txt
M Lib/idlelib/autocomplete.py
M Lib/idlelib/calltip.py
M Lib/idlelib/idle_test/test_autocomplete.py
M Misc/NEWS.d/next/IDLE/2019-03-23-01-45-56.bpo-36405.m7Wv1F.rst

diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index 9ae2b0a97239..f136810e7a86 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -3,7 +3,7 @@ Released on 2019-??-??
 ======================================
 
 
-bpo-36405: Use dict unpacking in idlelib and remove unneeded __main__ imports.
+bpo-36405: Use dict unpacking in idlelib.
 
 bpo-36396: Remove fgBg param of idlelib.config.GetHighlight().
 This param was only used twice and changed the return type.
diff --git a/Lib/idlelib/autocomplete.py b/Lib/idlelib/autocomplete.py
index 6751928f0450..d57e9c9000fa 100644
--- a/Lib/idlelib/autocomplete.py
+++ b/Lib/idlelib/autocomplete.py
@@ -3,6 +3,7 @@
 Either on demand or after a user-selected delay after a key character,
 pop up a list of candidates.
 """
+import __main__
 import os
 import string
 import sys
@@ -181,7 +182,8 @@ def fetch_completions(self, what, mode):
         else:
             if mode == COMPLETE_ATTRIBUTES:
                 if what == "":
-                    namespace = {**__builtins__.__dict__, **globals()}
+                    namespace = {**__main__.__builtins__.__dict__,
+                                 **__main__.__dict__}
                     bigl = eval("dir()", namespace)
                     bigl.sort()
                     if "__all__" in bigl:
@@ -216,8 +218,8 @@ def fetch_completions(self, what, mode):
             return smalll, bigl
 
     def get_entity(self, name):
-        "Lookup name in a namespace spanning sys.modules and globals()."
-        return eval(name, {**sys.modules, **globals()})
+        "Lookup name in a namespace spanning sys.modules and __main.dict__."
+        return eval(name, {**sys.modules, **__main__.__dict__})
 
 
 AutoComplete.reload()
diff --git a/Lib/idlelib/calltip.py b/Lib/idlelib/calltip.py
index 4b78917d7d98..b013a7f6ec0f 100644
--- a/Lib/idlelib/calltip.py
+++ b/Lib/idlelib/calltip.py
@@ -4,6 +4,7 @@
 parameter and docstring information when you type an opening parenthesis, and
 which disappear when you type a closing parenthesis.
 """
+import __main__
 import inspect
 import re
 import sys
@@ -99,10 +100,10 @@ def fetch_tip(self, expression):
 
 def get_entity(expression):
     """Return the object corresponding to expression evaluated
-    in a namespace spanning sys.modules and globals().
+    in a namespace spanning sys.modules and __main.dict__.
     """
     if expression:
-        namespace = {**sys.modules, **globals()}
+        namespace = {**sys.modules, **__main__.__dict__}
         try:
             return eval(expression, namespace)  # Only protect user code.
         except BaseException:
diff --git a/Lib/idlelib/idle_test/test_autocomplete.py b/Lib/idlelib/idle_test/test_autocomplete.py
index d7ee00af94b4..89a9ed11df40 100644
--- a/Lib/idlelib/idle_test/test_autocomplete.py
+++ b/Lib/idlelib/idle_test/test_autocomplete.py
@@ -3,6 +3,7 @@
 import unittest
 from test.support import requires
 from tkinter import Tk, Text
+import __main__
 
 import idlelib.autocomplete as ac
 import idlelib.autocomplete_w as acw
@@ -35,7 +36,7 @@ def tearDownClass(cls):
         del cls.root
 
     def setUp(self):
-        self.editor.text.delete('1.0', 'end')
+        self.text.delete('1.0', 'end')
         self.autocomplete = ac.AutoComplete(self.editor)
 
     def test_init(self):
@@ -132,12 +133,16 @@ def test_fetch_completions(self):
         # a small list containing non-private variables.
         # For file completion, a large list containing all files in the path,
         # and a small list containing files that do not start with '.'
-        pass
+        small, large = self.autocomplete.fetch_completions(
+                '', ac.COMPLETE_ATTRIBUTES)
+        self.assertLess(len(small), len(large))
+        if __main__.__file__ != ac.__file__:
+            self.assertNotIn('AutoComplete', small)  # See issue 36405.
 
     def test_get_entity(self):
         # Test that a name is in the namespace of sys.modules and
         # __main__.__dict__
-        pass
+        self.assertEqual(self.autocomplete.get_entity('int'), int)
 
 
 if __name__ == '__main__':
diff --git a/Misc/NEWS.d/next/IDLE/2019-03-23-01-45-56.bpo-36405.m7Wv1F.rst b/Misc/NEWS.d/next/IDLE/2019-03-23-01-45-56.bpo-36405.m7Wv1F.rst
index 619ab6af80c9..bef438d5a5a7 100644
--- a/Misc/NEWS.d/next/IDLE/2019-03-23-01-45-56.bpo-36405.m7Wv1F.rst
+++ b/Misc/NEWS.d/next/IDLE/2019-03-23-01-45-56.bpo-36405.m7Wv1F.rst
@@ -1 +1 @@
-Use dict unpacking in idlelib and remove unneeded __main__ imports.
+Use dict unpacking in idlelib.



More information about the Python-checkins mailing list