https://github.com/python/cpython/commit/349639cfa46180b18c2a3299db08cff253c7a959
commit: 349639cfa46180b18c2a3299db08cff253c7a959
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-03-06T10:25:09+01:00
summary:

gh-141510: Use frozendict in the stdlib (#144909)

Co-authored-by: Donghee Na <[email protected]>

files:
M Lib/functools.py
M Lib/gettext.py
M Lib/json/decoder.py
M Lib/json/tool.py
M Lib/opcode.py
M Lib/optparse.py
M Lib/platform.py
M Lib/plistlib.py
M Lib/ssl.py
M Lib/symtable.py
M Lib/tarfile.py

diff --git a/Lib/functools.py b/Lib/functools.py
index 9bc2ee7e8c894c..cd374631f16792 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -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 @@ def _lt_from_ge(self, other):
     '__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"""
diff --git a/Lib/gettext.py b/Lib/gettext.py
index 6c11ab2b1eb570..2f77f0e849e9ae 100644
--- a/Lib/gettext.py
+++ b/Lib/gettext.py
@@ -111,8 +111,9 @@ def _error(value):
     ('+', '-'),
     ('*', '/', '%'),
 )
-_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):
diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py
index 92ad6352557640..4cd6f8367a1349 100644
--- a/Lib/json/decoder.py
+++ b/Lib/json/decoder.py
@@ -43,11 +43,11 @@ def __reduce__(self):
         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)
diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 050c2fe2161e3e..e0b944b197d38b 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -22,13 +22,13 @@
     (?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):
diff --git a/Lib/opcode.py b/Lib/opcode.py
index f016b8dc4a50b2..165f42baed94e3 100644
--- a/Lib/opcode.py
+++ b/Lib/opcode.py
@@ -46,81 +46,81 @@
 
 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()
-}
+})
diff --git a/Lib/optparse.py b/Lib/optparse.py
index 5ff7f74754f9c1..de1082442ef7f2 100644
--- a/Lib/optparse.py
+++ b/Lib/optparse.py
@@ -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]
diff --git a/Lib/platform.py b/Lib/platform.py
index 3a71b669985f13..9d7aa5c66a91cb 100644
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -127,7 +127,7 @@
 # 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 @@
     '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=''):
 
diff --git a/Lib/plistlib.py b/Lib/plistlib.py
index cae38672f641b7..3c6a6b7bdc44d2 100644
--- a/Lib/plistlib.py
+++ b/Lib/plistlib.py
@@ -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()
 
diff --git a/Lib/ssl.py b/Lib/ssl.py
index 612b32cd0765ec..896db17baeb3db 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -150,7 +150,8 @@
     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)
 
diff --git a/Lib/symtable.py b/Lib/symtable.py
index 45610fd5612995..c7152a70f5aa0b 100644
--- a/Lib/symtable.py
+++ b/Lib/symtable.py
@@ -414,7 +414,7 @@ def get_namespace(self):
 _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):
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 75984bf8b262b9..7abda3653e764b 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to