bpo-46896: Add C API for watching dictionaries #31787
Open
+433
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.


Notes on the PR for reviewers:
_Py*APIs are internal-only, I added the API as public (Py*). But I think it should perhaps be CPython-specific, so I put it inInclude/cpython/dictobject.h, notInclude/dictobject.h. Not sure about any of this, though, so happy to change as requested.STORE_ATTR_WITH_HINTopcode implementation inceval.cpokes directly at dict internals. This meant I had to duplicate a bit of code that otherwise would be private todictobject.c. I think the best option here would be to add higher-level (if private) API in dictobject (something like_PyDict_SetWithHint) for the needs ofSTORE_ATTR_WITH_HINTso it doesn't have to poke directly at dict internals anymore (though that may be difficult due to the needs of deopt), but that seemed a bit far afield for this PR, so for now I just added the duplication with a comment.dict_notify_eventreturn the new dict version reduces the overhead in the unwatched case to a singletestb/jne(checking only once if the dict is watched.)https://bugs.python.org/issue46896