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-132762: Fix underallocation bug in dict.fromkeys() and expand test coverage#133627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
colesbury merged 5 commits intopython:mainfromangela-tarantula:fix-issue-132762
May 8, 2025

Conversation

angela-tarantula
Copy link
Contributor

@angela-tarantulaangela-tarantula commentedMay 8, 2025
edited
Loading

Closes#132762

Summary

dict_set_fromkeys() was only sizing its new table by the size of theiterable input, ignoring any existing entries in the dictionary. This triggered an infinite loop indictresize() whenever the new dictionary size was too small to reinsert those entries. This patch adds the samePy_MAX(…, DK_LOG_SIZE(mp->ma_keys)) guard thatdict_dict_fromkeys() uses, so we never accidentally shrink the table below its current capacity. The relevant test casebaddict3 has been updated to cover this edge case.

For more background, see myproposal.

3 New Regression Tests

Ever since thefast path wasupdated, the slow path completely lost test coverage. To rectify this, I added 3 new tests:

  • 1 slow-path test when theiterable input is neither a set nor a dictionary
  • 1 slow-path test whenfromkeys() is called on a proper subclass of dict,baddict4
  • 1 fast-path test when the input is a set (worth testing explicitly now thatdict_dict_fromkeys() anddict_set_fromkeys() are separate)

Thanks for the review!@DinoV@colesbury

previously covered: - fast path for dictionary inputs - fast path when object's constructor returns non-empty dict (too small   for good coverage)now additionally covered: - fast path for set inputs - slow path for non-set, non-dictionary inputs - fast path when object's constructor returns *large* non-empty dict - slow path when object is a proper subclass of dict
@python-cla-bot
Copy link

python-cla-botbot commentedMay 8, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@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.

@colesburycolesbury self-requested a reviewMay 8, 2025 17:01
@colesburycolesbury added needs backport to 3.13bugs and security fixes needs backport to 3.14bugs and security fixes labelsMay 8, 2025
Copy link
Contributor

@colesburycolesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks, lgtm!

@colesburycolesbury merged commit421ba58 intopython:mainMay 8, 2025
47 checks passed
@miss-islington-app
Copy link

Thanks@angela-tarantula for the PR, and@colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestMay 8, 2025
…h-133627)The function `dict_set_fromkeys()` adds elements of a set to an existingdictionary. The size of the expanded dictionary was estimated with`PySet_GET_SIZE(iterable)`, which did not take into account the size of theexisting dictionary.(cherry picked from commit421ba58)Co-authored-by: Angela Liss <59097311+angela-tarantula@users.noreply.github.com>
@miss-islington-app
Copy link

Sorry,@angela-tarantula and@colesbury, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 421ba589d02b53131f793889d221ef3b1f1410a4 3.13

@bedevere-app
Copy link

GH-133685 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelMay 8, 2025
colesbury pushed a commit to colesbury/cpython that referenced this pull requestMay 8, 2025
…ythongh-133627)The function `dict_set_fromkeys()` adds elements of a set to an existingdictionary. The size of the expanded dictionary was estimated with`PySet_GET_SIZE(iterable)`, which did not take into account the size of theexisting dictionary.(cherry picked from commit421ba58)Co-authored-by: Angela Liss <59097311+angela-tarantula@users.noreply.github.com>
@bedevere-app
Copy link

GH-133686 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelMay 8, 2025
colesbury pushed a commit that referenced this pull requestMay 8, 2025
) (gh-133685)The function `dict_set_fromkeys()` adds elements of a set to an existingdictionary. The size of the expanded dictionary was estimated with`PySet_GET_SIZE(iterable)`, which did not take into account the size of theexisting dictionary.(cherry picked from commit421ba58)Co-authored-by: Angela Liss <59097311+angela-tarantula@users.noreply.github.com>
colesbury added a commit that referenced this pull requestMay 8, 2025
) (gh-133686)The function `dict_set_fromkeys()` adds elements of a set to an existingdictionary. The size of the expanded dictionary was estimated with`PySet_GET_SIZE(iterable)`, which did not take into account the size of theexisting dictionary.(cherry picked from commit421ba58)Co-authored-by: Angela Liss <59097311+angela-tarantula@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@colesburycolesburycolesbury approved these changes

@methanemethaneAwaiting requested review from methanemethane is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees

@colesburycolesbury

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

dict_set_fromkeys() calculates size of dictionary improperly
2 participants
@angela-tarantula@colesbury

[8]ページ先頭

©2009-2025 Movatter.jp