@@ -238,3 +238,54 @@ Dictionary Objects
238
238
for key, value in seq2:
239
239
if override or key not in a:
240
240
a[key] = value
241
+
242
+ .. c :function :: int PyDict_AddWatcher (PyDict_WatchCallback callback)
243
+
244
+ Register *callback* as a dictionary watcher. Return a non-negative integer
245
+ id which must be passed to future calls to :c:func:`PyDict_Watch`. In case
246
+ of error (e.g. no more watcher IDs available), return ``-1`` and set an
247
+ exception.
248
+
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
+
255
+ .. c:function:: int PyDict_Watch(int watcher_id, PyObject *dict)
256
+
257
+ Mark dictionary *dict * as watched. The callback granted *watcher_id * by
258
+ :c:func: `PyDict_AddWatcher ` will be called when *dict * is modified or
259
+ deallocated.
260
+
261
+ .. c :type :: PyDict_WatchEvent
262
+
263
+ Enumeration of possible dictionary watcher events: ``PyDict_EVENT_ADDED ``,
264
+ ``PyDict_EVENT_MODIFIED ``, ``PyDict_EVENT_DELETED ``, ``PyDict_EVENT_CLONED ``,
265
+ ``PyDict_EVENT_CLEARED ``, or ``PyDict_EVENT_DEALLOCATED ``.
266
+
267
+ .. c :type :: int (*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
268
+
269
+ Type of a dict watcher callback function.
270
+
271
+ If *event * is ``PyDict_EVENT_CLEARED `` or ``PyDict_EVENT_DEALLOCATED ``, both
272
+ *key * and *new_value * will be ``NULL ``. If *event * is ``PyDict_EVENT_ADDED ``
273
+ or ``PyDict_EVENT_MODIFIED ``, *new_value * will be the new value for *key *.
274
+ If *event * is ``PyDict_EVENT_DELETED ``, *key * is being deleted from the
275
+ dictionary and *new_value * will be ``NULL ``.
276
+
277
+ ``PyDict_EVENT_CLONED `` occurs when *dict * was previously empty and another
278
+ dict is merged into it. To maintain efficiency of this operation, per-key
279
+ ``PyDict_EVENT_ADDED `` events are not issued in this case; instead a
280
+ single ``PyDict_EVENT_CLONED `` is issued, and *key * will be the source
281
+ dictionary.
282
+
283
+ The callback may inspect but must not modify *dict *; doing so could have
284
+ unpredictable effects, including infinite recursion.
285
+
286
+ Callbacks occur before the notified modification to *dict * takes place, so
287
+ the prior state of *dict * can be inspected.
288
+
289
+ If the callback returns with an exception set, it must return ``-1 ``; this
290
+ exception will be printed as an unraisable exception using
291
+ :c:func: `PyErr_WriteUnraisable `. Otherwise it should return ``0 ``.
0 commit comments