18. Watchers [Python 3.12+]

From Python 3.12 onwards watchers have been added [1]. This allows registering a callback function on specific dict, type, code or function object. The callback is called with any event that occurs on the specific object. This can be a powerful debugging technique.

Here is an example of a dictionary watcher.

18.1. Dictionary Watchers

Here is a context manager cWatchers.PyDictWatcher that wraps the low level CPython code with a watcher that reports every dictionary operation to stdout. The code is in src/cpy/Watchers/watcher_example.py.

 1"""Example of using watchers."""
 2
 3from cPyExtPatt import cWatchers
 4
 5
 6def dict_watcher_demo() -> None:
 7    print('dict_watcher_demo():')
 8    d = {}                              # The dictionary we are going to watch.
 9    with cWatchers.PyDictWatcher(d):
10        dd = {'age': 17, }
11        d.update(dd)                    # Generates event: PyDict_EVENT_CLONED
12        d['age'] = 42                   # Generates event: PyDict_EVENT_MODIFIED
13        del d['age']                    # Generates event: PyDict_EVENT_DELETED
14        d['name'] = 'Python'            # Generates event: PyDict_EVENT_ADDED
15        d.clear()                       # Generates event: PyDict_EVENT_CLEARED
16        del d
17
18
19if __name__ == '__main__':
20    dict_watcher_demo()

And the output would be something like this, it reports the Python file, line number, function, event and then detail about the arguments used to manipulate the dictionary:

dict_watcher_demo():
Dict @ 0x0x10fb53c00: watcher_example.py 11 dict_watcher_demo PyDict_EVENT_CLONED
    Dict: {}
    Key (dict): {'age': 17}
    New value : NULL
Dict @ 0x0x10fb53c00: watcher_example.py 12 dict_watcher_demo PyDict_EVENT_MODIFIED
    Dict: {'age': 17}
    Key (str): age
    New value (int): 42
Dict @ 0x0x10fb53c00: watcher_example.py 13 dict_watcher_demo PyDict_EVENT_DELETED
    Dict: {'age': 42}
    Key (str): age
    New value : NULL
Dict @ 0x0x10fb53c00: watcher_example.py 14 dict_watcher_demo PyDict_EVENT_ADDED
    Dict: {}
    Key (str): name
    New value (str): Python
Dict @ 0x0x10fb53c00: watcher_example.py 15 dict_watcher_demo PyDict_EVENT_CLEARED
    Dict: {'name': 'Python'}
    Key : NULL
    New value : NULL

There are some obvious variations here:

  • Add some prefix to each watcher output line to discriminate it from the rest of stdout.

  • Different outputs, such as JSON.

But how does this watcher work?

18.1.1. Low Level C Implementation

We need some low level C code that interacts with the CPython watcher API. First a header file that provides the interface to our dictionary watcher code. This declares two functions:

  • dict_watcher_verbose_add() this adds a watcher to a dictionary and returns the watcher ID.

  • dict_watcher_verbose_remove() this removes a watcher ID from a dictionary.

The actual code is in src/cpy/Watchers/DictWatcher.h. It looks like this, note the Python version guard to ensure this only works with Python 3.12+:

#define PPY_SSIZE_T_CLEAN
#include "Python.h"

#if PY_VERSION_HEX < 0x030C0000

#error "Required version of Python is 3.12+ (PY_VERSION_HEX >= 0x030C0000)"

#else

int dict_watcher_verbose_add(PyObject *dict);
int dict_watcher_verbose_remove(int watcher_id, PyObject *dict);

#endif // #if PY_VERSION_HEX >= 0x030C0000

So there are several moving parts in the implementation in src/cpy/Watchers/DictWatcher.c. First we have some general purpose functions that extract the file name, function name and line number from a Python frame. Note that the Python frame API changed in Python 3.11.

First up, getting the Python file name:

#include "DictWatcher.h"

static const unsigned char *
get_python_file_name(PyFrameObject *frame) {
    if (frame) {
// Python 3.11+ specific code.
#if PY_VERSION_HEX >= 0x030B0000
        /* See:
         * https://docs.python.org/3.11/whatsnew/3.11.html#pyframeobject-3-11-hiding
         */
        const unsigned char *file_name = PyUnicode_1BYTE_DATA(
            PyFrame_GetCode(frame)->co_filename
        );
#else
        const unsigned char *file_name = PyUnicode_1BYTE_DATA(
            frame->f_code->co_filename
        );
#endif // #if PY_VERSION_HEX >= 0x030B0000
        return file_name;
    }
    return "";
}

Now, getting the Python function name:

static const char *
get_python_function_name(PyFrameObject *frame) {
    const char *func_name = NULL;
    if (frame) {
// Python 3.11+ specific code.
#if PY_VERSION_HEX >= 0x030B0000
        /* See:
         * https://docs.python.org/3.11/whatsnew/3.11.html#pyframeobject-3-11-hiding
         */
        func_name = (const char *) PyUnicode_1BYTE_DATA(
            PyFrame_GetCode(frame)->co_name
        );
#else
        func_name = (const char *) PyUnicode_1BYTE_DATA(frame->f_code->co_name);
#endif // #if PY_VERSION_HEX >= 0x030B0000
        return func_name;
    }
    return "";
}

Then, getting the Python line number:

int get_python_line_number(PyFrameObject *frame) {
    if (frame) {
        return PyFrame_GetLineNumber(frame);
    }
    return 0;
}

We bring these together to print a summary of the frame state to a file, such as stdout:

static void
write_frame_data_to_outfile(FILE *outfile, PyFrameObject *frame) {
    if (frame) {
        fprintf(outfile,
                "%-80s %6d %-24s",
                get_python_file_name(frame),
                get_python_line_number(frame),
                get_python_function_name(frame)
                );
    } else {
        fprintf(outfile, "No Python frame available.");
    }
}

Then there is a simple little helper function that returns a string based on the event type:

static const char *watch_event_name(PyDict_WatchEvent event) {
    switch (event) {
        case PyDict_EVENT_ADDED:
            return "PyDict_EVENT_ADDED";
            break;
        case PyDict_EVENT_MODIFIED:
            return "PyDict_EVENT_MODIFIED";
            break;
        case PyDict_EVENT_DELETED:
            return "PyDict_EVENT_DELETED";
            break;
        case PyDict_EVENT_CLONED:
            return "PyDict_EVENT_CLONED";
            break;
        case PyDict_EVENT_CLEARED:
            return "PyDict_EVENT_CLEARED";
            break;
        case PyDict_EVENT_DEALLOCATED:
            return "PyDict_EVENT_DEALLOCATED";
            break;
        default:
            Py_UNREACHABLE();
            break;
    }
    return "PyDict_EVENT_UNKNOWN";
}

Now we define the callback function that reports the dictionary event to stdout. This calles all the functionas above and uses the CPython API PyObject_Print to print the representation of each object to stdout. This function has to respect NULL arguments:

static int dict_watcher_verbose(PyDict_WatchEvent event, PyObject *dict,
                                PyObject *key, PyObject *new_value) {
    fprintf(stdout, "Dict @ 0x%p: ", (void *)dict);
    write_frame_data_to_outfile(stdout, PyEval_GetFrame());
    fprintf(stdout, " Event: %-24s", watch_event_name(event));
    fprintf(stdout, "\n");
    if (dict) {
        fprintf(stdout, "    Dict: ");
        PyObject_Print(dict, stdout, Py_PRINT_RAW);
    } else {
        fprintf(stdout, "    Dict: NULL");
    }
    fprintf(stdout, "\n");
    if (key) {
        fprintf(stdout, "    Key (%s): ", Py_TYPE(key)->tp_name);
        PyObject_Print(key, stdout, Py_PRINT_RAW);
    } else {
        fprintf(stdout, "    Key : NULL");
    }
    fprintf(stdout, "\n");
    if (new_value) {
        fprintf(stdout, "    New value (%s): ", Py_TYPE(new_value)->tp_name);
        PyObject_Print(new_value, stdout, Py_PRINT_RAW);
    } else {
        fprintf(stdout, "    New value : NULL");
    }
    fprintf(stdout, "\n");
    return 0;
}

Finally we have the two implementations that register and unregister the callback using the low level Python C API. This uses PyDict_AddWatcher() and PyDict_Watch(). The first registers the callback, returning the watcher ID (error handling code omitted):

// Set watcher.
int dict_watcher_verbose_add(PyObject *dict) {
    int watcher_id = PyDict_AddWatcher(&dict_watcher_verbose);
    PyDict_Watch(watcher_id, dict);
    return watcher_id;
}

The second de-registers the callback, with the watcher ID and the dictionary in question (error handling code omitted). This uses PyDict_Unwatch() and PyDict_ClearWatcher().

// Remove watcher.
int dict_watcher_verbose_remove(int watcher_id, PyObject *dict) {
    PyDict_Unwatch(watcher_id, dict);
    PyDict_ClearWatcher(watcher_id);
    return 0;
}

18.1.2. Exposing This to CPython

Now we create a Python module cWatchers that exposes this low level C code to CPython. This code is in src/cpy/Watchers/cWatchers.c.

First some module level CPython wrappers around our underlying C code:

 1#define PPY_SSIZE_T_CLEAN
 2#include "Python.h"
 3
 4#include "DictWatcher.h"
 5
 6static PyObject *
 7py_dict_watcher_verbose_add(PyObject *Py_UNUSED(module), PyObject *arg) {
 8    if (!PyDict_Check(arg)) {
 9        PyErr_Format(
10            PyExc_TypeError,
11            "Argument must be a dict not type %s",
12            Py_TYPE(arg)->tp_name
13        );
14        return NULL;
15    }
16    long watcher_id = dict_watcher_verbose_add(arg);
17    return Py_BuildValue("l", watcher_id);
18}
19
20static PyObject *
21py_dict_watcher_verbose_remove(PyObject *Py_UNUSED(module), PyObject *args) {
22    long watcher_id;
23    PyObject *dict = NULL;
24
25    if (!PyArg_ParseTuple(args, "lO", &watcher_id, &dict)) {
26        return NULL;
27    }
28
29    if (!PyDict_Check(dict)) {
30        PyErr_Format(
31            PyExc_TypeError,
32            "Argument must be a dict not type %s",
33            Py_TYPE(dict)->tp_name
34        );
35        return NULL;
36    }
37    long result = dict_watcher_verbose_remove(watcher_id, dict);
38    return Py_BuildValue("l", result);
39}

Now create the table of module methods:

static PyMethodDef module_methods[] = {
        {"py_dict_watcher_verbose_add",
                (PyCFunction) py_dict_watcher_verbose_add,
                METH_O,
                "Adds watcher to a dictionary. Returns the watcher ID."
        },
        {"py_dict_watcher_verbose_remove",
                (PyCFunction) py_dict_watcher_verbose_remove,
                METH_VARARGS,
                "Removes the watcher ID from the dictionary."
        },
        {NULL, NULL, 0, NULL} /* Sentinel */
};

18.1.3. Creating the Context Manager

These are all fine but to be Pythonic it would be helpful to create a Context Manager (see Context Managers) in C. The context manager holds a reference to the dictionary and the watcher ID. Here is the definition which holds a watcher ID and a reference to the dictionary:

typedef struct {
    PyObject_HEAD
    int watcher_id;
    PyObject *dict;
} PyDictWatcher;

Here is the creation code:

static PyDictWatcher *
PyDictWatcher_new(PyObject *Py_UNUSED(arg)) {
    PyDictWatcher *self;
    self = PyObject_New(PyDictWatcher, &PyDictWatcher_Type);
    if (self == NULL) {
        return NULL;
    }
    self->watcher_id = -1;
    self->dict = NULL;
    return self;
}

static PyObject *
PyDictWatcher_init(PyDictWatcher *self, PyObject *args) {
    if (!PyArg_ParseTuple(args, "O", &self->dict)) {
        return NULL;
    }
    if (!PyDict_Check(self->dict)) {
        PyErr_Format(
            PyExc_TypeError,
            "Argument must be a dictionary not a %s",
            Py_TYPE(self->dict)->tp_name
        );
        return NULL;
    }
    Py_INCREF(self->dict);
    return (PyObject *)self;
}

The destruction code just decrements the reference count of the dictionary:

static void
PyDictWatcher_dealloc(PyDictWatcher *self) {
    Py_DECREF(self->dict);
    PyObject_Del(self);
}

Now the code that provides the context manager’s __enter__ and __exit__ methods. First the __enter__ function, this uses the low level C function dict_watcher_verbose_add() to mark the dictionary as watched and hold the watcher ID:

static PyObject *
PyDictWatcher_enter(PyDictWatcher *self, PyObject *Py_UNUSED(args)) {
    self->watcher_id = dict_watcher_verbose_add(self->dict);
    Py_INCREF(self);
    return (PyObject *)self;
}

Now the __exit__ function, this uses the low level C function dict_watcher_verbose_remove() to remove the watcher from the dictionary:

static PyObject *
PyDictWatcher_exit(PyDictWatcher *self, PyObject *Py_UNUSED(args)) {
    int result = dict_watcher_verbose_remove(self->watcher_id, self->dict);
    if (result) {
        PyErr_Format(
            PyExc_RuntimeError,
            "dict_watcher_verbose_remove() returned %d",
            result
        );
        Py_RETURN_TRUE;
    }
    Py_RETURN_FALSE;
}

Now we define the context manager methods and type:

static PyMethodDef PyDictWatcher_methods[] = {
        {"__enter__", (PyCFunction) PyDictWatcher_enter, METH_VARARGS,
                PyDoc_STR("__enter__() -> PyDictWatcher")},
        {"__exit__", (PyCFunction) PyDictWatcher_exit, METH_VARARGS,
                PyDoc_STR("__exit__(exc_type, exc_value, exc_tb) -> bool")},
        {NULL, NULL, 0, NULL} /* sentinel */
};

static PyTypeObject PyDictWatcher_Type = {
        PyVarObject_HEAD_INIT(NULL, 0)
        .tp_name = "cWatchers.PyDictWatcher",
        .tp_basicsize = sizeof(PyDictWatcher),
        .tp_dealloc = (destructor) PyDictWatcher_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
        .tp_methods = PyDictWatcher_methods,
        .tp_new = (newfunc) PyDictWatcher_new,
        .tp_init = (initproc) PyDictWatcher_init
};

18.1.4. Module, Setup and Test

Now we create the cWatchers module,

static PyModuleDef cWatchers = {
        PyModuleDef_HEAD_INIT,
        .m_name = "cWatchers",
        .m_doc = "Dictionary and type watchers.",
        .m_size = -1,
        .m_methods = module_methods,
};

PyMODINIT_FUNC PyInit_cWatchers(void) {
    PyObject *m = PyModule_Create(&cWatchers);
    if (!m) {
        goto fail;
    }
    if (PyType_Ready(&PyDictWatcher_Type) < 0) {
        goto fail;
    }
    if (PyModule_AddObject(m, "PyDictWatcher", (PyObject *) &PyDictWatcher_Type)) {
        goto fail;
    }
    return m;
fail:
    Py_XDECREF(m);
    return NULL;
}

And then in setup.py we add the extension:

Extension(
    name=f"{PACKAGE_NAME}.cWatchers",
    include_dirs=['src/cpy', ],
    sources=[
        "src/cpy/Watchers/cWatchers.c",
        "src/cpy/Watchers/DictWatcher.c",
        "src/cpy/pyextpatt_util.c",
    ],
    extra_compile_args=extra_compile_args_c,
    language='c',
),

And it can be used like this:

from cPyExtPatt import cWatchers

d = {}
with cWatchers.PyDictWatcher(d):
    d['age'] = 42

And the result on stdout is something like:

Dict @ 0x0x10fb53c00: watcher_example.py 12 dict_watcher_demo PyDict_EVENT_ADDED
    Dict: {}
    Key (str): age
    New value (int): 42

18.1.5. Without the Context Manager

If you are putting in some debugging code then a context manager might not be convenient. cWatchers provides two functions, py_dict_watcher_verbose_add() and py_dict_watcher_verbose_remove that achieve the same aim:

from cPyExtPatt import cWatchers

d = {}
watcher_id = cWatchers.py_dict_watcher_verbose_add(d)
d['age'] = 42
cWatchers.py_dict_watcher_verbose_remove(watcher_id, d)

18.2. Type Watchers

These allow a callback when a type is modified.

This beyond the scope of this version of this document.

More information can be found in https://docs.python.org/3/c-api/type.html

18.3. Function Watchers

These allow a callback when a function is created and destroyed.

This beyond the scope of this version of this document.

More information can be found in https://docs.python.org/3/c-api/function.html

18.4. Code Watchers

These allow a callback when code is created and destroyed.

This beyond the scope of this version of this document.

More information can be found in https://docs.python.org/3/c-api/code.html

Footnotes