Skip to content

Commit 300cf8f

Browse files
vstinnercorona10
andcommitted
gh-141510: Use frozendict in the stdlib
Co-authored-by: Donghee Na <donghee.na@python.org>
1 parent 6ef2578 commit 300cf8f

13 files changed

Lines changed: 53 additions & 49 deletions

File tree

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: 24 additions & 24 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": {
49+
_cache_format = frozendict(
50+
LOAD_GLOBAL={
5151
"counter": 1,
5252
"index": 1,
5353
"module_keys_version": 1,
5454
"builtin_keys_version": 1,
5555
},
56-
"BINARY_OP": {
56+
BINARY_OP={
5757
"counter": 1,
5858
"descr": 4,
5959
},
60-
"UNPACK_SEQUENCE": {
60+
UNPACK_SEQUENCE={
6161
"counter": 1,
6262
},
63-
"COMPARE_OP": {
63+
COMPARE_OP={
6464
"counter": 1,
6565
},
66-
"CONTAINS_OP": {
66+
CONTAINS_OP={
6767
"counter": 1,
6868
},
69-
"FOR_ITER": {
69+
FOR_ITER={
7070
"counter": 1,
7171
},
72-
"LOAD_SUPER_ATTR": {
72+
LOAD_SUPER_ATTR={
7373
"counter": 1,
7474
},
75-
"LOAD_ATTR": {
75+
LOAD_ATTR={
7676
"counter": 1,
7777
"version": 2,
7878
"keys_version": 2,
7979
"descr": 4,
8080
},
81-
"STORE_ATTR": {
81+
STORE_ATTR={
8282
"counter": 1,
8383
"version": 2,
8484
"index": 1,
8585
},
86-
"CALL": {
86+
CALL={
8787
"counter": 1,
8888
"func_version": 2,
8989
},
90-
"CALL_KW": {
90+
CALL_KW={
9191
"counter": 1,
9292
"func_version": 2,
9393
},
94-
"CALL_FUNCTION_EX": {
94+
CALL_FUNCTION_EX={
9595
"counter": 1,
9696
},
97-
"STORE_SUBSCR": {
97+
STORE_SUBSCR={
9898
"counter": 1,
9999
},
100-
"SEND": {
100+
SEND={
101101
"counter": 1,
102102
},
103-
"JUMP_BACKWARD": {
103+
JUMP_BACKWARD={
104104
"counter": 1,
105105
},
106-
"TO_BOOL": {
106+
TO_BOOL={
107107
"counter": 1,
108108
"version": 2,
109109
},
110-
"POP_JUMP_IF_TRUE": {
110+
POP_JUMP_IF_TRUE={
111111
"counter": 1,
112112
},
113-
"POP_JUMP_IF_FALSE": {
113+
POP_JUMP_IF_FALSE={
114114
"counter": 1,
115115
},
116-
"POP_JUMP_IF_NONE": {
116+
POP_JUMP_IF_NONE={
117117
"counter": 1,
118118
},
119-
"POP_JUMP_IF_NOT_NONE": {
119+
POP_JUMP_IF_NOT_NONE={
120120
"counter": 1,
121121
},
122-
}
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/stringprep.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def in_table_b1(code):
2121
return ord(code) in b1_set
2222

2323

24-
b3_exceptions = {
24+
b3_exceptions = frozendict({
2525
0xb5:'\u03bc', 0xdf:'ss', 0x130:'i\u0307', 0x149:'\u02bcn',
2626
0x17f:'s', 0x1f0:'j\u030c', 0x345:'\u03b9', 0x37a:' \u03b9',
2727
0x390:'\u03b9\u0308\u0301', 0x3b0:'\u03c5\u0308\u0301', 0x3c2:'\u03c3', 0x3d0:'\u03b2',
@@ -184,7 +184,7 @@ def in_table_b1(code):
184184
0x1d79c:'\u03bd', 0x1d79d:'\u03be', 0x1d79e:'\u03bf', 0x1d79f:'\u03c0',
185185
0x1d7a0:'\u03c1', 0x1d7a1:'\u03b8', 0x1d7a2:'\u03c3', 0x1d7a3:'\u03c4',
186186
0x1d7a4:'\u03c5', 0x1d7a5:'\u03c6', 0x1d7a6:'\u03c7', 0x1d7a7:'\u03c8',
187-
0x1d7a8:'\u03c9', 0x1d7bb:'\u03c3', }
187+
0x1d7a8:'\u03c9', 0x1d7bb:'\u03c3', })
188188

189189
def map_table_b3(code):
190190
r = b3_exceptions.get(ord(code))

0 commit comments

Comments
 (0)