]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141510: Use frozendict in the stdlib (#144909)
authorVictor Stinner <vstinner@python.org>
Fri, 6 Mar 2026 09:25:09 +0000 (10:25 +0100)
committerGitHub <noreply@github.com>
Fri, 6 Mar 2026 09:25:09 +0000 (10:25 +0100)
Co-authored-by: Donghee Na <donghee.na@python.org>
Lib/functools.py
Lib/gettext.py
Lib/json/decoder.py
Lib/json/tool.py
Lib/opcode.py
Lib/optparse.py
Lib/platform.py
Lib/plistlib.py
Lib/ssl.py
Lib/symtable.py
Lib/tarfile.py

index 9bc2ee7e8c894c3f0232579a84e4041cd6762629..cd374631f167925b9a495a2d6e6d75d910a80ebb 100644 (file)
@@ -170,7 +170,7 @@ def _lt_from_ge(self, other):
         return op_result
     return not op_result
 
-_convert = {
+_convert = frozendict({
     '__lt__': [('__gt__', _gt_from_lt),
                ('__le__', _le_from_lt),
                ('__ge__', _ge_from_lt)],
@@ -183,7 +183,7 @@ _convert = {
     '__ge__': [('__le__', _le_from_ge),
                ('__gt__', _gt_from_ge),
                ('__lt__', _lt_from_ge)]
-}
+})
 
 def total_ordering(cls):
     """Class decorator that fills in missing ordering methods"""
index 6c11ab2b1eb57091affba895a6fe8bc34de84ecc..2f77f0e849e9aeb40f3fec748537020243f012ca 100644 (file)
@@ -111,8 +111,9 @@ _binary_ops = (
     ('+', '-'),
     ('*', '/', '%'),
 )
-_binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops}
-_c2py_ops = {'||': 'or', '&&': 'and', '/': '//'}
+_binary_ops = frozendict({op: i for i, ops in enumerate(_binary_ops, 1)
+                          for op in ops})
+_c2py_ops = frozendict({'||': 'or', '&&': 'and', '/': '//'})
 
 
 def _parse(tokens, priority=-1):
index 92ad635255764098c0feb5aff1a3f4584d92fd90..4cd6f8367a1349cd82433aa70f7d2ab9ecfd7785 100644 (file)
@@ -43,11 +43,11 @@ class JSONDecodeError(ValueError):
         return self.__class__, (self.msg, self.doc, self.pos)
 
 
-_CONSTANTS = {
+_CONSTANTS = frozendict({
     '-Infinity': NegInf,
     'Infinity': PosInf,
     'NaN': NaN,
-}
+})
 
 
 HEXDIGITS = re.compile(r'[0-9A-Fa-f]{4}', FLAGS)
index 050c2fe2161e3ea9403feb55c95a8aa53152a621..e0b944b197d38b6d5867450e9b29d19c6751f3de 100644 (file)
@@ -22,13 +22,13 @@ _color_pattern = re.compile(r'''
     (?P<null>null)
 ''', re.VERBOSE)
 
-_group_to_theme_color = {
+_group_to_theme_color = frozendict({
     "key": "definition",
     "string": "string",
     "number": "number",
     "boolean": "keyword",
     "null": "keyword",
-}
+})
 
 
 def _colorize_json(json_str, theme):
index f016b8dc4a50b2ffc0d271924ec3d28bbdf8e5bf..165f42baed94e3d26dedd2afb17a1dc53a70936d 100644 (file)
@@ -46,81 +46,81 @@ _nb_ops = _opcode.get_nb_ops()
 
 hascompare = [opmap["COMPARE_OP"]]
 
-_cache_format = {
-    "LOAD_GLOBAL": {
-        "counter": 1,
-        "index": 1,
-        "module_keys_version": 1,
-        "builtin_keys_version": 1,
-    },
-    "BINARY_OP": {
-        "counter": 1,
-        "descr": 4,
-    },
-    "UNPACK_SEQUENCE": {
-        "counter": 1,
-    },
-    "COMPARE_OP": {
-        "counter": 1,
-    },
-    "CONTAINS_OP": {
-        "counter": 1,
-    },
-    "FOR_ITER": {
-        "counter": 1,
-    },
-    "LOAD_SUPER_ATTR": {
-        "counter": 1,
-    },
-    "LOAD_ATTR": {
-        "counter": 1,
-        "version": 2,
-        "keys_version": 2,
-        "descr": 4,
-    },
-    "STORE_ATTR": {
-        "counter": 1,
-        "version": 2,
-        "index": 1,
-    },
-    "CALL": {
-        "counter": 1,
-        "func_version": 2,
-    },
-    "CALL_KW": {
-        "counter": 1,
-        "func_version": 2,
-    },
-    "CALL_FUNCTION_EX": {
-        "counter": 1,
-    },
-    "STORE_SUBSCR": {
-        "counter": 1,
-    },
-    "SEND": {
-        "counter": 1,
-    },
-    "JUMP_BACKWARD": {
-        "counter": 1,
-    },
-    "TO_BOOL": {
-        "counter": 1,
-        "version": 2,
-    },
-    "POP_JUMP_IF_TRUE": {
-        "counter": 1,
-    },
-    "POP_JUMP_IF_FALSE": {
-        "counter": 1,
-    },
-    "POP_JUMP_IF_NONE": {
-        "counter": 1,
-    },
-    "POP_JUMP_IF_NOT_NONE": {
-        "counter": 1,
-    },
-}
+_cache_format = frozendict(
+    LOAD_GLOBAL=frozendict(
+        counter=1,
+        index=1,
+        module_keys_version=1,
+        builtin_keys_version=1,
+    ),
+    BINARY_OP=frozendict(
+        counter=1,
+        descr=4,
+    ),
+    UNPACK_SEQUENCE=frozendict(
+        counter=1,
+    ),
+    COMPARE_OP=frozendict(
+        counter=1,
+    ),
+    CONTAINS_OP=frozendict(
+        counter=1,
+    ),
+    FOR_ITER=frozendict(
+        counter=1,
+    ),
+    LOAD_SUPER_ATTR=frozendict(
+        counter=1,
+    ),
+    LOAD_ATTR=frozendict(
+        counter=1,
+        version=2,
+        keys_version=2,
+        descr=4,
+    ),
+    STORE_ATTR=frozendict(
+        counter=1,
+        version=2,
+        index=1,
+    ),
+    CALL=frozendict(
+        counter=1,
+        func_version=2,
+    ),
+    CALL_KW=frozendict(
+        counter=1,
+        func_version=2,
+    ),
+    CALL_FUNCTION_EX=frozendict(
+        counter=1,
+    ),
+    STORE_SUBSCR=frozendict(
+        counter=1,
+    ),
+    SEND=frozendict(
+        counter=1,
+    ),
+    JUMP_BACKWARD=frozendict(
+        counter=1,
+    ),
+    TO_BOOL=frozendict(
+        counter=1,
+        version=2,
+    ),
+    POP_JUMP_IF_TRUE=frozendict(
+        counter=1,
+    ),
+    POP_JUMP_IF_FALSE=frozendict(
+        counter=1,
+    ),
+    POP_JUMP_IF_NONE=frozendict(
+        counter=1,
+    ),
+    POP_JUMP_IF_NOT_NONE=frozendict(
+        counter=1,
+    ),
+)
 
-_inline_cache_entries = {
+_inline_cache_entries = frozendict({
     name : sum(value.values()) for (name, value) in _cache_format.items()
-}
+})
index 5ff7f74754f9c1f9f9346e14803817e777c0bc4d..de1082442ef7f2eb741c1c23365e46d171b93243 100644 (file)
@@ -407,10 +407,12 @@ def _parse_num(val, type):
 def _parse_int(val):
     return _parse_num(val, int)
 
-_builtin_cvt = { "int" : (_parse_int, _("integer")),
-                 "long" : (_parse_int, _("integer")),
-                 "float" : (float, _("floating-point")),
-                 "complex" : (complex, _("complex")) }
+_builtin_cvt = frozendict({
+    "int": (_parse_int, _("integer")),
+    "long": (_parse_int, _("integer")),
+    "float": (float, _("floating-point")),
+    "complex": (complex, _("complex")),
+})
 
 def check_builtin(option, opt, value):
     (cvt, what) = _builtin_cvt[option.type]
index 3a71b669985f132bbc09ca143082bec8168b0645..9d7aa5c66a91cb3b11bbba76d325be0ff86eb41d 100644 (file)
@@ -127,7 +127,7 @@ except ImportError:
 # Based on the description of the PHP's version_compare():
 # http://php.net/manual/en/function.version-compare.php
 
-_ver_stages = {
+_ver_stages = frozendict({
     # any string not found in this dict, will get 0 assigned
     'dev': 10,
     'alpha': 20, 'a': 20,
@@ -136,7 +136,7 @@ _ver_stages = {
     'RC': 50, 'rc': 50,
     # number, will get 100 assigned
     'pl': 200, 'p': 200,
-}
+})
 
 
 def _comparable_version(version):
@@ -705,11 +705,11 @@ def _syscmd_file(target, default=''):
 
 # Default values for architecture; non-empty strings override the
 # defaults given as parameters
-_default_architecture = {
+_default_architecture = frozendict({
     'win32': ('', 'WindowsPE'),
     'win16': ('', 'Windows'),
     'dos': ('', 'MSDOS'),
-}
+})
 
 def architecture(executable=sys.executable, bits='', linkage=''):
 
index cae38672f641b76906a5ff1cc1ee0392d955eae6..3c6a6b7bdc44d299832fa0d12eb04ea8ab757172 100644 (file)
@@ -453,7 +453,7 @@ class InvalidFileException (ValueError):
     def __init__(self, message="Invalid file"):
         ValueError.__init__(self, message)
 
-_BINARY_FORMAT = {1: 'B', 2: 'H', 4: 'L', 8: 'Q'}
+_BINARY_FORMAT = frozendict({1: 'B', 2: 'H', 4: 'L', 8: 'Q'})
 
 _undefined = object()
 
index 612b32cd0765ecafd3e8007a2672eb08ac9b8ac9..896db17baeb3dbde6eb9a824262250389e2ef101 100644 (file)
@@ -150,7 +150,8 @@ _IntEnum._convert_(
     source=_ssl)
 
 PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_SSLv23 = _SSLMethod.PROTOCOL_TLS
-_PROTOCOL_NAMES = {value: name for name, value in _SSLMethod.__members__.items()}
+_PROTOCOL_NAMES = frozendict({
+    value: name for name, value in _SSLMethod.__members__.items()})
 
 _SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None)
 
index 45610fd5612995b177ccde18a41b3a604faf75e5..c7152a70f5aa0b0b83555ee52853cc3ffa23e323 100644 (file)
@@ -414,7 +414,7 @@ class Symbol:
 _flags = [('USE', USE)]
 _flags.extend(kv for kv in globals().items() if kv[0].startswith('DEF_'))
 _scopes_names = ('FREE', 'LOCAL', 'GLOBAL_IMPLICIT', 'GLOBAL_EXPLICIT', 'CELL')
-_scopes_value_to_name = {globals()[n]: n for n in _scopes_names}
+_scopes_value_to_name = frozendict({globals()[n]: n for n in _scopes_names})
 
 
 def main(args):
index 75984bf8b262b932fc0b0fd0fe35bfc82ed467af..7abda3653e764b6bacffe7633c3ea98ca72c4ebb 100644 (file)
@@ -859,11 +859,11 @@ def data_filter(member, dest_path):
         return member.replace(**new_attrs, deep=False)
     return member
 
-_NAMED_FILTERS = {
+_NAMED_FILTERS = frozendict({
     "fully_trusted": fully_trusted_filter,
     "tar": tar_filter,
     "data": data_filter,
-}
+})
 
 #------------------
 # Exported Classes