FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141510: Fix copy.deepcopy() for recursive frozendict by vstinner · Pull Request #145027 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 11 additions & 1 deletion Lib/copy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,17 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
d[dict] = _deepcopy_dict

def _deepcopy_frozendict(x, memo, deepcopy=deepcopy):
y = _deepcopy_dict(x, memo, deepcopy)
y = {}
for key, value in x.items():
y[deepcopy(key, memo)] = deepcopy(value, memo)

# We're not going to put the frozendict in the memo, but it's still
# important we check for it, in case the frozendict contains recursive
# mutable structures.
try:
return memo[id(x)]
except KeyError:
pass
return frozendict(y)
d[frozendict] = _deepcopy_frozendict

Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_copy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,23 @@ def test_deepcopy_frozendict(self):
self.assertIsNot(x, y)
self.assertIsNot(x["foo"], y["foo"])

# recursive frozendict
x = frozendict(foo=[])
x['foo'].append(x)
Comment thread
vstinner marked this conversation as resolved.
y = copy.deepcopy(x)
self.assertEqual(y.keys(), x.keys())
self.assertIsNot(x, y)
self.assertIsNot(x["foo"], y["foo"])
Comment thread
vstinner marked this conversation as resolved.
self.assertIs(y['foo'][0], y)

x = frozendict(foo=[])
x['foo'].append(x)
x = x['foo']
y = copy.deepcopy(x)
self.assertIsNot(x, y)
self.assertIsNot(x[0], y[0])
self.assertIs(y[0]['foo'], y)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Add also other assertions similar to the above ones:

        self.assertEqual(y, x)
        self.assertIsNot(x, y)
        self.assertIsNot(x[0], y[0])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

self.assertEqual(y, x) fails with RecursionError. I added the two other tests.


@support.skip_emscripten_stack_overflow()
@support.skip_wasi_stack_overflow()
def test_deepcopy_reflexive_dict(self):
Expand Down
Loading

Back | FazBrowse Home | New Git URL