Skip to content

gh-141510: Raise TypeError in PyDict_SetItem() on frozendict - #145564

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:pydict_setitem
Mar 11, 2026
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:pydict_setitem

Conversation

@vstinner

@vstinner vstinner commented Mar 5, 2026 •

Copy link
Copy Markdown
Member

If the following functions get an unexpected frozendict, raise a TypeError with an useful error message:

  • PyDict_DelItem()
  • PyDict_DelItemString()
  • PyDict_Merge()
  • PyDict_MergeFromSeq2()
  • PyDict_Pop()
  • PyDict_PopString()
  • PyDict_SetDefault()
  • PyDict_SetDefaultRef()
  • PyDict_SetItem()
  • PyDict_SetItemString()
  • _PyDict_SetItem_KnownHash()
  • PyDict_Update()
If the following functions get an unexpected frozendict, raise a
TypeError with an useful error message:

* PyDict_DelItem()
* PyDict_DelItemString()
* PyDict_Merge()
* PyDict_MergeFromSeq2()
* PyDict_Pop()
* PyDict_PopString()
* PyDict_SetDefault()
* PyDict_SetDefaultRef()
* PyDict_SetItem()
* PyDict_SetItemString()
* _PyDict_SetItem_KnownHash()
* PyDict_Update()

Co-Authored-by: mohsinm-dev <mohsin.mdev@gmail.com>
@vstinner

vstinner commented Mar 5, 2026

Copy link
Copy Markdown
Member Author

This change raises a nice TypeError exception with an useful error message when a frozendict is passed to a PyDict function which tries to modify it (ex: PyDict_SetItem).

The initial version of the code is based on PR gh-145123.

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generally looks good to me, but let me take a look til this weekend.

@corona10 corona10 self-assigned this Mar 9, 2026

@corona10 corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@vstinner
vstinner merged commit 713be70 into python:main Mar 11, 2026
52 checks passed
@vstinner
vstinner deleted the pydict_setitem branch March 11, 2026 16:05
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for the review @corona10.

ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…ython#145564)

If the following functions get an unexpected frozendict,
raise TypeError instead of SystemError:

* PyDict_DelItem()
* PyDict_DelItemString()
* PyDict_Merge()
* PyDict_MergeFromSeq2()
* PyDict_Pop()
* PyDict_PopString()
* PyDict_SetDefault()
* PyDict_SetDefaultRef()
* PyDict_SetItem()
* PyDict_SetItemString()
* _PyDict_SetItem_KnownHash()
* PyDict_Update()

Co-authored-by: mohsinm-dev <mohsin.mdev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants