]> granicus.if.org Git - python/commitdiff
Issue #10386: Added __all__ to token module; this simplifies importing
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Thu, 11 Nov 2010 14:07:41 +0000 (14:07 +0000)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Thu, 11 Nov 2010 14:07:41 +0000 (14:07 +0000)
in tokenize module and prevents leaking of private names through
import *.

Lib/token.py
Lib/tokenize.py
Misc/NEWS

index a48bf3c2927f59f62ecd3fcc3d9b47506d02a50e..7e2bfcfaf2d9a30dc6249a2d9b77007c8e373c95 100755 (executable)
@@ -1,7 +1,7 @@
-#! /usr/bin/env python3
-
 """Token constants (from "token.h")."""
 
+__all__ = ['tok_name', 'ISTERMINAL', 'ISNONTERMINAL', 'ISEOF']
+
 #  This file is automatically generated; please don't muck it up!
 #
 #  To update the symbols in this file, 'cd' to the top directory of
@@ -68,12 +68,10 @@ N_TOKENS = 55
 NT_OFFSET = 256
 #--end constants--
 
-tok_name = {}
-for _name, _value in list(globals().items()):
-    if type(_value) is type(0):
-        tok_name[_value] = _name
-del _name, _value
-
+tok_name = {value: name
+            for name, value in globals().items()
+            if isinstance(value, int)}
+__all__.extend(tok_name.values())
 
 def ISTERMINAL(x):
     return x < NT_OFFSET
@@ -85,7 +83,7 @@ def ISEOF(x):
     return x == ENDMARKER
 
 
-def main():
+def _main():
     import re
     import sys
     args = sys.argv[1:]
@@ -139,4 +137,4 @@ def main():
 
 
 if __name__ == "__main__":
-    main()
+    _main()
index 7745412edfb589d2f3b4c5be085705151f141084..506aa6a42f91ee8c5b2bb5f94296718d7c57228c 100644 (file)
@@ -33,9 +33,8 @@ from io import TextIOWrapper
 cookie_re = re.compile("coding[:=]\s*([-\w.]+)")
 
 import token
-__all__ = [x for x in dir(token) if not x.startswith("_")]
-__all__.extend(["COMMENT", "tokenize", "detect_encoding", "NL", "untokenize",
-                "ENCODING", "TokenInfo"])
+__all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding",
+                           "NL", "untokenize", "ENCODING", "TokenInfo"]
 del token
 
 COMMENT = N_TOKENS
index 7dc9b6c5ad7b246b33c6e02de33b89e2788320e8..a887fafd9fd98220fc0ab053bb41f07495595e0a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #10386: Add __all__ to token module; this simplifies importing
+  in tokenize module and prevents leaking of private names through
+  import *.
+
 - Issue #4471: Properly shutdown socket in IMAP.shutdown().  Patch by
   Lorenzo M. Catucci.