Skip to content

Commit 349639c

Browse files
vstinnercorona10
andauthored
gh-141510: Use frozendict in the stdlib (#144909)
Co-authored-by: Donghee Na <donghee.na@python.org>
1 parent 4fce98a commit 349639c

File tree

11 files changed

+101
-97
lines changed

11 files changed

+101
-97
lines changed

Lib/functools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def _lt_from_ge(self, other):
170170
return op_result
171171
return not op_result
172172

173-
_convert = {
173+
_convert = frozendict({
174174
'__lt__': [('__gt__', _gt_from_lt),
175175
('__le__', _le_from_lt),
176176
('__ge__', _ge_from_lt)],
@@ -183,7 +183,7 @@ def _lt_from_ge(self, other):
183183
'__ge__': [('__le__', _le_from_ge),
184184
('__gt__', _gt_from_ge),
185185
('__lt__', _lt_from_ge)]
186-
}
186+
})
187187

188188
def total_ordering(cls):
189189
"""Class decorator that fills in missing ordering methods"""

Lib/gettext.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ def _error(value):
111111
('+', '-'),
112112
('*', '/', '%'),
113113
)
114-
_binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops}
115-
_c2py_ops = {'||': 'or', '&&': 'and', '/': '//'}
114+
_binary_ops = frozendict({op: i for i, ops in enumerate(_binary_ops, 1)
115+
for op in ops})
116+
_c2py_ops = frozendict({'||': 'or', '&&': 'and', '/': '//'})
116117

117118

118119
def _parse(tokens, priority=-1):

Lib/json/decoder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ def __reduce__(self):
4343
return self.__class__, (self.msg, self.doc, self.pos)
4444

4545

46-
_CONSTANTS = {
46+
_CONSTANTS = frozendict({
4747
'-Infinity': NegInf,
4848
'Infinity': PosInf,
4949
'NaN': NaN,
50-
}
50+
})
5151

5252

5353
HEXDIGITS = re.compile(r'[0-9A-Fa-f]{4}', FLAGS)

