summaryrefslogtreecommitdiffstats
path: root/Doc/c-api
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2022-10-07 00:08:00 (GMT)
committerGitHub <noreply@github.com>2022-10-07 00:08:00 (GMT)
commita4b7794887929f82c532fcd055326954ff1197ce (patch)
tree257e2dc783858251f893d75c17663913b05a0fad /Doc/c-api
parent683ab859554c34831fcecc854de35745d7fd603c (diff)
downloadcpython-a4b7794887929f82c532fcd055326954ff1197ce.zip
cpython-a4b7794887929f82c532fcd055326954ff1197ce.tar.gz
cpython-a4b7794887929f82c532fcd055326954ff1197ce.tar.bz2
GH-91052: Add C API for watching dictionaries (GH-31787)
Diffstat (limited to 'Doc/c-api')
-rw-r--r--Doc/c-api/dict.rst51
1 files changed, 51 insertions, 0 deletions
diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst
index 819168d..7bebea0 100644
--- a/Doc/c-api/dict.rst
+++ b/Doc/c-api/dict.rst
@@ -238,3 +238,54 @@ Dictionary Objects
for key, value in seq2:
if override or key not in a:
a[key] = value
+
+.. c:function:: int PyDict_AddWatcher(PyDict_WatchCallback callback)
+
+ Register *callback* as a dictionary watcher. Return a non-negative integer
+ id which must be passed to future calls to :c:func:`PyDict_Watch`. In case
+ of error (e.g. no more watcher IDs available), return ``-1`` and set an
+ exception.
+
+.. c:function:: int PyDict_ClearWatcher(int watcher_id)
+
+ Clear watcher identified by *watcher_id* previously returned from
+ :c:func:`PyDict_AddWatcher`. Return ``0`` on success, ``-1`` on error (e.g.
+ if the given *watcher_id* was never registered.)
+
+.. c:function:: int PyDict_Watch(int watcher_id, PyObject *dict)
+
+ Mark dictionary *dict* as watched. The callback granted *watcher_id* by
+ :c:func:`PyDict_AddWatcher` will be called when *dict* is modified or
+ deallocated.
+
+.. c:type:: PyDict_WatchEvent
+
+ Enumeration of possible dictionary watcher events: ``PyDict_EVENT_ADDED``,
+ ``PyDict_EVENT_MODIFIED``, ``PyDict_EVENT_DELETED``, ``PyDict_EVENT_CLONED``,
+ ``PyDict_EVENT_CLEARED``, or ``PyDict_EVENT_DEALLOCATED``.
+
+.. c:type:: int (*PyDict_WatchCallback)(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
+
+ Type of a dict watcher callback function.
+
+ If *event* is ``PyDict_EVENT_CLEARED`` or ``PyDict_EVENT_DEALLOCATED``, both
+ *key* and *new_value* will be ``NULL``. If *event* is ``PyDict_EVENT_ADDED``
+ or ``PyDict_EVENT_MODIFIED``, *new_value* will be the new value for *key*.
+ If *event* is ``PyDict_EVENT_DELETED``, *key* is being deleted from the
+ dictionary and *new_value* will be ``NULL``.
+
+ ``PyDict_EVENT_CLONED`` occurs when *dict* was previously empty and another
+ dict is merged into it. To maintain efficiency of this operation, per-key
+ ``PyDict_EVENT_ADDED`` events are not issued in this case; instead a
+ single ``PyDict_EVENT_CLONED`` is issued, and *key* will be the source
+ dictionary.
+
+ The callback may inspect but must not modify *dict*; doing so could have
+ unpredictable effects, including infinite recursion.
+
+ Callbacks occur before the notified modification to *dict* takes place, so
+ the prior state of *dict* can be inspected.
+
+ If the callback returns with an exception set, it must return ``-1``; this
+ exception will be printed as an unraisable exception using
+ :c:func:`PyErr_WriteUnraisable`. Otherwise it should return ``0``.