We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent aa8591e commit ae83c78Copy full SHA for ae83c78
Include/internal/pycore_interp.h
@@ -142,7 +142,6 @@ struct _is {
142
// Initialized to _PyEval_EvalFrameDefault().
143
_PyFrameEvalFunction eval_frame;
144
145
- PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];
146
PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
147
// One bit is set for each non-NULL entry in func_watchers
148
uint8_t active_func_watchers;
Modules/_testcapi/watchers.c
@@ -630,14 +630,16 @@ static PyMethodDef test_methods[] = {
630
{"clear_dict_watcher", clear_dict_watcher, METH_O, NULL},
631
{"watch_dict", watch_dict, METH_VARARGS, NULL},
632
{"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
633
- {"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL},
+ {"get_dict_watcher_events",
634
+ (PyCFunction) get_dict_watcher_events, METH_NOARGS, NULL},
635
636
// Type watchers.
637
{"add_type_watcher", add_type_watcher, METH_O, NULL},
638
{"clear_type_watcher", clear_type_watcher, METH_O, NULL},
639
{"watch_type", watch_type, METH_VARARGS, NULL},
640
{"unwatch_type", unwatch_type, METH_VARARGS, NULL},
- {"get_type_modified_events", get_type_modified_events, METH_NOARGS, NULL},
641
+ {"get_type_modified_events",
642
+ (PyCFunction) get_type_modified_events, METH_NOARGS, NULL},
643
644
// Code object watchers.
645
{"add_code_watcher", add_code_watcher, METH_O, NULL},
Objects/codeobject.c
@@ -15,14 +15,21 @@ static void
15
notify_code_watchers(PyCodeEvent event, PyCodeObject *co)
16
{
17
PyInterpreterState *interp = _PyInterpreterState_GET();
18
- if (interp->active_code_watchers) {
19
- assert(interp->_initialized);
20
- for (int i = 0; i < CODE_MAX_WATCHERS; i++) {
+ assert(interp->_initialized);
+ uint8_t bits = interp->active_code_watchers;
+ int i = 0;
21
+ while (bits) {
22
+ assert(i < CODE_MAX_WATCHERS);
23
+ if (bits & 1) {
24
PyCode_WatchCallback cb = interp->code_watchers[i];
- if ((cb != NULL) && (cb(event, co) < 0)) {
25
+ // callback must be non-null if the watcher bit is set
26
+ assert(cb != NULL);
27
+ if (cb(event, co) < 0) {
28
PyErr_WriteUnraisable((PyObject *) co);
29
}
30
31
+ i++;
32
+ bits >>= 1;
33
34
35
Objects/funcobject.c
@@ -12,11 +12,20 @@ static void
12
notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
13
PyFunctionObject *func, PyObject *new_value)
14
- 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;
+ assert(i < FUNC_MAX_WATCHERS);
+ PyFunction_WatchCallback cb = interp->func_watchers[i];
+ if (cb(event, func, new_value) < 0) {
+ PyErr_WriteUnraisable((PyObject *) func);
+ }
@@ -25,6 +34,7 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
PyObject *new_value)
36
37
38
if (interp->active_func_watchers) {
39
notify_func_watchers(interp, event, func, new_value);
40
Objects/typeobject.c
@@ -485,23 +485,24 @@ PyType_Modified(PyTypeObject *type)
485
486
487
488
+ // Notify registered type watchers, if any
489
if (type->tp_watched) {
490
491
int bits = type->tp_watched;
492
int i = 0;
- while(bits && i < TYPE_MAX_WATCHERS) {
493
494
+ assert(i < TYPE_MAX_WATCHERS);
495
if (bits & 1) {
496
PyType_WatchCallback cb = interp->type_watchers[i];
497
if (cb && (cb(type) < 0)) {
498
PyErr_WriteUnraisable((PyObject *)type);
499
500
- i += 1;
501
502
bits >>= 1;
503
504
505
-
506
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
507
type->tp_version_tag = 0; /* 0 is not a valid version tag */
508
Python/pystate.c
@@ -461,6 +461,10 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
461
interp->dict_state.watchers[i] = NULL;
462
463
464
+ for (int i=0; i < TYPE_MAX_WATCHERS; i++) {
465
+ interp->type_watchers[i] = NULL;
466
467
+
468
for (int i=0; i < FUNC_MAX_WATCHERS; i++) {
469
interp->func_watchers[i] = NULL;
470
0 commit comments