gh-91051: allow setting a callback hook on PyType_Modified
- Issue: gh-91051
PyPerformance results show neutral impact on geometric mean of benchmarks: https://gist.github.com/ericsnowcurrently/01b79f54360efa52d4c5bb536cc885e5
For the record, discussed this in person with @markshannon and agreed to expand it so that you watch individual types instead of subscribing only to all changes to all types. This will make it more similar to dictionary watchers (https://github.com/python/cpython/pull/31787). So I'm working on that update.
@markshannon I think this is ready for your review now; I've implemented the changes we discussed.
You may want to take a look at how I chose to document the behavior of callback "aggregation" (the "only call back if valid version tag" stuff); I aimed to accurately describe the behavior without getting into how it is implemented internally, and without promising that it won't change.
Also note that I am calling assign_version_tag in PyType_Watch. I don't think this is strictly required for correctness, as presumably in real usage anyone who is calling PyType_Watch is probably also doing a lookup on the type around the same time which will populate the version tag. But I think it makes the behavior more intuitive and less potentially confusing, since it means you'll always get called back on the next modification to the type after you watch it. Let me know if you think we should do something different here.
(I also removed the method cache struct from the signature of assign_version_tag -- this seems fine since it was unused there, and this is a private static function.)
Thanks @erlend-aasland, updated the test coverage according to your comments.
@markshannon I think this is ready, pretty straightforward along the same lines as dict watchers, would be great to just get it in and stop having to resolve conflicts.
Thanks @erlend-aasland, updated the test coverage according to your comments.
Thanks! Looks good to me.
@markshannon good suggestion, updated!
Thanks @carljm for implementing this.