Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-145492: Fix defaultdict __repr__ infinite recursion#145659

Open
mvanhorn wants to merge 3 commits intopython:mainfrom
mvanhorn:osc/145492-fix-defaultdict-repr-recursion
Open

gh-145492: Fix defaultdict __repr__ infinite recursion#145659
mvanhorn wants to merge 3 commits intopython:mainfrom
mvanhorn:osc/145492-fix-defaultdict-repr-recursion

Conversation

@mvanhorn
Copy link

@mvanhornmvanhorn commentedMar 9, 2026
edited by bedevere-appbot
Loading

Fixes#145492.

Summary

Fixed the recursion guard indefdict_repr so thatPy_ReprLeave is only called whenPy_ReprEnter successfully entered (returned 0). Previously,Py_ReprLeave was called unconditionally, including whenPy_ReprEnter returned > 0 (recursion detected). SincePy_ReprEnter does not add a new entry when it detects recursion, callingPy_ReprLeave in 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: movePy_ReprLeave(dd->default_factory) inside theelse branch of the recursion check.

Test plan

  • Addedtest_repr_recursive_factory regression test that reproduces the infinite recursion scenario from the issue
  • All existingtest_defaultdict tests continue to pass (13/13)

This contribution was developed with AI assistance (Claude Code).

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>
@bedevere-app
Copy link

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 theskip news label instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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`.

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.

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

Reviewers

@StanFromIrelandStanFromIrelandStanFromIreland left review comments

@skirpichevskirpichevskirpichev approved these changes

@rhettingerrhettingerAwaiting requested review from rhettingerrhettinger is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

collections.defaultdict's__repr__ can lead to infinite recursion

3 participants

@mvanhorn@skirpichev@StanFromIreland

[8]ページ先頭

©2009-2026 Movatter.jp