-
-
Couldn't load subscription status.
- Fork 33.3k
gh-93627: Align Python implementation of pickle with C implementation of pickle #103035
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 41 commits
9939a1c
29445b9
95cbf77
31d3b05
e928112
fa2a741
dfc4ef2
3e6e4eb
92362c1
bab3dab
3a6c8c5
9c5cd38
a8a797c
1f1b2ff
ec8d1c7
889f2ab
1900578
d822705
c900f53
a366dc5
2e7fbb7
e43d962
1e79c83
90a06d3
789fe8c
30ffb7c
3a88607
dc7d33e
d365a44
1bf64c0
f275ede
3548dc3
250accc
1ea9a1b
e58f4a1
b1bd83c
f565513
4a928aa
39ab1ed
f694005
833b2fb
c803480
7f3ee96
1f02ab2
f9dd613
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 |
|---|---|---|
|
|
@@ -570,6 +570,87 @@ def test_multiprocessing_exceptions(self): | |
| ('multiprocessing.context', name)) | ||
|
|
||
|
|
||
| class C_with_reduce_ex: | ||
| def __reduce_ex__(self, proto): | ||
| return object.__reduce_ex__(self, proto) | ||
|
|
||
| class C_None_reduce_ex: | ||
| __reduce_ex__ = None | ||
|
|
||
| class C_None_reduce: | ||
| __reduce__ = None | ||
|
|
||
| class C_None_setstate: | ||
| def __getstate__(self): | ||
| return 1 | ||
|
|
||
| __setstate__ = None | ||
|
|
||
| class C_setstate: | ||
| def __init__(self): | ||
| self.value = 0 | ||
|
|
||
| def __getstate__(self): | ||
| return self.value | ||
|
|
||
| def __setstate__(self, state): | ||
| print(state) | ||
eendebakpt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.value = state + 1 | ||
|
|
||
| class PickleReductionMethodsTests(unittest.TestCase): | ||
| # see gh-93627 | ||
|
|
||
| def test_pickle_invalid_reduction_method(self): | ||
| c = C_None_reduce_ex() | ||
| with self.assertRaises(TypeError): | ||
| pickle.dumps(c) | ||
|
||
|
|
||
| c = C_None_reduce() | ||
| with self.assertRaises(TypeError): | ||
| pickle.dumps(c) | ||
|
|
||
| def test_pickle_invalid_reducer_override(self): | ||
| obj=C_with_reduce_ex() | ||
eendebakpt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| bio = io.BytesIO() | ||
| pickler = pickle._Pickler(bio) | ||
| pickler.dump(obj) | ||
|
|
||
| pickler.clear_memo() | ||
| pickler.reducer_override = None | ||
| with self.assertRaises(TypeError): | ||
| pickler.dump(obj) | ||
|
|
||
| pickler.clear_memo() | ||
| pickler.reducer_override = 10 | ||
| with self.assertRaises(TypeError): | ||
| pickler.dump(obj) | ||
|
|
||
| def test_pickle_invalid_dispatch_table(self): | ||
| obj=C_with_reduce_ex() | ||
|
|
||
| bio = io.BytesIO() | ||
| pickler = pickle._Pickler(bio) | ||
| pickler.dispatch_table = {type(obj): None} | ||
| with self.assertRaises(TypeError): | ||
| pickler.dump(obj) | ||
|
|
||
|
|
||
| class PickleSetstateTests(unittest.TestCase): | ||
| # See gh-103035 | ||
|
|
||
| def test_pickle_setstate(self): | ||
| c = C_setstate() | ||
| s = pickle.dumps(c) | ||
| c2 = pickle.loads(s) | ||
| self.assertEqual(c2.value, c.value + 1) | ||
|
|
||
| c = C_None_setstate() | ||
| p = pickle.dumps(c) | ||
| with self.assertRaises(TypeError): | ||
| pickle.loads(p) | ||
|
|
||
|
|
||
| def load_tests(loader, tests, pattern): | ||
| tests.addTest(doctest.DocTestSuite()) | ||
| return tests | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Update the Python pickle module implementation to match the C implementation of the pickle module. For objects setting reduction methods like :meth:`~object.__reduce_ex__` or :meth:`~object.__reduce__` to ``None``, pickling will result in a :exc:`TypeError`. |
Uh oh!
There was an error while loading. Please reload this page.