| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -142,7 +142,6 @@ struct _is { | |||
| 142 | 142 | // Initialized to _PyEval_EvalFrameDefault(). | |
| 143 | 143 | _PyFrameEvalFunction eval_frame; | |
| 144 | 144 | ||
| 145 | - PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS]; | ||
| 146 | 145 | PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS]; | |
| 147 | 146 | // One bit is set for each non-NULL entry in func_watchers | |
| 148 | 147 | uint8_t active_func_watchers; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -630,14 +630,16 @@ static PyMethodDef test_methods[] = { | |||
| 630 | 630 | {"clear_dict_watcher", clear_dict_watcher, METH_O, NULL}, | |
| 631 | 631 | {"watch_dict", watch_dict, METH_VARARGS, NULL}, | |
| 632 | 632 | {"unwatch_dict", unwatch_dict, METH_VARARGS, NULL}, | |
| 633 | - {"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL}, | ||
| 633 | + {"get_dict_watcher_events", | ||
| 634 | + (PyCFunction) get_dict_watcher_events, METH_NOARGS, NULL}, | ||
| 634 | 635 | ||
| 635 | 636 | // Type watchers. | |
| 636 | 637 | {"add_type_watcher", add_type_watcher, METH_O, NULL}, | |
| 637 | 638 | {"clear_type_watcher", clear_type_watcher, METH_O, NULL}, | |
| 638 | 639 | {"watch_type", watch_type, METH_VARARGS, NULL}, | |
| 639 | 640 | {"unwatch_type", unwatch_type, METH_VARARGS, NULL}, | |
| 640 | - {"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}, | ||
| 641 | 643 | ||
| 642 | 644 | // Code object watchers. | |
| 643 | 645 | {"add_code_watcher", add_code_watcher, METH_O, NULL}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,14 +15,21 @@ static void | |||
| 15 | 15 | notify_code_watchers(PyCodeEvent event, PyCodeObject *co) | |
| 16 | 16 | { | |
| 17 | 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++) { | ||
| 18 | + assert(interp->_initialized); | ||
| 19 | + uint8_t bits = interp->active_code_watchers; | ||
| 20 | + int i = 0; | ||
| 21 | + while (bits) { | ||
| 22 | + assert(i < CODE_MAX_WATCHERS); | ||
| 23 | + if (bits & 1) { | ||
| 21 | 24 | PyCode_WatchCallback cb = interp->code_watchers[i]; | |
| 22 | - 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) { | ||
| 23 | 28 | PyErr_WriteUnraisable((PyObject *) co); | |
| 24 | 29 | } | |
| 25 | 30 | } | |
| 31 | + i++; | ||
| 32 | + bits >>= 1; | ||
| 26 | 33 | } | |
| 27 | 34 | } | |
| 28 | 35 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,11 +12,20 @@ static void | |||
| 12 | 12 | notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event, | |
| 13 | 13 | PyFunctionObject *func, PyObject *new_value) | |
| 14 | 14 | { | |
| 15 | - for (int i = 0; i < FUNC_MAX_WATCHERS; i++) { | ||
| 16 | - PyFunction_WatchCallback cb = interp->func_watchers[i]; | ||
| 17 | - if ((cb != NULL) && (cb(event, func, new_value) < 0)) { | ||
| 18 | - PyErr_WriteUnraisable((PyObject *) func); | ||
| 15 | + uint8_t bits = interp->active_func_watchers; | ||
| 16 | + int i = 0; | ||
| 17 | + while (bits) { | ||
| 18 | + assert(i < FUNC_MAX_WATCHERS); | ||
| 19 | + if (bits & 1) { | ||
| 20 | + PyFunction_WatchCallback cb = interp->func_watchers[i]; | ||
| 21 | + // callback must be non-null if the watcher bit is set | ||
| 22 | + assert(cb != NULL); | ||
| 23 | + if (cb(event, func, new_value) < 0) { | ||
| 24 | + PyErr_WriteUnraisable((PyObject *) func); | ||
| 25 | + } | ||
| 19 | 26 | } | |
| 27 | + i++; | ||
| 28 | + bits >>= 1; | ||
| 20 | 29 | } | |
| 21 | 30 | } | |
| 22 | 31 | ||
@@ -25,6 +34,7 @@ handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func, | |||
| 25 | 34 | PyObject *new_value) | |
| 26 | 35 | { | |
| 27 | 36 | PyInterpreterState *interp = _PyInterpreterState_GET(); | |
| 37 | + assert(interp->_initialized); | ||
| 28 | 38 | if (interp->active_func_watchers) { | |
| 29 | 39 | notify_func_watchers(interp, event, func, new_value); | |
| 30 | 40 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -485,23 +485,24 @@ PyType_Modified(PyTypeObject *type) | |||
| 485 | 485 | } | |
| 486 | 486 | } | |
| 487 | 487 | ||
| 488 | + // Notify registered type watchers, if any | ||
| 488 | 489 | if (type->tp_watched) { | |
| 489 | 490 | PyInterpreterState *interp = _PyInterpreterState_GET(); | |
| 490 | 491 | int bits = type->tp_watched; | |
| 491 | 492 | int i = 0; | |
| 492 | - while(bits && i < TYPE_MAX_WATCHERS) { | ||
| 493 | + while (bits) { | ||
| 494 | + assert(i < TYPE_MAX_WATCHERS); | ||
| 493 | 495 | if (bits & 1) { | |
| 494 | 496 | PyType_WatchCallback cb = interp->type_watchers[i]; | |
| 495 | 497 | if (cb && (cb(type) < 0)) { | |
| 496 | 498 | PyErr_WriteUnraisable((PyObject *)type); | |
| 497 | 499 | } | |
| 498 | 500 | } | |
| 499 | - i += 1; | ||
| 501 | + i++; | ||
| 500 | 502 | bits >>= 1; | |
| 501 | 503 | } | |
| 502 | 504 | } | |
| 503 | 505 | ||
| 504 | - | ||
| 505 | 506 | type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; | |
| 506 | 507 | type->tp_version_tag = 0; /* 0 is not a valid version tag */ | |
| 507 | 508 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -461,6 +461,10 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) | |||
| 461 | 461 | interp->dict_state.watchers[i] = NULL; | |
| 462 | 462 | } | |
| 463 | 463 | ||
| 464 | + for (int i=0; i < TYPE_MAX_WATCHERS; i++) { | ||
| 465 | + interp->type_watchers[i] = NULL; | ||
| 466 | + } | ||
| 467 | + | ||
| 464 | 468 | for (int i=0; i < FUNC_MAX_WATCHERS; i++) { | |
| 465 | 469 | interp->func_watchers[i] = NULL; | |
| 466 | 470 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments