summaryrefslogtreecommitdiff
path: root/Objects/funcobject.c
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2022-12-14 11:14:16 -0800
committerGitHub <noreply@github.com>2022-12-14 19:14:16 +0000
commitae83c782155ffe86830c3255e338f366e331ad30 (patch)
tree353db1dc5287520c07b951467461deb8a10a84c4 /Objects/funcobject.c
parentaa8591e9ca9a3e55e96a03c17fa9d3902f0674dc (diff)
downloadcpython-git-ae83c782155ffe86830c3255e338f366e331ad30.tar.gz
GH-100000: Cleanup and polish various watchers code (GH-99998)
* Initialize `type_watchers` array to `NULL`s * Optimize code watchers notification * Optimize func watchers notification
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r--Objects/funcobject.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 9df0652058..d5cf5b9277 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -12,11 +12,20 @@ static void
notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
PyFunctionObject *func, PyObject *new_value)
{
- for (int i = 0; i < FUNC_MAX_WATCHERS; i++) {
- PyFunction_WatchCallback cb = interp->func_watchers[i];
- if ((cb != NULL) && (cb(event, func, new_value) < 0)) {
- PyErr_WriteUnraisable((PyObject *) func);
+ uint8_t bits = interp->active_func_watchers;
+ int i = 0;
+ while (bits) {
+ assert(i < FUNC_MAX_WATCHERS);
+ if (bits & 1) {
+ PyFunction_WatchCallback cb = interp->func_watchers[i];
+ // callback must be non-null if the watcher bit is set
+ assert(cb != NULL);
+ if (cb(event, func, new_value) < 0) {
+ PyErr_WriteUnraisable((PyObject *) func);
+ }
}
+ i++;
+ bits >>= 1;
}
}
@@ -25,6 +34,7 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
PyObject *new_value)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
+ assert(interp->_initialized);
if (interp->active_func_watchers) {
notify_func_watchers(interp, event, func, new_value);
}