Commits

Alexander Belopolsky committed 98bd3ca

Issue #10386: Added __all__ to token module; this simplifies importing
in tokenize module and prevents leaking of private names through
import *.

Comments (0)

Files changed (3)

-#! /usr/bin/env python3
+"""Token constants (from "token.h")."""
 
-"""Token constants (from "token.h")."""
+__all__ = ['tok_name', 'ISTERMINAL', 'ISNONTERMINAL', 'ISEOF']
 
 #  This file is automatically generated; please don't muck it up!
 #
 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
     return x == ENDMARKER
 
 
-def main():
+def _main():
     import re
     import sys
     args = sys.argv[1:]
 
 
 if __name__ == "__main__":
-    main()
+    _main()
 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
 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.