Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3eb714ad7972ff24d757a416a1b79666d7e2939cba8162f6b64cba5290842c1c5f7ef7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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,50 @@ 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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): | ||
| fd = frozendict(c=1, b=2, a=3, d=4, e=5, f=6) | ||
| for method_name in (None, 'keys', 'values', 'items'): | ||
| if method_name is not None: | ||
| meth = getattr(fd, method_name) | ||
| else: | ||
| meth = lambda: fd | ||
| expected = list(meth())[1:] | ||
| for proto in range(pickle.HIGHEST_PROTOCOL + 1): | ||
| with self.subTest(method_name=method_name, protocol=proto): | ||
| it = iter(meth()) | ||
| next(it) | ||
| p = pickle.dumps(it, proto) | ||
| unpickled = pickle.loads(p) | ||
| self.assertEqual(list(unpickled), expected) | ||
| self.assertEqual(list(it), expected) | ||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.