summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapimodule.c
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend.aasland@protonmail.com>2022-11-16 19:13:32 (GMT)
committerGitHub <noreply@github.com>2022-11-16 19:13:32 (GMT)
commit51d10354f43c708ebbb7d13c14bf0ea52b28489e (patch)
treebce59a3bca311fcd9192403265c806e0f72baa68 /Modules/_testcapimodule.c
parent9db1e17c80217d7b18a2f44297d72acd10ee73d6 (diff)
downloadcpython-51d10354f43c708ebbb7d13c14bf0ea52b28489e.zip
cpython-51d10354f43c708ebbb7d13c14bf0ea52b28489e.tar.gz
cpython-51d10354f43c708ebbb7d13c14bf0ea52b28489e.tar.bz2
gh-93649: Split watcher API tests from _testcapimodule.c (#99532)
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r--Modules/_testcapimodule.c288
1 files changed, 3 insertions, 285 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 2c21782..01b3923 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3390,159 +3390,6 @@ test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}
-
-// Test dict watching
-static PyObject *g_dict_watch_events;
-static int g_dict_watchers_installed;
-
-static int
-dict_watch_callback(PyDict_WatchEvent event,
- PyObject *dict,
- PyObject *key,
- PyObject *new_value)
-{
- PyObject *msg;
- switch(event) {
- case PyDict_EVENT_CLEARED:
- msg = PyUnicode_FromString("clear");
- break;
- case PyDict_EVENT_DEALLOCATED:
- msg = PyUnicode_FromString("dealloc");
- break;
- case PyDict_EVENT_CLONED:
- msg = PyUnicode_FromString("clone");
- break;
- case PyDict_EVENT_ADDED:
- msg = PyUnicode_FromFormat("new:%S:%S", key, new_value);
- break;
- case PyDict_EVENT_MODIFIED:
- msg = PyUnicode_FromFormat("mod:%S:%S", key, new_value);
- break;
- case PyDict_EVENT_DELETED:
- msg = PyUnicode_FromFormat("del:%S", key);
- break;
- default:
- msg = PyUnicode_FromString("unknown");
- }
- if (!msg) {
- return -1;
- }
- assert(PyList_Check(g_dict_watch_events));
- if (PyList_Append(g_dict_watch_events, msg) < 0) {
- Py_DECREF(msg);
- return -1;
- }
- Py_DECREF(msg);
- return 0;
-}
-
-static int
-dict_watch_callback_second(PyDict_WatchEvent event,
- PyObject *dict,
- PyObject *key,
- PyObject *new_value)
-{
- PyObject *msg = PyUnicode_FromString("second");
- if (!msg) {
- return -1;
- }
- if (PyList_Append(g_dict_watch_events, msg) < 0) {
- Py_DECREF(msg);
- return -1;
- }
- Py_DECREF(msg);
- return 0;
-}
-
-static int
-dict_watch_callback_error(PyDict_WatchEvent event,
- PyObject *dict,
- PyObject *key,
- PyObject *new_value)
-{
- PyErr_SetString(PyExc_RuntimeError, "boom!");
- return -1;
-}
-
-static PyObject *
-add_dict_watcher(PyObject *self, PyObject *kind)
-{
- int watcher_id;
- assert(PyLong_Check(kind));
- long kind_l = PyLong_AsLong(kind);
- if (kind_l == 2) {
- watcher_id = PyDict_AddWatcher(dict_watch_callback_second);
- } else if (kind_l == 1) {
- watcher_id = PyDict_AddWatcher(dict_watch_callback_error);
- } else {
- watcher_id = PyDict_AddWatcher(dict_watch_callback);
- }
- if (watcher_id < 0) {
- return NULL;
- }
- if (!g_dict_watchers_installed) {
- assert(!g_dict_watch_events);
- if (!(g_dict_watch_events = PyList_New(0))) {
- return NULL;
- }
- }
- g_dict_watchers_installed++;
- return PyLong_FromLong(watcher_id);
-}
-
-static PyObject *
-clear_dict_watcher(PyObject *self, PyObject *watcher_id)
-{
- if (PyDict_ClearWatcher(PyLong_AsLong(watcher_id))) {
- return NULL;
- }
- g_dict_watchers_installed--;
- if (!g_dict_watchers_installed) {
- assert(g_dict_watch_events);
- Py_CLEAR(g_dict_watch_events);
- }
- Py_RETURN_NONE;
-}
-
-static PyObject *
-watch_dict(PyObject *self, PyObject *args)
-{
- PyObject *dict;
- int watcher_id;
- if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) {
- return NULL;
- }
- if (PyDict_Watch(watcher_id, dict)) {
- return NULL;
- }
- Py_RETURN_NONE;
-}
-
-static PyObject *
-unwatch_dict(PyObject *self, PyObject *args)
-{
- PyObject *dict;
- int watcher_id;
- if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) {
- return NULL;
- }
- if (PyDict_Unwatch(watcher_id, dict)) {
- return NULL;
- }
- Py_RETURN_NONE;
-}
-
-static PyObject *
-get_dict_watcher_events(PyObject *self, PyObject *Py_UNUSED(args))
-{
- if (!g_dict_watch_events) {
- PyErr_SetString(PyExc_RuntimeError, "no watchers active");
- return NULL;
- }
- return Py_NewRef(g_dict_watch_events);
-}
-
-
// Test PyFloat_Pack2(), PyFloat_Pack4() and PyFloat_Pack8()
static PyObject *
test_float_pack(PyObject *self, PyObject *args)
@@ -3988,128 +3835,6 @@ function_set_kw_defaults(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
-
-// type watchers
-
-static PyObject *g_type_modified_events;
-static int g_type_watchers_installed;
-
-static int
-type_modified_callback(PyTypeObject *type)
-{
- assert(PyList_Check(g_type_modified_events));
- if(PyList_Append(g_type_modified_events, (PyObject *)type) < 0) {
- return -1;
- }
- return 0;
-}
-
-static int
-type_modified_callback_wrap(PyTypeObject *type)
-{
- assert(PyList_Check(g_type_modified_events));
- PyObject *list = PyList_New(0);
- if (!list) {
- return -1;
- }
- if (PyList_Append(list, (PyObject *)type) < 0) {
- Py_DECREF(list);
- return -1;
- }
- if (PyList_Append(g_type_modified_events, list) < 0) {
- Py_DECREF(list);
- return -1;
- }
- Py_DECREF(list);
- return 0;
-}
-
-static int
-type_modified_callback_error(PyTypeObject *type)
-{
- PyErr_SetString(PyExc_RuntimeError, "boom!");
- return -1;
-}
-
-static PyObject *
-add_type_watcher(PyObject *self, PyObject *kind)
-{
- int watcher_id;
- assert(PyLong_Check(kind));
- long kind_l = PyLong_AsLong(kind);
- if (kind_l == 2) {
- watcher_id = PyType_AddWatcher(type_modified_callback_wrap);
- } else if (kind_l == 1) {
- watcher_id = PyType_AddWatcher(type_modified_callback_error);
- } else {
- watcher_id = PyType_AddWatcher(type_modified_callback);
- }
- if (watcher_id < 0) {
- return NULL;
- }
- if (!g_type_watchers_installed) {
- assert(!g_type_modified_events);
- if (!(g_type_modified_events = PyList_New(0))) {
- return NULL;
- }
- }
- g_type_watchers_installed++;
- return PyLong_FromLong(watcher_id);
-}
-
-static PyObject *
-clear_type_watcher(PyObject *self, PyObject *watcher_id)
-{
- if (PyType_ClearWatcher(PyLong_AsLong(watcher_id))) {
- return NULL;
- }
- g_type_watchers_installed--;
- if (!g_type_watchers_installed) {
- assert(g_type_modified_events);
- Py_CLEAR(g_type_modified_events);
- }
- Py_RETURN_NONE;
-}
-
-static PyObject *
-get_type_modified_events(PyObject *self, PyObject *Py_UNUSED(args))
-{
- if (!g_type_modified_events) {
- PyErr_SetString(PyExc_RuntimeError, "no watchers active");
- return NULL;
- }
- return Py_NewRef(g_type_modified_events);
-}
-
-static PyObject *
-watch_type(PyObject *self, PyObject *args)
-{
- PyObject *type;
- int watcher_id;
- if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) {
- return NULL;
- }
- if (PyType_Watch(watcher_id, type)) {
- return NULL;
- }
- Py_RETURN_NONE;
-}
-
-static PyObject *
-unwatch_type(PyObject *self, PyObject *args)
-{
- PyObject *type;
- int watcher_id;
- if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) {
- return NULL;
- }
- if (PyType_Unwatch(watcher_id, type)) {
- return NULL;
- }
- Py_RETURN_NONE;
-}
-
-
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
static PyMethodDef TestMethods[] = {
@@ -4259,11 +3984,6 @@ static PyMethodDef TestMethods[] = {
{"settrace_to_record", settrace_to_record, METH_O, NULL},
{"test_macros", test_macros, METH_NOARGS, NULL},
{"clear_managed_dict", clear_managed_dict, METH_O, NULL},
- {"add_dict_watcher", add_dict_watcher, METH_O, NULL},
- {"clear_dict_watcher", clear_dict_watcher, METH_O, NULL},
- {"watch_dict", watch_dict, METH_VARARGS, NULL},
- {"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
- {"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL},
{"function_get_code", function_get_code, METH_O, NULL},
{"function_get_globals", function_get_globals, METH_O, NULL},
{"function_get_module", function_get_module, METH_O, NULL},
@@ -4271,11 +3991,6 @@ static PyMethodDef TestMethods[] = {
{"function_set_defaults", function_set_defaults, METH_VARARGS, NULL},
{"function_get_kw_defaults", function_get_kw_defaults, METH_O, NULL},
{"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
- {"add_type_watcher", add_type_watcher, METH_O, NULL},
- {"clear_type_watcher", clear_type_watcher, METH_O, NULL},
- {"watch_type", watch_type, METH_VARARGS, NULL},
- {"unwatch_type", unwatch_type, METH_VARARGS, NULL},
- {"get_type_modified_events", get_type_modified_events, METH_NOARGS, NULL},
{NULL, NULL} /* sentinel */
};
@@ -5096,6 +4811,9 @@ PyInit__testcapi(void)
if (_PyTestCapi_Init_Mem(m) < 0) {
return NULL;
}
+ if (_PyTestCapi_Init_Watchers(m) < 0) {
+ return NULL;
+ }
#ifndef LIMITED_API_AVAILABLE
PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_False);