Skip to content

[3.15] gh-158254: Detect keys changing during reverse dict iteration (GH-158255) - #158267

Open
miss-islington wants to merge 1 commit into
python:3.15from
miss-islington:backport-042a7c1-3.15
Open

miss-islington wants to merge 1 commit into
python:3.15from
miss-islington:backport-042a7c1-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

A reverse dict iterator only checked that the dict's size was unchanged,
so after the keys were replaced with the same number of keys it kept
yielding entries for the new keys. It could yield more items than the
dict had when it was created, and length_hint() then wrapped around.

Add the check the forward iterators already have: finding an entry after
the expected number of items raises "dictionary keys changed during
iteration".
(cherry picked from commit 042a7c1)

Co-authored-by: Joshua Bronson jabronson@gmail.com
Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

…ythonGH-158255)

A reverse dict iterator only checked that the dict's size was unchanged,
so after the keys were replaced with the same number of keys it kept
yielding entries for the new keys. It could yield more items than the
dict had when it was created, and __length_hint__() then wrapped around.

Add the check the forward iterators already have: finding an entry after
the expected number of items raises "dictionary keys changed during
iteration".
(cherry picked from commit 042a7c150552a4ddb5a90cf65a6e5b9e7ba3f7dc)

Co-authored-by: Joshua Bronson <jabronson@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants