Skip to content

Commit 7dcf9b0

Browse files
committed
Address review comments
1 parent 4bb5d9d commit 7dcf9b0

File tree

7 files changed

+243
-161
lines changed

7 files changed

+243
-161
lines changed

Doc/c-api/dict.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ Dictionary Objects
246246
of error (e.g. no more watcher IDs available), return ``-1`` and set an
247247
exception.
248248
249+
.. c:function:: int PyDict_ClearWatcher(int watcher_id)
250+
251+
Clear watcher identified by *watcher_id* previously returned from
252+
:c:func:`PyDict_AddWatcher`. Return ``0`` on success, ``-1`` on error (e.g.
253+
if the given *watcher_id* was never registered.)
254+
249255
.. c:function:: int PyDict_Watch(int watcher_id, PyObject *dict)
250256
251257
Mark dictionary *dict* as watched. The callback granted *watcher_id* by
@@ -258,7 +264,7 @@ Dictionary Objects
258264
``PyDict_EVENT_MODIFIED``, ``PyDict_EVENT_DELETED``, ``PyDict_EVENT_CLONED``,
259265
``PyDict_EVENT_CLEARED``, or ``PyDict_EVENT_DEALLOCED``.
260266
261-
.. c:type:: void (*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
267+
.. c:type:: int (*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
262268
263269
Type of a dict watcher callback function.
264270
@@ -279,3 +285,7 @@ Dictionary Objects
279285
280286
Callbacks occur before the notified modification to *dict* takes place, so
281287
the prior state of *dict* can be inspected.
288+
289+
If an error occurs in the callback, it may return ``-1`` with an exception
290+
set; this exception will be printed as an unraisable exception using
291+
:c:func:`PyErr_WriteUnraisable`. On success it should return ``0``.

Include/cpython/dictobject.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ typedef enum {
9898
// Callback to be invoked when a watched dict is cleared, dealloced, or modified.
9999
// In clear/dealloc case, key and new_value will be NULL. Otherwise, new_value will be the
100100
// new value for key, NULL if key is being deleted.
101-
typedef void(*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject* dict, PyObject* key, PyObject* new_value);
101+
typedef int(*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject* dict, PyObject* key, PyObject* new_value);
102102

103-
// Register a dict-watcher callback
103+
// Register/unregister a dict-watcher callback
104104
PyAPI_FUNC(int) PyDict_AddWatcher(PyDict_WatchCallback callback);
105+
PyAPI_FUNC(int) PyDict_ClearWatcher(int watcher_id);
105106

106107
// Mark given dictionary as "watched" (callback will be called if it is modified)
107108
PyAPI_FUNC(int) PyDict_Watch(int watcher_id, PyObject* dict);

Include/internal/pycore_dict.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ struct _dictvalues {
155155
extern uint64_t _pydict_global_version;
156156

157157
#define DICT_MAX_WATCHERS 8
158-
#define DICT_VERSION_MASK 255
159-
#define DICT_VERSION_INCREMENT 256
158+
#define DICT_VERSION_INCREMENT (1 << DICT_MAX_WATCHERS)
159+
#define DICT_VERSION_MASK (DICT_VERSION_INCREMENT - 1)
160160

161161
#define DICT_NEXT_VERSION() (_pydict_global_version += DICT_VERSION_INCREMENT)
162162

Include/internal/pycore_interp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct _is {
144144
// Initialized to _PyEval_EvalFrameDefault().
145145
_PyFrameEvalFunction eval_frame;
146146

147-
void *dict_watchers[8];
147+
PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];
148148

149149
Py_ssize_t co_extra_user_count;
150150
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];

Lib/test/test_capi.py

+132
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# these are all functions _testcapi exports whose name begins with 'test_'.
33

44
from collections import OrderedDict
5+
from contextlib import contextmanager
56
import _thread
67
import importlib.machinery
78
import importlib.util
@@ -1393,5 +1394,136 @@ def func2(x=None):
13931394
self.do_test(func2)
13941395

13951396

1397+
class TestDictWatchers(unittest.TestCase):
1398+
# types of watchers testcapimodule can add:
1399+
EVENTS = 0 # appends dict events as strings to global event list
1400+
ERROR = 1 # unconditionally sets and signals a RuntimeException
1401+
SECOND = 2 # always appends "second" to global event list
1402+
1403+
def add_watcher(self, kind=EVENTS):
1404+
return _testcapi.add_dict_watcher(kind)
1405+
1406+
def clear_watcher(self, watcher_id):
1407+
_testcapi.clear_dict_watcher(watcher_id)
1408+
1409+
@contextmanager
1410+
def watcher(self, kind=EVENTS):
1411+
wid = self.add_watcher(kind)
1412+
try:
1413+
yield wid
1414+
finally:
1415+
self.clear_watcher(wid)
1416+
1417+
def assert_events(self, expected):
1418+
actual = _testcapi.get_dict_watcher_events()
1419+
self.assertEqual(actual, expected)
1420+
1421+
def watch(self, wid, d):
1422+
_testcapi.watch_dict(wid, d)
1423+
1424+
def test_set_new_item(self):
1425+
d = {}
1426+
with self.watcher() as wid:
1427+
self.watch(wid, d)
1428+
d["foo"] = "bar"
1429+
self.assert_events(["new:foo:bar"])
1430+
1431+
def test_set_existing_item(self):
1432+
d = {"foo": "bar"}
1433+
with self.watcher() as wid:
1434+
self.watch(wid, d)
1435+
d["foo"] = "baz"
1436+
self.assert_events(["mod:foo:baz"])
1437+
1438+
def test_clone(self):
1439+
d = {}
1440+
d2 = {"foo": "bar"}
1441+
with self.watcher() as wid:
1442+
self.watch(wid, d)
1443+
d.update(d2)
1444+
self.assert_events(["clone"])
1445+
1446+
def test_no_event_if_not_watched(self):
1447+
d = {}
1448+
with self.watcher() as wid:
1449+
d["foo"] = "bar"
1450+
self.assert_events([])
1451+
1452+
def test_del(self):
1453+
d = {"foo": "bar"}
1454+
with self.watcher() as wid:
1455+
self.watch(wid, d)
1456+
del d["foo"]
1457+
self.assert_events(["del:foo"])
1458+
1459+
def test_pop(self):
1460+
d = {"foo": "bar"}
1461+
with self.watcher() as wid:
1462+
self.watch(wid, d)
1463+
d.pop("foo")
1464+
self.assert_events(["del:foo"])
1465+
1466+
def test_clear(self):
1467+
d = {"foo": "bar"}
1468+
with self.watcher() as wid:
1469+
self.watch(wid, d)
1470+
d.clear()
1471+
self.assert_events(["clear"])
1472+
1473+
def test_dealloc(self):
1474+
d = {"foo": "bar"}
1475+
with self.watcher() as wid:
1476+
self.watch(wid, d)
1477+
del d
1478+
self.assert_events(["dealloc"])
1479+
1480+
def test_error(self):
1481+
d = {}
1482+
unraisables = []
1483+
def unraisable_hook(unraisable):
1484+
unraisables.append(unraisable)
1485+
with self.watcher(kind=self.ERROR) as wid:
1486+
self.watch(wid, d)
1487+
orig_unraisable_hook = sys.unraisablehook
1488+
sys.unraisablehook = unraisable_hook
1489+
try:
1490+
d["foo"] = "bar"
1491+
finally:
1492+
sys.unraisablehook = orig_unraisable_hook
1493+
self.assert_events([])
1494+
self.assertEqual(len(unraisables), 1)
1495+
unraisable = unraisables[0]
1496+
self.assertIs(unraisable.object, d)
1497+
self.assertEqual(str(unraisable.exc_value), "boom!")
1498+
1499+
def test_two_watchers(self):
1500+
d1 = {}
1501+
d2 = {}
1502+
with self.watcher() as wid1:
1503+
with self.watcher(kind=self.SECOND) as wid2:
1504+
self.watch(wid1, d1)
1505+
self.watch(wid2, d2)
1506+
d1["foo"] = "bar"
1507+
d2["hmm"] = "baz"
1508+
self.assert_events(["new:foo:bar", "second"])
1509+
1510+
def test_watch_non_dict(self):
1511+
with self.watcher() as wid:
1512+
with self.assertRaisesRegex(ValueError, r"Cannot watch non-dictionary"):
1513+
self.watch(wid, 1)
1514+
1515+
def test_watch_out_of_range_watcher_id(self):
1516+
d = {}
1517+
with self.assertRaisesRegex(ValueError, r"Invalid dict watcher ID -1"):
1518+
self.watch(-1, d)
1519+
with self.assertRaisesRegex(ValueError, r"Invalid dict watcher ID 8"):
1520+
self.watch(8, d) # DICT_MAX_WATCHERS = 8
1521+
1522+
def test_unassigned_watcher_id(self):
1523+
d = {}
1524+
with self.assertRaisesRegex(ValueError, r"No dict watcher set for ID 1"):
1525+
self.watch(1, d)
1526+
1527+
13961528
if __name__ == "__main__":
13971529
unittest.main()

0 commit comments

Comments
 (0)