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

Skip to content

Commit b9d10d0

Browse files
committed
Issue #10386: Added __all__ to token module; this simplifies importing
in tokenize module and prevents leaking of private names through import *.
1 parent bb27c12 commit b9d10d0

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

Lib/token.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#! /usr/bin/env python3
2-
31
"""Token constants (from "token.h")."""
42

3+
__all__ = ['tok_name', 'ISTERMINAL', 'ISNONTERMINAL', 'ISEOF']
4+
55
# This file is automatically generated; please don't muck it up!
66
#
77
# To update the symbols in this file, 'cd' to the top directory of
@@ -68,12 +68,10 @@
6868
NT_OFFSET = 256
6969
#--end constants--
7070

71-
tok_name = {}
72-
for _name, _value in list(globals().items()):
73-
if type(_value) is type(0):
74-
tok_name[_value] = _name
75-
del _name, _value
76-
71+
tok_name = {value: name
72+
for name, value in globals().items()
73+
if isinstance(value, int)}
74+
__all__.extend(tok_name.values())
7775

7876
def ISTERMINAL(x):
7977
return x < NT_OFFSET
@@ -85,7 +83,7 @@ def ISEOF(x):
8583
return x == ENDMARKER
8684

8785

88-
def main():
86+
def _main():
8987
import re
9088
import sys
9189
args = sys.argv[1:]
@@ -139,4 +137,4 @@ def main():
139137

140138

141139
if __name__ == "__main__":
142-
main()
140+
_main()

Lib/tokenize.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
cookie_re = re.compile("coding[:=]\s*([-\w.]+)")
3434

3535
import token
36-
__all__ = [x for x in dir(token) if not x.startswith("_")]
37-
__all__.extend(["COMMENT", "tokenize", "detect_encoding", "NL", "untokenize",
38-
"ENCODING", "TokenInfo"])
36+
__all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding",
37+
"NL", "untokenize", "ENCODING", "TokenInfo"]
3938
del token
4039

4140
COMMENT = N_TOKENS

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Core and Builtins
6363
Library
6464
-------
6565

66+
- Issue #10386: Add __all__ to token module; this simplifies importing
67+
in tokenize module and prevents leaking of private names through
68+
import *.
69+
6670
- Issue #4471: Properly shutdown socket in IMAP.shutdown(). Patch by
6771
Lorenzo M. Catucci.
6872

0 commit comments

Comments
 (0)