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-132617: Fixdict.update() mutation check#134815

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

Open
colesbury wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromcolesbury:gh-132617-dict-update

Conversation

colesbury
Copy link
Contributor

@colesburycolesbury commentedMay 27, 2025
edited by bedevere-appbot
Loading

Usema_used instead ofma_keys->dk_nentries for modification check so that we only check if the dictionary is modified, not if new keys are added to a different dictionary that shared the same keys object.

Use `ma_used` instead of `ma_keys->dk_nentries` for modification checkso that we only check if the dictionary is modified, not if new keys areadded to a different dictionary that shared the same keys object.
@colesburycolesbury added needs backport to 3.13bugs and security fixes needs backport to 3.14bugs and security fixes labelsMay 27, 2025
@colesburycolesbury requested a review frommpageMay 27, 2025 20:45
@colesburycolesbury changed the titlegh-132617: Fixdict.update() mutation checkgh-132617: Fixdict.update() mutation checkMay 27, 2025
Comment on lines 303 to 304
# Create an object that shares the same PyDictKeysObject as
# the dict
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused by this comment and the blurb. I think the instances ofMyClass (obj andobj2) share the same keys object, but that keys object is not shared withx.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I updated the comment to make it explicit that it's the same keys object asobj.__dict__.

The mutation check is on the argument todict.update(). I don't think there's any check on the receiver.

@@ -3858,7 +3858,7 @@ dict_dict_merge(PyInterpreterState *interp, PyDictObject *mp, PyDictObject *othe
}
}

Py_ssize_t orig_size = other->ma_keys->dk_nentries;
Py_ssize_t orig_size = other->ma_used;
Copy link
Member

Choose a reason for hiding this comment

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

ma_used is too weak. It cannot detectdel d["a"]; d["b"] = None.

For better check:

  • For all table, checkother->ma_keys is not changed.
  • For combined table, checkother->ma_keys->dk_nentries is not changed.
  • For split table, checkother->ma_values->size is not changed.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

It's the same condition we use in iterators and elsewhere. I don't think we should strengthen any of the checks. These are heuristic checks, they're not going to be perfect, but if we decide to do so, I think we should do that in a subsequent PR and not backport that change.

if (di->di_used!=d->ma_used) {
PyErr_SetString(PyExc_RuntimeError,
"dictionary changed size during iteration");
di->di_used=-1;/* Make this state sticky */
returnNULL;
}

mpage reacted with thumbs up emoji
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@methanemethaneAwaiting requested review from methanemethane is a code owner

@mpagempageAwaiting requested review from mpage

Assignees
No one assigned
Labels
awaiting core reviewneeds backport to 3.13bugs and security fixesneeds backport to 3.14bugs and security fixes
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@colesbury@methane@mpage

[8]ページ先頭

©2009-2025 Movatter.jp