]> granicus.if.org Git - python/commitdiff
bpo-29446: tkinter 'import *' only imports what it should (GH-14864)
authorFlavian Hautbois <flavianh@sicara.com>
Fri, 26 Jul 2019 01:30:33 +0000 (03:30 +0200)
committerTerry Jan Reedy <tjreedy@udel.edu>
Fri, 26 Jul 2019 01:30:33 +0000 (21:30 -0400)
Add __all__ to tkinter.__init__ and submodules.  Replace 'import *'
with explicit imports in some submodules.

12 files changed:
Lib/tkinter/__init__.py
Lib/tkinter/colorchooser.py
Lib/tkinter/commondialog.py
Lib/tkinter/dialog.py
Lib/tkinter/dnd.py
Lib/tkinter/filedialog.py
Lib/tkinter/font.py
Lib/tkinter/messagebox.py
Lib/tkinter/scrolledtext.py
Lib/tkinter/test/test_tkinter/test_misc.py
Misc/ACKS
Misc/NEWS.d/next/Library/2019-07-19-16-06-48.bpo-29446.iXGuoi.rst [new file with mode: 0644]

index 57d5b2572822820753fc01d01fecbc2624d1fb94..9626a2780db60a4421487e51d368d2efb014ed77 100644 (file)
@@ -32,13 +32,13 @@ tk.mainloop()
 
 import enum
 import sys
+import types
 
 import _tkinter # If this fails your Python may not be configured for Tk
 TclError = _tkinter.TclError
 from tkinter.constants import *
 import re
 
-
 wantobjects = 1
 
 TkVersion = float(_tkinter.TK_VERSION)
@@ -4569,5 +4569,9 @@ def _test():
     root.mainloop()
 
 
+__all__ = [name for name, obj in globals().items()
+           if not name.startswith('_') and not isinstance(obj, types.ModuleType)
+           and name not in {'wantobjects'}]
+
 if __name__ == '__main__':
     _test()
index 9dc96713364089366b9e799e75f7317a26277fdf..3cfc06f6f1fae60e89e6027fad4dce5d15f22305 100644 (file)
@@ -21,6 +21,8 @@
 
 from tkinter.commondialog import Dialog
 
+__all__ = ["Chooser", "askcolor"]
+
 
 #
 # color chooser class
index c4ec010ee6b34e382728b15d195d9d186a0d51e7..e56b5baf7d1e12b40865a84c8027eb618cb20988 100644 (file)
@@ -8,15 +8,17 @@
 # written by Fredrik Lundh, May 1997
 #
 
-from tkinter import *
+__all__ = ["Dialog"]
+
+from tkinter import Frame
 
 
 class Dialog:
 
-    command  = None
+    command = None
 
     def __init__(self, master=None, **options):
-        self.master  = master
+        self.master = master
         self.options = options
         if not master and options.get('parent'):
             self.master = options['parent']
index cb463f717c0e4c97d982fb6f899686b8fb3ea9f3..8ae214011727cc88775aecee5dbe925a7d4e0f7a 100644 (file)
@@ -1,7 +1,8 @@
 # dialog.py -- Tkinter interface to the tk_dialog script.
 
-from tkinter import *
-from tkinter import _cnfmerge
+from tkinter import _cnfmerge, Widget, TclError, Button, Pack
+
+__all__ = ["Dialog"]
 
 DIALOG_ICON = 'questhead'
 
index 4de2331c87626122ff05d8463e95c715501cc369..3120ff342f8c0e633760fddee569365647e6baae 100644 (file)
@@ -99,9 +99,10 @@ active; it will never call dnd_commit().
 
 """
 
-
 import tkinter
 
+__all__ = ["dnd_start", "DndHandler"]
+
 
 # The factory function
 
index d9d3436145c9c9cdff78d7a62c0874bc59f094dd..dbb97dd5777e1b9997be5f3166270c0c7ad7ec40 100644 (file)
@@ -11,14 +11,20 @@ to the native file dialogues available in Tk 4.2 and newer, and the
 directory dialogue available in Tk 8.3 and newer.
 These interfaces were written by Fredrik Lundh, May 1997.
 """