Lib/json/tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
(?P<null>null)
2323
''', re.VERBOSE)
2424

25-
_group_to_theme_color = {
25+
_group_to_theme_color = frozendict({
2626
"key": "definition",
2727
"string": "string",
2828
"number": "number",
2929
"boolean": "keyword",
3030
"null": "keyword",
31-
}
31+
})
3232

3333

3434
def _colorize_json(json_str, theme):

Lib/opcode.py

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -46,81 +46,81 @@
4646

4747
hascompare = [opmap["COMPARE_OP"]]
4848

49-
_cache_format = {
50-
"LOAD_GLOBAL": {
51-
"counter": 1,
52-
"index": 1,
53-
"module_keys_version": 1,
54-
"builtin_keys_version": 1,
55-
},
56-
"BINARY_OP": {
57-
"counter": 1,
58-
"descr": 4,
59-
},
60-
"UNPACK_SEQUENCE": {
61-
"counter": 1,
62-
},
63-
"COMPARE_OP": {
64-
"counter": 1,
65-
},
66-
"CONTAINS_OP": {
67-
"counter": 1,
68-
},
69-
"FOR_ITER": {
70-
"counter": 1,
71-
},
72-
"LOAD_SUPER_ATTR": {
73-
"counter": 1,
74-
},
75-
"LOAD_ATTR": {
76-
"counter": 1,
77-
"version": 2,
78-
"keys_version": 2,
79-
"descr": 4,
80-
},
81-
"STORE_ATTR": {
82-
"counter": 1,
83-
"version": 2,
84-
"index": 1,
85-
},
86-
"CALL": {
87-
"counter": 1,
88-
"func_version": 2,
89-
},
90-
"CALL_KW": {
91-
"counter": 1,
92-
"func_version": 2,
93-
},
94-
"CALL_FUNCTION_EX": {
95-
"counter": 1,
96-
},
97-
"STORE_SUBSCR": {
98-
"counter": 1,
99-
},
100-
"SEND": {
101-
"counter": 1,
102-
},
103-
"JUMP_BACKWARD": {
104-
"counter": 1,
105-
},
106-
"TO_BOOL": {
107-
"counter": 1,
108-
"version": 2,
109-
},
110-
"POP_JUMP_IF_TRUE": {
111-
"counter": 1,
112-
},
113-
"POP_JUMP_IF_FALSE": {
114-
"counter": 1,
115-
},
116-
"POP_JUMP_IF_NONE": {
117-
"counter": 1,
118-
},
119-
"POP_JUMP_IF_NOT_NONE": {
120-
"counter": 1,
121-
},
122-
}
49+
_cache_format = frozendict(
50+
LOAD_GLOBAL=frozendict(
51+
counter=1,
52+
index=1,
53+
module_keys_version=1,
54+
builtin_keys_version=1,
55+
),
56+
BINARY_OP=frozendict(
57+
counter=1,
58+
descr=4,
59+
),
60+
UNPACK_SEQUENCE=frozendict(
61+
counter=1,
62+
),
63+
COMPARE_OP=frozendict(
64+
counter=1,
65+
),
66+
CONTAINS_OP=frozendict(
67+
counter=1,
68+
),
69+
FOR_ITER=frozendict(
70+
counter=1,
71+
),
72+
LOAD_SUPER_ATTR=frozendict(
73+
counter=1,
74+
),
75+
LOAD_ATTR=frozendict(
76+
counter=1,
77+
version=2,
78+
keys_version=2,
79+
descr=4,
80+
),
81+
STORE_ATTR=frozendict(
82+
counter=1,
83+
version=2,
84+
index=1,
85+
),
86+
CALL=frozendict(
87+
counter=1,
88+
func_version=2,
89+
),
90+
CALL_KW=frozendict(
91+
counter=1,
92+
func_version=2,
93+
),
94+
CALL_FUNCTION_EX=frozendict(
95+
counter=1,
96+
),
97+
STORE_SUBSCR=frozendict(
98+
counter=1,
99+
),
100+
SEND=frozendict(
101+
counter=1,
102+
),
103+
JUMP_BACKWARD=frozendict(
104+
counter=1,
105+
),
106+
TO_BOOL=frozendict(
107+
counter=1,
108+
version=2,
109+
),
110+
POP_JUMP_IF_TRUE=frozendict(
111+
counter=1,
112+
),
113+
POP_JUMP_IF_FALSE=frozendict(
114+
counter=1,
115+
),
116+
POP_JUMP_IF_NONE=frozendict(
117+
counter=1,
118+
),
119+
POP_JUMP_IF_NOT_NONE=frozendict(
120+
counter=1,
121+
),
122+
)
123123

124-
_inline_cache_entries = {
124+
_inline_cache_entries = frozendict({
125125
name : sum(value.values()) for (name, value) in _cache_format.items()
126-
}
126+
})

Lib/optparse.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,12 @@ def _parse_num(val, type):
407407
def _parse_int(val):
408408
return _parse_num(val, int)
409409

410-
_builtin_cvt = { "int" : (_parse_int, _("integer")),
411-
"long" : (_parse_int, _("integer")),
412-
"float" : (float, _("floating-point")),
413-
"complex" : (complex, _("complex")) }
410+
_builtin_cvt = frozendict({
411+
"int": (_parse_int, _("integer")),
412+
"long": (_parse_int, _("integer")),
413+
"float": (float, _("floating-point")),
414+
"complex": (complex, _("complex")),
415+
})
414416

415417
def check_builtin(option, opt, value):
416418
(cvt, what) = _builtin_cvt[option.type]

Lib/platform.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
# Based on the description of the PHP's version_compare():
128128
# http://php.net/manual/en/function.version-compare.php
129129

130-
_ver_stages = {
130+
_ver_stages = frozendict({
131131
# any string not found in this dict, will get 0 assigned
132132
'dev': 10,
133133
'alpha': 20, 'a': 20,
@@ -136,7 +136,7 @@
136136
'RC': 50, 'rc': 50,
137137
# number, will get 100 assigned
138138
'pl': 200, 'p': 200,
139-
}
139+
})
140140

141141

142142
def _comparable_version(version):
@@ -705,11 +705,11 @@ def _syscmd_file(target, default=''):
705705

706706
# Default values for architecture; non-empty strings override the
707707
# defaults given as parameters
708-
_default_architecture = {
708+
_default_architecture = frozendict({
709709
'win32': ('', 'WindowsPE'),
710710
'win16': ('', 'Windows'),
711711
'dos': ('', 'MSDOS'),
712-
}
712+
})
713713

714714
def architecture(executable=sys.executable, bits='', linkage=''):
715715

Lib/plistlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class InvalidFileException (ValueError):
453453
def __init__(self, message="Invalid file"):
454454
ValueError.__init__(self, message)
455455

456-
_BINARY_FORMAT = {1: 'B', 2: 'H', 4: 'L', 8: 'Q'}
456+
_BINARY_FORMAT = frozendict({1: 'B', 2: 'H', 4: 'L', 8: 'Q'})
457457

458458
_undefined = object()
459459

Lib/ssl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@
150150
source=_ssl)
151151

152152
PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_TLS
153-
_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
153+
_PROTOCOL_NAMES = frozendict({
154+
value: name for name, value in _SSLMethod.__members__.items()})
154155

155156
_SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None)
156157

Lib/symtable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def get_namespace(self):
414414
_flags = [('USE', USE)]
415415
_flags.extend(kv for kv in globals().items() if kv[0].startswith('DEF_'))
416416
_scopes_names = ('FREE', 'LOCAL', 'GLOBAL_IMPLICIT', 'GLOBAL_EXPLICIT', 'CELL')
417-
_scopes_value_to_name = {globals()[n]: n for n in _scopes_names}
417+
_scopes_value_to_name = frozendict({globals()[n]: n for n in _scopes_names})
418418

419419

420420
def main(args):

0 commit comments

Comments
 (0)