You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pickling a weakref.ref subclass with protocol 0 or 1 raises RecursionError #162644
Pickling a weakref.ref subclass with protocol 0 or 1 raises RecursionError on main (3.16.0a2+, tested on macOS):
importpickle, weakrefclassR(weakref.ref):
passclassO:
passo=O()
r=R(o)
pickle.dumps(r, 0) # same with protocol 1
C pickler: RecursionError: Stack overflow (used 16320 kB)
pickle._Pickler (pure Python): RecursionError: maximum recursion depth exceeded
On main, protocols 2 to 5 raise TypeError: cannot pickle 'R' object. On 3.14.3, protocols 0 and 1 raise TypeError: cannot pickle 'ReferenceType' object. Protocols 0 and 1 should also raise TypeError.
This started with gh-74876 / GH-153818 (merged 21 October 2026), which made weak references picklable. Only main is affected.
For protocols 0 and 1, copyreg._reduce_ex() builds the state with base(self). Here base is weakref.ReferenceType, so the state is a new weak reference to the object being pickled. Pickling that reference pickles its referent, the same object again, without end.
I have a fix in copyreg._reduce_ex() with a test and can open a PR.
Activity