+__all__ = ["FileDialog", "LoadFileDialog", "SaveFileDialog",
+           "Open", "SaveAs", "Directory",
+           "askopenfilename", "asksaveasfilename", "askopenfilenames",
+           "askopenfile", "askopenfiles", "asksaveasfile", "askdirectory"]
 
-from tkinter import *
+import fnmatch
+import os
+from tkinter import (
+    Frame, LEFT, YES, BOTTOM, Entry, TOP, Button, Tk, X,
+    Toplevel, RIGHT, Y, END, Listbox, BOTH, Scrollbar,
+)
 from tkinter.dialog import Dialog
 from tkinter import commondialog
 
-import os
-import fnmatch
-
 
 dialogstates = {}
 
index 136425726ab8ce14d2963b24d1f0f84e2e4f7659..eeff454b530655d078422b84eb69ea4d97a7158f 100644 (file)
@@ -3,11 +3,12 @@
 # written by Fredrik Lundh, February 1998
 #
 
-__version__ = "0.9"
-
 import itertools
 import tkinter
 
+__version__ = "0.9"
+__all__ = ["NORMAL", "ROMAN", "BOLD", "ITALIC",
+           "nametofont", "Font", "families", "names"]
 
 # weight/slant
 NORMAL = "normal"
index 4a711fa623b395eae6fcc7863d13e28c0def8d2c..5f0343b660c68c5a884f38ca78291caf14936231 100644 (file)
 
 from tkinter.commondialog import Dialog
 
+__all__ = ["showinfo", "showwarning", "showerror",
+           "askquestion", "askokcancel", "askyesno",
+           "askyesnocancel", "askretrycancel"]
+
 #
 # constants
 
index 749a06a6f00fd2a7df658e3dd1d9fcca15ba8a51..4f9a8815b6184b2209305c91a1402267f78e81a6 100644 (file)
@@ -11,11 +11,11 @@ Most methods calls are inherited from the Text widget; Pack, Grid and
 Place methods are redirected to the Frame widget however.
 """
 
-__all__ = ['ScrolledText']
-
 from tkinter import Frame, Text, Scrollbar, Pack, Grid, Place
 from tkinter.constants import RIGHT, LEFT, Y, BOTH
 
+__all__ = ['ScrolledText']
+
 
 class ScrolledText(Text):
     def __init__(self, master=None, **kw):
index 1d1a3c29f6bc4da24fa5ad9fadc181c74afc0bae..9d5a93ef6fb34d203312fdc2e7d301aa03951975 100644 (file)
@@ -7,6 +7,20 @@ support.requires('gui')
 
 class MiscTest(AbstractTkTest, unittest.TestCase):
 
+    def test_all(self):
+        self.assertIn("Widget", tkinter.__all__)
+        # Check that variables from tkinter.constants are also in tkinter.__all__
+        self.assertIn("CASCADE", tkinter.__all__)
+        self.assertIsNotNone(tkinter.CASCADE)
+        # Check that sys, re, and constants are not in tkinter.__all__
+        self.assertNotIn("re", tkinter.__all__)
+        self.assertNotIn("sys", tkinter.__all__)
+        self.assertNotIn("constants", tkinter.__all__)
+        # Check that an underscored functions is not in tkinter.__all__
+        self.assertNotIn("_tkerror", tkinter.__all__)
+        # Check that wantobjects is not in tkinter.__all__
+        self.assertNotIn("wantobjects", tkinter.__all__)
+
     def test_repr(self):
         t = tkinter.Toplevel(self.root, name='top')
         f = tkinter.Frame(t, name='child')
index b062855b9e28990e62e8c1f5bdcf42b405d40d22..e02e8e1fa5155d6fb319dca67ed6d1621e0b1920 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -649,6 +649,7 @@ Zac Hatfield-Dodds
 Shane Hathaway
 Michael Haubenwallner
 Janko Hauser
+Flavian Hautbois
 Rycharde Hawkes
 Ben Hayden
 Jochen Hayek
diff --git a/Misc/NEWS.d/next/Library/2019-07-19-16-06-48.bpo-29446.iXGuoi.rst b/Misc/NEWS.d/next/Library/2019-07-19-16-06-48.bpo-29446.iXGuoi.rst
new file mode 100644 (file)
index 0000000..9afda7e
--- /dev/null
@@ -0,0 +1 @@
+Make `from tkinter import *` import only the expected objects.
\ No newline at end of file