GeezerVison is showing github.com — gh-141510: Implement copy and deepcopy for frozendict (#144905) · python/cpython@dd64e42
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/copy.py +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -101,7 +101,7 @@ def copy(x): | 101 | 101 |
| 102 | 102 |
| 103 | 103 | _copy_atomic_types = frozenset({types.NoneType, int, float, bool, complex, str, tuple, | 104 | | - bytes, frozenset, type, range, slice, property, | | 104 | + bytes, frozendict, frozenset, type, range, slice, property, | 105 | 105 | types.BuiltinFunctionType, types.EllipsisType, | 106 | 106 | types.NotImplementedType, types.FunctionType, types.CodeType, | 107 | 107 | weakref.ref, super}) | @@ -203,6 +203,11 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy): | 203 | 203 | return y | 204 | 204 | d[dict] = _deepcopy_dict | 205 | 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 | + | 206 | 211 | def _deepcopy_method(x, memo): # Copy instance methods | 207 | 212 | return type(x)(x.__func__, deepcopy(x.__self__, memo)) | 208 | 213 | d[types.MethodType] = _deepcopy_method | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_copy.py +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -133,6 +133,12 @@ def test_copy_dict(self): | 133 | 133 | self.assertEqual(y, x) | 134 | 134 | self.assertIsNot(y, x) | 135 | 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 | + self.assertIs(copy.copy(x), x) | | 141 | + | 136 | 142 | def test_copy_set(self): | 137 | 143 | x = {1, 2, 3} | 138 | 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): | |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +The :mod:`copy` module now supports the :class:`frozendict` type. Patch by | | 2 | +Pieter Eendebak based on work by Victor Stinner. |
|