Repository navigation
gh-162644: Fix infinite recursion when pickling a weakref.ref subclass with protocol 0 or 1 - #162666
Open
AlexxRussell wants to merge 1 commit into
Open
AlexxRussell wants to merge 1 commit into
AlexxRussell wants to merge 1 commit into
Conversation
…ubclass with protocol 0 or 1 The pickler now raises TypeError when the weak save of an object leads straight back to a weak save of the same object and nothing has been memoized in between.
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.
Since GH-153818, pickling an instance of a plain
weakref.refsubclass with protocol 0 or 1 ends inRecursionError.copyreg._reduce_ex()putsweakref.ref(self)into the reduce arguments, andsave_weakref()saves its referent, which is the instance being reduced and not in the memo yet, so the same steps repeat. In 3.14 this raisedTypeError, as protocols 2 and higher still do.save_weakref()now raisesTypeErrorwhen the weak save of an object that is not in the memo leads straight back to a weak save of the same object and nothing has been memoized in between.In the issue I wrote that I had a fix in
copyreg._reduce_ex(). That one was wrong:object.__reduce__()always goes through_reduce_ex(), so raising there also broke subclasses that reuse the state fromsuper().__reduce__(), which work in 3.14 and on main:Two things to know:
__reduce__()that changes its answer between calls works on main and can raiseTypeErrornow.RecursionErroras well.