We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7ac0868 commit dd64e42Copy full SHA for dd64e42
3 files changed
Lib/copy.py
@@ -101,7 +101,7 @@ def copy(x):
101
102
103
_copy_atomic_types = frozenset({types.NoneType, int, float, bool, complex, str, tuple,
104
- bytes, frozenset, type, range, slice, property,
+ bytes, frozendict, frozenset, type, range, slice, property,
105
types.BuiltinFunctionType, types.EllipsisType,
106
types.NotImplementedType, types.FunctionType, types.CodeType,
107
weakref.ref, super})
@@ -203,6 +203,11 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
203
return y
204
d[dict] = _deepcopy_dict
205
206
+def _deepcopy_frozendict(x, memo, deepcopy=deepcopy):
207
+ y = _deepcopy_dict(x, memo, deepcopy)
208
+ return frozendict(y)
209
+d[frozendict] = _deepcopy_frozendict
210
+
211
def _deepcopy_method(x, memo): # Copy instance methods
212
return type(x)(x.__func__, deepcopy(x.__self__, memo))
213
d[types.MethodType] = _deepcopy_method
Lib/test/test_copy.py
@@ -133,6 +133,12 @@ def test_copy_dict(self):
133
self.assertEqual(y, x)
134
self.assertIsNot(y, x)
135
136
+ def test_copy_frozendict(self):
137
+ x = frozendict(x=1, y=2)
138
+ self.assertIs(copy.copy(x), x)
139
+ x = frozendict()
140
141
142
def test_copy_set(self):
143
x = {1, 2, 3}
144
y = copy.copy(x)
@@ -419,6 +425,13 @@ def test_deepcopy_dict(self):
419
425
self.assertIsNot(x, y)
420
426
self.assertIsNot(x["foo"], y["foo"])
421
427
428
+ def test_deepcopy_frozendict(self):
429
+ x = frozendict({"foo": [1, 2], "bar": 3})
430
+ y = copy.deepcopy(x)
431
+ self.assertEqual(y, x)
432
+ self.assertIsNot(x, y)
433
+ self.assertIsNot(x["foo"], y["foo"])
434
422
435
@support.skip_emscripten_stack_overflow()
423
436
@support.skip_wasi_stack_overflow()
424
437
def test_deepcopy_reflexive_dict(self):
Misc/NEWS.d/next/Library/2026-02-17-11-28-37.gh-issue-141510.OpAz0M.rst
@@ -0,0 +1,2 @@
1
+The :mod:`copy` module now supports the :class:`frozendict` type. Patch by
2
+Pieter Eendebak based on work by Victor Stinner.
0 commit comments