Skip to content

Commit 4a1a504

Browse files
committed
copy: support frozendict
1 parent 439c98c commit 4a1a504

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Lib/copy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def copy(x):
105105
types.BuiltinFunctionType, types.EllipsisType,
106106
types.NotImplementedType, types.FunctionType, types.CodeType,
107107
weakref.ref, super})
108-
_copy_builtin_containers = frozenset({list, dict, set, bytearray})
108+
_copy_builtin_containers = frozenset({list, dict, frozendict, set, bytearray})
109109

110110
def deepcopy(x, memo=None):
111111
"""Deep copy operation on arbitrary Python objects.
@@ -203,6 +203,11 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
203203
return y
204204
d[dict] = _deepcopy_dict
205205

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+
206211
def _deepcopy_method(x, memo): # Copy instance methods
207212
return type(x)(x.__func__, deepcopy(x.__self__, memo))
208213
d[types.MethodType] = _deepcopy_method

Lib/test/test_copy.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ def test_copy_dict(self):
133133
self.assertEqual(y, x)
134134
self.assertIsNot(y, x)
135135

136+
def test_copy_frozendict(self):
137+
x = frozendict(foo=1, bar=2)
138+
y = copy.copy(x)
139+
self.assertEqual(y, x)
140+
self.assertEqual(type(y), frozendict)
141+
self.assertIsNot(y, x)
142+
x = frozendict()
143+
y = copy.copy(x)
144+
self.assertEqual(y, x)
145+
self.assertEqual(type(y), frozendict)
146+
self.assertIsNot(y, x)
147+
136148
def test_copy_set(self):
137149
x = {1, 2, 3}
138150
y = copy.copy(x)

0 commit comments

Comments
 (0)