-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
gh-141510, PEP 814: Add frozendict support to pickle #144967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
3eb714a
d7972ff
24d757a
416a1b7
9666d7e
2939cba
8162f6b
64cba52
90842c1
c5f7ef7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1731,6 +1731,10 @@ class FrozenDict(frozendict): | |
| pass | ||
|
|
||
|
|
||
| class FrozenDictSlots(frozendict): | ||
| __slots__ = ('slot_attr',) | ||
|
|
||
|
|
||
| class FrozenDictTests(unittest.TestCase): | ||
| def test_copy(self): | ||
| d = frozendict(x=1, y=2) | ||
|
|
@@ -1773,10 +1777,8 @@ def test_repr(self): | |
| d = frozendict(x=1, y=2) | ||
| self.assertEqual(repr(d), "frozendict({'x': 1, 'y': 2})") | ||
|
|
||
| class MyFrozenDict(frozendict): | ||
| pass | ||
| d = MyFrozenDict(x=1, y=2) | ||
| self.assertEqual(repr(d), "MyFrozenDict({'x': 1, 'y': 2})") | ||
| d = FrozenDict(x=1, y=2) | ||
| self.assertEqual(repr(d), "FrozenDict({'x': 1, 'y': 2})") | ||
|
|
||
| def test_hash(self): | ||
| # hash() doesn't rely on the items order | ||
|
|
@@ -1825,6 +1827,40 @@ def __new__(self): | |
| self.assertEqual(type(fd), DictSubclass) | ||
| self.assertEqual(created, frozendict(x=1)) | ||
|
|
||
| def test_pickle(self): | ||
| for proto in range(pickle.HIGHEST_PROTOCOL + 1): | ||
| for fd in ( | ||
| frozendict(), | ||
| frozendict(x=1, y=2), | ||
|
vstinner marked this conversation as resolved.
|
||
| FrozenDict(x=1, y=2), | ||
| FrozenDictSlots(x=1, y=2), | ||
| ): | ||
| if type(fd) == FrozenDict: | ||
| fd.attr = 123 | ||
| if type(fd) == FrozenDictSlots: | ||
| fd.slot_attr = 456 | ||
| with self.subTest(fd=fd, proto=proto): | ||
| if proto >= 2: | ||
| p = pickle.dumps(fd, proto) | ||
| fd2 = pickle.loads(p) | ||
| self.assertEqual(fd2, fd) | ||
| self.assertEqual(type(fd2), type(fd)) | ||
|
vstinner marked this conversation as resolved.
|
||
| if type(fd) == FrozenDict: | ||
| self.assertEqual(fd2.attr, 123) | ||
| if type(fd) == FrozenDictSlots: | ||
| self.assertEqual(fd2.slot_attr, 456) | ||
| else: | ||
| # protocol 0 and 1 don't support frozendict | ||
| with self.assertRaises(TypeError): | ||
| pickle.dumps(fd, proto) | ||
|
|
||
| def test_pickle_iter(self): | ||
| it = iter(frozendict(x=1, y=2)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about value iterator and item iterator? Consume one item from the iterator, to ensure that it correctly restores its state. See pickling tests in If things are pickleable, they should also be deepcopyable. It is worth to have explicit deepcopy tests, because they can preserve additional invariants. For example, it should be possible to deepcopy a frozendict containing lambdas or modules. |
||
| for proto in range(pickle.HIGHEST_PROTOCOL + 1): | ||
| p = pickle.dumps(it, proto) | ||
| it2 = pickle.loads(p) | ||
| self.assertEqual(list(it2), ['x', 'y']) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Uh oh!
There was an error while loading. Please reload this page.