Repository navigation
Conversation
Protocols 0 and 1 made copyreg call pybind11_object(self), which threw a C++ exception through C frames and aborted the process on Python 3.9+. py::pickle() now defines __reduce_ex__ to reduce as protocol 2 for lower protocols. pybind11_object_new raises TypeError and no longer throws. Fixes pybind#3745 Assisted-by: ClaudeCode:claude-fable-5-2
PyPy pickles a class without py::pickle() at protocols 0 and 1, then segfaults on loads because __setstate__ runs on an uninitialized instance. Assisted-by: ClaudeCode:claude-opus-5-5
pybind11_object_new checked all_type_info(), then allocate_layout() looked it up again. Pass the result through instead. Assisted-by: ClaudeCode:claude-fable-5-2
The error depends on which module creates pybind11_object, and PyPy does not raise it. Assisted-by: ClaudeCode:claude-fable-5-2
…0 and 1 The automatic __reduce_ex__ now calls the next __reduce_ex__ in the MRO, and a __reduce_ex__ bound on the same class replaces it. py::enum_ uses the same helper. Assisted-by: ClaudeCode:claude-fable-5-2
a6353e6 to
2118674
Compare
|
— codex on behalf of Ralf — Preserve descriptor binding when delegating to an inherited Generated by Codex. Ralf has yet to review these findings. |
Use super() so an inherited classmethod or staticmethod __reduce_ex__ gets the right arguments. Assisted-by: ClaudeCode:claude-opus-5-5
|
🤖 AI text below 🤖 Fixed in 417a45c. The delegation now calls |
PyPy's Windows import library does not export PySuper_Type. Get super from builtins at runtime. Assisted-by: ClaudeCode:claude-opus-5-5
This is one of the major pain points of pybind11, would be exciting if we can finally improve it.
Some Codex and Claude investigation showed three possible points:
This can cause a change in this case:
class A(Bound, Mixin), whereMixindefines__reduce_ex__. The new one gets used instead of that. I think that's fine (easy to workaround, like putting the mixin first, etc.) All other combinations, like normal parents, etc. work fine if a user was overloading this.Current promise of a type error is a bit too broad. Edit: fixed.
And finally, this can cause an extra lookup, about 5% on a trial class. If we pass the
all_type_inforesult frompybind11_object_newintoallocate_layout, then the lookup happens once. Edit: This is a trivial change, so I can push it to keep this minimal impact.🤖 AI text below 🤖
Description
Fixes #3745.
With pickle protocols 0 and 1,
copyreg._reduce_excallspybind11_object(self)on Python 3.9+.pybind11_object_newthen threw a C++ exception through C frames, and the process aborted.py::pickle()now also defines__reduce_ex__. It reduces as protocol 2 when the requested protocol is lower. The result is valid for all protocols, so these classes now round-trip on protocols 0 and 1.pybind11_object_newraisesTypeErrorwhen the type has no pybind11-registered base. Classes that bind__getstate__and__setstate__directly still do not support protocols 0 and 1. On CPython,dumpsnow raises thisTypeErrorand no longer aborts.The
TypeErroris not guaranteed.pybind11_objectis shared across modules, so it applies only when the module that creates the base type has this fix. PyPy does not raise indumps. Thepy::pickle()change is per class.Suggested changelog entry:
py::pickle()now support pickle protocols 0 and 1. Classes that bind__getstate__and__setstate__directly still do not support these protocols; on CPython,pickle.dumpsnow raisesTypeErrorin most cases and no longer aborts the process.