Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Conform to PEP-7
  • Loading branch information
mpage committed Nov 8, 2022
commit 896d70879b2ade0f47026adc7293b40c1dcd30bc
6 changes: 3 additions & 3 deletions Lib/test/test_func_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def myfunc():
self.assertIsInstance(cm.unraisable.exc_value, MyError)

def test_clear_out_of_range_watcher_id(self):
with self.assertRaisesRegex(ValueError, r"Invalid func watcher ID -1"):
with self.assertRaisesRegex(ValueError, r"invalid func watcher ID -1"):
_clear_func_watcher(-1)
with self.assertRaisesRegex(ValueError, r"Invalid func watcher ID 8"):
with self.assertRaisesRegex(ValueError, r"invalid func watcher ID 8"):
_clear_func_watcher(8) # FUNC_MAX_WATCHERS = 8

def test_clear_unassigned_watcher_id(self):
with self.assertRaisesRegex(ValueError, r"No func watcher set for ID 1"):
with self.assertRaisesRegex(ValueError, r"no func watcher set for ID 1"):
_clear_func_watcher(1)
38 changes: 22 additions & 16 deletions Modules/_testcapi/func_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
static PyObject *pyfunc_watchers[NUM_WATCHERS];
static int watcher_ids[NUM_WATCHERS] = {-1, -1};

static PyObject *get_id(PyObject *obj) {
PyObject *builtins = PyEval_GetBuiltins();
static
PyObject *get_id(PyObject *obj)
Comment thread
mpage marked this conversation as resolved.
Outdated
{
PyObject *builtins = PyEval_GetBuiltins(); // borrowed ref.
if (builtins == NULL) {
return NULL;
}
Expand All @@ -25,7 +27,8 @@ static PyObject *get_id(PyObject *obj) {
}

static int
call_pyfunc_watcher(PyObject *watcher, PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value)
call_pyfunc_watcher(PyObject *watcher, PyFunction_WatchEvent event,
PyFunctionObject *func, PyObject *new_value)
{
PyObject *event_obj = PyLong_FromLong(event);
if (event_obj == NULL) {
Expand All @@ -44,7 +47,8 @@ call_pyfunc_watcher(PyObject *watcher, PyFunction_WatchEvent event, PyFunctionOb
Py_DECREF(new_value);
return -1;
}
} else {
}
else {
Py_INCREF(func);
func_or_id = (PyObject *) func;
}
Expand All @@ -59,20 +63,22 @@ call_pyfunc_watcher(PyObject *watcher, PyFunction_WatchEvent event, PyFunctionOb
}

static int
first_watcher_callback(PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value)
first_watcher_callback(PyFunction_WatchEvent event, PyFunctionObject *func,
PyObject *new_value)
{
return call_pyfunc_watcher(pyfunc_watchers[0], event, func, new_value);
}

static int
second_watcher_callback(PyFunction_WatchEvent event, PyFunctionObject *func, PyObject *new_value)
second_watcher_callback(PyFunction_WatchEvent event, PyFunctionObject *func,
PyObject *new_value)
{
return call_pyfunc_watcher(pyfunc_watchers[1], event, func, new_value);
}

static PyFunction_WatchCallback watcher_callbacks[NUM_WATCHERS] = {
first_watcher_callback,
second_watcher_callback
first_watcher_callback,
second_watcher_callback
};

static int
Expand Down Expand Up @@ -114,8 +120,7 @@ add_watcher(PyObject *self, PyObject *func)
Py_DECREF(result);
return NULL;
}
pyfunc_watchers[idx] = func;
Py_INCREF(func);
pyfunc_watchers[idx] = Py_NewRef(func);
return result;
}

Expand All @@ -124,7 +129,7 @@ clear_watcher(PyObject *self, PyObject *watcher_id_obj)
{
long watcher_id = PyLong_AsLong(watcher_id_obj);
if ((watcher_id < INT_MIN) || (watcher_id > INT_MAX)) {
Comment thread
mpage marked this conversation as resolved.
Outdated
PyErr_SetString(PyExc_ValueError, "invalid watcher id");
PyErr_SetString(PyExc_ValueError, "invalid watcher ID");
return NULL;
}
int wid = (int) watcher_id;
Expand All @@ -133,10 +138,10 @@ clear_watcher(PyObject *self, PyObject *watcher_id_obj)
}
int idx = -1;
for (int i = 0; i < NUM_WATCHERS; i++) {
if (watcher_ids[i] == wid) {
idx = i;
break;
}
if (watcher_ids[i] == wid) {
idx = i;
break;
}
}
assert(idx != -1);
Py_CLEAR(pyfunc_watchers[idx]);
Expand All @@ -151,7 +156,8 @@ static PyMethodDef TestMethods[] = {
};

int
_PyTestCapi_Init_FuncEvents(PyObject *m) {
_PyTestCapi_Init_FuncEvents(PyObject *m)
{
if (PyModule_AddFunctions(m, TestMethods) < 0) {
return -1;
}
Expand Down
7 changes: 4 additions & 3 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ PyFunction_AddWatcher(PyFunction_WatchCallback callback)
}
PyErr_SetString(PyExc_RuntimeError, "no more func watcher IDs available");
return -1;
Comment thread
mpage marked this conversation as resolved.

}

int
PyFunction_ClearWatcher(int watcher_id)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
if (watcher_id < 0 || watcher_id >= FUNC_MAX_WATCHERS) {
PyErr_Format(PyExc_ValueError, "Invalid func watcher ID %d", watcher_id);
PyErr_Format(PyExc_ValueError, "invalid func watcher ID %d",
watcher_id);
return -1;
}
if (!interp->func_watchers[watcher_id]) {
PyErr_Format(PyExc_ValueError, "No func watcher set for ID %d", watcher_id);
PyErr_Format(PyExc_ValueError, "no func watcher set for ID %d",
watcher_id);
return -1;
}
interp->func_watchers[watcher_id] = NULL;
Expand Down