Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.2k
gh-145492: Fix defaultdict __repr__ infinite recursion#145659
Open
mvanhorn wants to merge 3 commits intopython:mainfrom
Open
gh-145492: Fix defaultdict __repr__ infinite recursion#145659mvanhorn wants to merge 3 commits intopython:mainfrom
mvanhorn wants to merge 3 commits intopython:mainfrom
Conversation
Move Py_ReprLeave(dd->default_factory) inside the else branch so it isonly called when Py_ReprEnter returned 0 (successfully entered). WhenPy_ReprEnter detects recursion (returns > 0), it does not add a newentry to the repr tracking list. Calling Py_ReprLeave in that caseincorrectly removed the entry from the outer (non-recursive) call,which allowed subsequent recursive calls to bypass the guard entirely,leading to infinite recursion.Includes a regression test.Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
skirpichev approved these changesMar 9, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| @@ -0,0 +1,3 @@ | |||
| Fix infinite recursion in:class:`collections.defaultdict` ``__repr__`` | |||
| when a ``defaultdict`` contains itself. Based on analysis by KowalskiThomas | |||
| in:issue:`145492`. | |||
Member
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Suggested change
| in:issue:`145492`. | |
| in:gh:`145492`. |
:issue: is for b.p.o issues.
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 freeto 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.
Uh oh!
There was an error while loading.Please reload this page.
Fixes#145492.
Summary
Fixed the recursion guard in
defdict_reprso thatPy_ReprLeaveis only called whenPy_ReprEntersuccessfully entered (returned 0). Previously,Py_ReprLeavewas called unconditionally, including whenPy_ReprEnterreturned > 0 (recursion detected). SincePy_ReprEnterdoes not add a new entry when it detects recursion, callingPy_ReprLeavein that case incorrectly removed the entry from the outer (non-recursive) call, allowing subsequent recursive calls to bypass the guard and cause infinite recursion.The fix is a 3-line change: move
Py_ReprLeave(dd->default_factory)inside theelsebranch of the recursion check.Test plan
test_repr_recursive_factoryregression test that reproduces the infinite recursion scenario from the issuetest_defaultdicttests continue to pass (13/13)This contribution was developed with AI assistance (Claude Code).
collections.defaultdict's__repr__can lead to infinite recursion #145492