Skip to content

Fall back to a higher pickle protocol when protocol 0 fails - #327

Merged
gilch merged 3 commits into
gilch:masterfrom
Sanjays2402:fix/pickle-protocol-fallback
Aug 3, 2026
Merged

Fall back to a higher pickle protocol when protocol 0 fails#327
gilch merged 3 commits into
gilch:masterfrom
Sanjays2402:fix/pickle-protocol-fallback

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Closes #291

Compiler.pickle() built its candidate list eagerly for protocol 0 and MAX_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 (SyntaxError from 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.

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
@gilch

gilch commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Check seems to be stuck. I'll try reopening.

@gilch gilch closed this Aug 1, 2026
@gilch

gilch commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Reopened to restart Actions.

@gilch gilch reopened this Aug 1, 2026

@gilch gilch left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment. No rush. I planned to fix this issue myself eventually. If I get around to it first, I'll just close yours.

Comment thread src/hissp/compiler.py
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
gilch merged commit 5c21527 into gilch:master Aug 3, 2026
3 checks passed
@gilch

gilch commented Aug 3, 2026

Copy link
Copy Markdown
Owner

I should probably mention this in the changelog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler should fall back to higher pickle protocols in some cases

2 participants