-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-91052: Add C API for watching dictionaries #31787
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0447232
gh-91052: Add C API for watching dictionaries
carljm 1c7f814
Add second-watcher tests, fix MSVC warning
carljm 6e5cee2
Over-engineer the watcher bits check
carljm 4bb5d9d
Merge branch 'main' into dictwatch
carljm 7dcf9b0
Address review comments
carljm 7938d6f
Merge branch 'main' into dictwatch
carljm fb19617
Minor cleanups
carljm e5d2169
Merge branch 'main' into dictwatch
carljm cc1d0b7
Address further review comments
carljm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# these are all functions _testcapi exports whose name begins with 'test_'. | ||
|
||
from collections import OrderedDict | ||
from contextlib import contextmanager | ||
import _thread | ||
import importlib.machinery | ||
import importlib.util | ||
|
@@ -1393,5 +1394,136 @@ def func2(x=None): | |
self.do_test(func2) | ||
|
||
|
||
class TestDictWatchers(unittest.TestCase): | ||
# types of watchers testcapimodule can add: | ||
EVENTS = 0 # appends dict events as strings to global event list | ||
ERROR = 1 # unconditionally sets and signals a RuntimeException | ||
SECOND = 2 # always appends "second" to global event list | ||
|
||
def add_watcher(self, kind=EVENTS): | ||
return _testcapi.add_dict_watcher(kind) | ||
|
||
def clear_watcher(self, watcher_id): | ||
_testcapi.clear_dict_watcher(watcher_id) | ||
|
||
@contextmanager | ||
def watcher(self, kind=EVENTS): | ||
wid = self.add_watcher(kind) | ||
try: | ||
yield wid | ||
finally: | ||
self.clear_watcher(wid) | ||
|
||
def assert_events(self, expected): | ||
actual = _testcapi.get_dict_watcher_events() | ||
self.assertEqual(actual, expected) | ||
|
||
def watch(self, wid, d): | ||
_testcapi.watch_dict(wid, d) | ||
|
||
def test_set_new_item(self): | ||
d = {} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
d["foo"] = "bar" | ||
self.assert_events(["new:foo:bar"]) | ||
|
||
def test_set_existing_item(self): | ||
d = {"foo": "bar"} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
d["foo"] = "baz" | ||
self.assert_events(["mod:foo:baz"]) | ||
|
||
def test_clone(self): | ||
d = {} | ||
d2 = {"foo": "bar"} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
d.update(d2) | ||
self.assert_events(["clone"]) | ||
|
||
def test_no_event_if_not_watched(self): | ||
d = {} | ||
with self.watcher() as wid: | ||
d["foo"] = "bar" | ||
self.assert_events([]) | ||
|
||
def test_del(self): | ||
d = {"foo": "bar"} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
del d["foo"] | ||
self.assert_events(["del:foo"]) | ||
|
||
def test_pop(self): | ||
d = {"foo": "bar"} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
d.pop("foo") | ||
self.assert_events(["del:foo"]) | ||
|
||
def test_clear(self): | ||
d = {"foo": "bar"} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
d.clear() | ||
self.assert_events(["clear"]) | ||
|
||
def test_dealloc(self): | ||
d = {"foo": "bar"} | ||
with self.watcher() as wid: | ||
self.watch(wid, d) | ||
del d | ||
self.assert_events(["dealloc"]) | ||
|
||
def test_error(self): | ||
d = {} | ||
unraisables = [] | ||
def unraisable_hook(unraisable): | ||
unraisables.append(unraisable) | ||
with self.watcher(kind=self.ERROR) as wid: | ||
self.watch(wid, d) | ||
orig_unraisable_hook = sys.unraisablehook | ||
sys.unraisablehook = unraisable_hook | ||
try: | ||
d["foo"] = "bar" | ||
finally: | ||
sys.unraisablehook = orig_unraisable_hook | ||
self.assert_events([]) | ||
self.assertEqual(len(unraisables), 1) | ||
unraisable = unraisables[0] | ||
self.assertIs(unraisable.object, d) | ||
self.assertEqual(str(unraisable.exc_value), "boom!") | ||
|
||
def test_two_watchers(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is ref leaking on my PR: #98001 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah probably fixed in #98017 ? |
||
d1 = {} | ||
d2 = {} | ||
with self.watcher() as wid1: | ||
with self.watcher(kind=self.SECOND) as wid2: | ||
self.watch(wid1, d1) | ||
self.watch(wid2, d2) | ||
d1["foo"] = "bar" | ||
d2["hmm"] = "baz" | ||
self.assert_events(["new:foo:bar", "second"]) | ||
|
||
def test_watch_non_dict(self): | ||
with self.watcher() as wid: | ||
with self.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"): | ||
self.watch(wid, 1) | ||
|
||
def test_watch_out_of_range_watcher_id(self): | ||
d = {} | ||
with self.assertRaisesRegex(ValueError, r"Invalid dict watcher ID -1"): | ||
self.watch(-1, d) | ||
with self.assertRaisesRegex(ValueError, r"Invalid dict watcher ID 8"): | ||
self.watch(8, d) # DICT_MAX_WATCHERS = 8 | ||
|
||
def test_unassigned_watcher_id(self): | ||
d = {} | ||
with self.assertRaisesRegex(ValueError, r"No dict watcher set for ID 1"): | ||
self.watch(1, d) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2022-10-03-16-12-39.gh-issue-91052.MsYL9d.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add API for subscribing to modification events on selected dictionaries. |
Oops, something went wrong.
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.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.