-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-146059: Call fast_save_leave() in pickle save_frozenset() #146173
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 all commits
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 |
|---|---|---|
|
|
@@ -57,6 +57,8 @@ | |
| # kind of outer loop. | ||
| protocols = range(pickle.HIGHEST_PROTOCOL + 1) | ||
|
|
||
| FAST_NESTING_LIMIT = 50 | ||
|
|
||
|
|
||
| # Return True if opcode code appears in the pickle, else False. | ||
| def opcode_in_pickle(code, pickle): | ||
|
|
@@ -4552,6 +4554,98 @@ def __reduce__(self): | |
| expected = "changed size during iteration" | ||
| self.assertIn(expected, str(e)) | ||
|
|
||
| def fast_save_enter(self, create_data, minprotocol=0): | ||
| # gh-146059: Check that fast_save() is called when | ||
| # fast_save_enter() is called. | ||
| if not hasattr(self, "pickler"): | ||
| self.skipTest("need Pickler class") | ||
|
|
||
| data = [create_data(i) for i in range(FAST_NESTING_LIMIT * 2)] | ||
| data = {"key": data} | ||
|
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. Why is this needed?
Member
Author
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. Ah, it comes from the initial reproducer. But I confirm that I can still reproduce the bug/crash without |
||
| protocols = range(minprotocol, pickle.HIGHEST_PROTOCOL + 1) | ||
| for proto in protocols: | ||
| with self.subTest(proto=proto): | ||
| buf = io.BytesIO() | ||
| pickler = self.pickler(buf, protocol=proto) | ||
| # Enable fast mode (disables memo, enables cycle detection) | ||
| pickler.fast = 1 | ||
| pickler.dump(data) | ||
|
|
||
| buf.seek(0) | ||
| data2 = self.unpickler(buf).load() | ||
| self.assertEqual(data2, data) | ||
|
|
||
| def test_fast_save_enter_tuple(self): | ||
| self.fast_save_enter(lambda i: (i,)) | ||
|
|
||
| def test_fast_save_enter_list(self): | ||
| self.fast_save_enter(lambda i: [i]) | ||
|
|
||
| def test_fast_save_enter_frozenset(self): | ||
| self.fast_save_enter(lambda i: frozenset([i])) | ||
|
|
||
| def test_fast_save_enter_set(self): | ||
| self.fast_save_enter(lambda i: set([i])) | ||
|
|
||
| def test_fast_save_enter_frozendict(self): | ||
| if self.py_version < (3, 15): | ||
| self.skipTest('need frozendict') | ||
| self.fast_save_enter(lambda i: frozendict(key=i), minprotocol=2) | ||
|
|
||
| def test_fast_save_enter_dict(self): | ||
| self.fast_save_enter(lambda i: {"key": i}) | ||
|
|
||
| def deep_nested_struct(self, seed, create_nested, | ||
|
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. Why is the
Member
Author
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. I looked nice to me to have a seed of the same type than the nested structure, but you're right, it makes the test too complex, it's not worth it. Also, I confirm that I can still trigger the bug (when removing |
||
| minprotocol=0, compare_equal=True, | ||
| depth=FAST_NESTING_LIMIT * 2): | ||
| # gh-146059: Check that fast_save() is called when | ||
| # fast_save_enter() is called. | ||
| if not hasattr(self, "pickler"): | ||
| self.skipTest("need Pickler class") | ||
|
|
||
| data = seed | ||
| for i in range(depth): | ||
| data = create_nested(data) | ||
| data = {"key": data} | ||
|
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. Why is this needed? |
||
| protocols = range(minprotocol, pickle.HIGHEST_PROTOCOL + 1) | ||
| for proto in protocols: | ||
| with self.subTest(proto=proto): | ||
| buf = io.BytesIO() | ||
| pickler = self.pickler(buf, protocol=proto) | ||
| # Enable fast mode (disables memo, enables cycle detection) | ||
| pickler.fast = 1 | ||
| pickler.dump(data) | ||
|
|
||
| buf.seek(0) | ||
| data2 = self.unpickler(buf).load() | ||
| if compare_equal: | ||
| self.assertEqual(data2, data) | ||
|
|
||
| def test_deep_nested_struct_tuple(self): | ||
| self.deep_nested_struct((1,), lambda data: (data,)) | ||
|
|
||
| def test_deep_nested_struct_list(self): | ||
| self.deep_nested_struct([1], lambda data: [data]) | ||
|
|
||
| def test_deep_nested_struct_frozenset(self): | ||
| self.deep_nested_struct(frozenset((1,)), | ||
| lambda data: frozenset((1, data))) | ||
|
|
||
| def test_deep_nested_struct_set(self): | ||
| self.deep_nested_struct({1}, lambda data: {K(data)}, | ||
| depth=FAST_NESTING_LIMIT+1, | ||
| compare_equal=False) | ||
|
|
||
| def test_deep_nested_struct_frozendict(self): | ||
| if self.py_version < (3, 15): | ||
| self.skipTest('need frozendict') | ||
| self.deep_nested_struct(frozendict(x=1), | ||
| lambda data: frozendict(x=data), | ||
| minprotocol=2) | ||
|
|
||
| def test_deep_nested_struct_dict(self): | ||
| self.deep_nested_struct({'x': 1}, lambda data: {'x': data}) | ||
|
|
||
|
|
||
| class BigmemPickleTests: | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean
fast_save_leave()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, you're right.