Fall back to a higher pickle protocol when protocol 0 fails - #327
Merged
Conversation
Compiler.pickle() built the candidate list eagerly for both protocol 0 and MAX_PROTOCOL, so a form that protocol 0 cannot represent at all raised before the higher protocol was ever tried. An object of a class defining __slots__ without __getstate__ is the reported case: pickle handles it fine at protocol 5, but the compiler emitted a CompileError. Each protocol is now attempted independently and only the ones that succeed are considered. If none succeed the pickling error is re-raised so genuinely unpicklable forms still fail as before. Adds a regression test for the __slots__ case. Closes gilch#291
Owner
|
Check seems to be stuck. I'll try reopening. |
Owner
|
Reopened to restart Actions. |
gilch
requested changes
Aug 1, 2026
A local lambda raises AttributeError rather than PicklingError on some CPython versions, so assert against a class whose __reduce__ returns a non-tuple, which every protocol rejects with PicklingError.
gilch
approved these changes
Aug 3, 2026
Owner
|
I should probably mention this in the changelog. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #291
Compiler.pickle()built its candidate list eagerly for protocol 0 andMAX_PROTOCOL, so a form protocol 0 cannot represent — an object of a class defining__slots__without__getstate__— raised before the higher protocol was tried. Each protocol is now attempted independently and only the successful ones are candidates; if none succeed the pickling error is re-raised, so genuinely unpicklable forms still fail as before.Regression test added in
tests/test_compiler.py; it fails on master (SyntaxErrorfrom the CompileError comment) and passes with the fix. Black clean; the rest of the suite is unchanged from baseline.This change was prepared with AI assistance; the regression test was run locally and fails without the fix.