cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-91051: allow setting a callback hook on PyType_Modified

Open carljm opened this issue 3 years ago • 3 comments

  • Issue: gh-91051

carljm avatar Oct 04 '22 23:10 carljm

PyPerformance results show neutral impact on geometric mean of benchmarks: https://gist.github.com/ericsnowcurrently/01b79f54360efa52d4c5bb536cc885e5

carljm avatar Oct 05 '22 20:10 carljm

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.

carljm avatar Oct 06 '22 17:10 carljm

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

carljm avatar Oct 08 '22 18:10 carljm

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.

carljm avatar Oct 17 '22 20:10 carljm

Thanks @erlend-aasland, updated the test coverage according to your comments.

Thanks! Looks good to me.

erlend-aasland avatar Oct 18 '22 08:10 erlend-aasland

@markshannon good suggestion, updated!

carljm avatar Oct 20 '22 17:10 carljm

Thanks @carljm for implementing this.

markshannon avatar Oct 21 '22 13:10 markshannon