From 456d56698db6c6287500f591927c900a5f5221ca Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 15 May 2023 23:59:41 +0300 Subject: [PATCH] gh-104469: Convert _testcapi/watchers.c to use Argument Clinic (#104503) Remove boilerplate code by converting the following functions: - _testcapi.watch_dict - _testcapi.unwatch_dict - _testcapi.watch_type - _testcapi.unwatch_type - _testcapi.set_func_defaults_via_capi - _testcapi.set_func_kwdefaults_via_capi --- Modules/_testcapi/clinic/watchers.c.h | 198 ++++++++++++++++++++++++++ Modules/_testcapi/watchers.c | 104 ++++++++------ 2 files changed, 259 insertions(+), 43 deletions(-) create mode 100644 Modules/_testcapi/clinic/watchers.c.h diff --git a/Modules/_testcapi/clinic/watchers.c.h b/Modules/_testcapi/clinic/watchers.c.h new file mode 100644 index 00000000000..975244bd59a --- /dev/null +++ b/Modules/_testcapi/clinic/watchers.c.h @@ -0,0 +1,198 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) +# include "pycore_gc.h" // PyGC_Head +# include "pycore_runtime.h" // _Py_ID() +#endif + + +PyDoc_STRVAR(_testcapi_watch_dict__doc__, +"watch_dict($module, watcher_id, dict, /)\n" +"--\n" +"\n"); + +#define _TESTCAPI_WATCH_DICT_METHODDEF \ + {"watch_dict", _PyCFunction_CAST(_testcapi_watch_dict), METH_FASTCALL, _testcapi_watch_dict__doc__}, + +static PyObject * +_testcapi_watch_dict_impl(PyObject *module, int watcher_id, PyObject *dict); + +static PyObject * +_testcapi_watch_dict(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int watcher_id; + PyObject *dict; + + if (!_PyArg_CheckPositional("watch_dict", nargs, 2, 2)) { + goto exit; + } + watcher_id = _PyLong_AsInt(args[0]); + if (watcher_id == -1 && PyErr_Occurred()) { + goto exit; + } + dict = args[1]; + return_value = _testcapi_watch_dict_impl(module, watcher_id, dict); + +exit: + return return_value; +} + +PyDoc_STRVAR(_testcapi_unwatch_dict__doc__, +"unwatch_dict($module, watcher_id, dict, /)\n" +"--\n" +"\n"); + +#define _TESTCAPI_UNWATCH_DICT_METHODDEF \ + {"unwatch_dict", _PyCFunction_CAST(_testcapi_unwatch_dict), METH_FASTCALL, _testcapi_unwatch_dict__doc__}, + +static PyObject * +_testcapi_unwatch_dict_impl(PyObject *module, int watcher_id, PyObject *dict); + +static PyObject * +_testcapi_unwatch_dict(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int watcher_id; + PyObject *dict; + + if (!_PyArg_CheckPositional("unwatch_dict", nargs, 2, 2)) { + goto exit; + } + watcher_id = _PyLong_AsInt(args[0]); + if (watcher_id == -1 && PyErr_Occurred()) { + goto exit; + } + dict = args[1]; + return_value = _testcapi_unwatch_dict_impl(module, watcher_id, dict); + +exit: + return return_value; +} + +PyDoc_STRVAR(_testcapi_watch_type__doc__, +"watch_type($module, watcher_id, type, /)\n" +"--\n" +"\n"); + +#define _TESTCAPI_WATCH_TYPE_METHODDEF \ + {"watch_type", _PyCFunction_CAST(_testcapi_watch_type), METH_FASTCALL, _testcapi_watch_type__doc__}, + +static PyObject * +_testcapi_watch_type_impl(PyObject *module, int watcher_id, PyObject *type); + +static PyObject * +_testcapi_watch_type(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int watcher_id; + PyObject *type; + + if (!_PyArg_CheckPositional("watch_type", nargs, 2, 2)) { + goto exit; + } + watcher_id = _PyLong_AsInt(args[0]); + if (watcher_id == -1 && PyErr_Occurred()) { + goto exit; + } + type = args[1]; + return_value = _testcapi_watch_type_impl(module, watcher_id, type); + +exit: + return return_value; +} + +PyDoc_STRVAR(_testcapi_unwatch_type__doc__, +"unwatch_type($module, watcher_id, type, /)\n" +"--\n" +"\n"); + +#define _TESTCAPI_UNWATCH_TYPE_METHODDEF \ + {"unwatch_type", _PyCFunction_CAST(_testcapi_unwatch_type), METH_FASTCALL, _testcapi_unwatch_type__doc__}, + +static PyObject * +_testcapi_unwatch_type_impl(PyObject *module, int watcher_id, PyObject *type); + +static PyObject * +_testcapi_unwatch_type(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int watcher_id; + PyObject *type; + + if (!_PyArg_CheckPositional("unwatch_type", nargs, 2, 2)) { + goto exit; + } + watcher_id = _PyLong_AsInt(args[0]); + if (watcher_id == -1 && PyErr_Occurred()) { + goto exit; + } + type = args[1]; + return_value = _testcapi_unwatch_type_impl(module, watcher_id, type); + +exit: + return return_value; +} + +PyDoc_STRVAR(_testcapi_set_func_defaults_via_capi__doc__, +"set_func_defaults_via_capi($module, func, defaults, /)\n" +"--\n" +"\n"); + +#define _TESTCAPI_SET_FUNC_DEFAULTS_VIA_CAPI_METHODDEF \ + {"set_func_defaults_via_capi", _PyCFunction_CAST(_testcapi_set_func_defaults_via_capi), METH_FASTCALL, _testcapi_set_func_defaults_via_capi__doc__}, + +static PyObject * +_testcapi_set_func_defaults_via_capi_impl(PyObject *module, PyObject *func, + PyObject *defaults); + +static PyObject * +_testcapi_set_func_defaults_via_capi(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *func; + PyObject *defaults; + + if (!_PyArg_CheckPositional("set_func_defaults_via_capi", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + defaults = args[1]; + return_value = _testcapi_set_func_defaults_via_capi_impl(module, func, defaults); + +exit: + return return_value; +} + +PyDoc_STRVAR(_testcapi_set_func_kwdefaults_via_capi__doc__, +"set_func_kwdefaults_via_capi($module, func, defaults, /)\n" +"--\n" +"\n"); + +#define _TESTCAPI_SET_FUNC_KWDEFAULTS_VIA_CAPI_METHODDEF \ + {"set_func_kwdefaults_via_capi", _PyCFunction_CAST(_testcapi_set_func_kwdefaults_via_capi), METH_FASTCALL, _testcapi_set_func_kwdefaults_via_capi__doc__}, + +static PyObject * +_testcapi_set_func_kwdefaults_via_capi_impl(PyObject *module, PyObject *func, + PyObject *defaults); + +static PyObject * +_testcapi_set_func_kwdefaults_via_capi(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *func; + PyObject *defaults; + + if (!_PyArg_CheckPositional("set_func_kwdefaults_via_capi", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + defaults = args[1]; + return_value = _testcapi_set_func_kwdefaults_via_capi_impl(module, func, defaults); + +exit: + return return_value; +} +/*[clinic end generated code: output=12c375089125d165 input=a9049054013a1b77]*/ diff --git a/Modules/_testcapi/watchers.c b/Modules/_testcapi/watchers.c index 1284fdc2767..d2c71fb401d 100644 --- a/Modules/_testcapi/watchers.c +++ b/Modules/_testcapi/watchers.c @@ -1,9 +1,16 @@ #include "parts.h" +#include "clinic/watchers.c.h" + #define Py_BUILD_CORE #include "pycore_function.h" // FUNC_MAX_WATCHERS #include "pycore_code.h" // CODE_MAX_WATCHERS +/*[clinic input] +module _testcapi +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/ + // Test dict watching static PyObject *g_dict_watch_events; static int g_dict_watchers_installed; @@ -119,28 +126,31 @@ clear_dict_watcher(PyObject *self, PyObject *watcher_id) Py_RETURN_NONE; } +/*[clinic input] +_testcapi.watch_dict + watcher_id: int + dict: object + / +[clinic start generated code]*/ + static PyObject * -watch_dict(PyObject *self, PyObject *args) +_testcapi_watch_dict_impl(PyObject *module, int watcher_id, PyObject *dict) +/*[clinic end generated code: output=1426e0273cebe2d8 input=269b006d60c358bd]*/ { - 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; } +/*[clinic input] +_testcapi.unwatch_dict = _testcapi.watch_dict +[clinic start generated code]*/ + static PyObject * -unwatch_dict(PyObject *self, PyObject *args) +_testcapi_unwatch_dict_impl(PyObject *module, int watcher_id, PyObject *dict) +/*[clinic end generated code: output=512b1a71ae33c351 input=cae7dc1b6f7713b8]*/ { - PyObject *dict; - int watcher_id; - if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) { - return NULL; - } if (PyDict_Unwatch(watcher_id, dict)) { return NULL; } @@ -250,28 +260,31 @@ get_type_modified_events(PyObject *self, PyObject *Py_UNUSED(args)) return Py_NewRef(g_type_modified_events); } +/*[clinic input] +_testcapi.watch_type + watcher_id: int + type: object + / +[clinic start generated code]*/ + static PyObject * -watch_type(PyObject *self, PyObject *args) +_testcapi_watch_type_impl(PyObject *module, int watcher_id, PyObject *type) +/*[clinic end generated code: output=fdf4777126724fc4 input=5a808bf12be7e3ed]*/ { - 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; } +/*[clinic input] +_testcapi.unwatch_type = _testcapi.watch_type +[clinic start generated code]*/ + static PyObject * -unwatch_type(PyObject *self, PyObject *args) +_testcapi_unwatch_type_impl(PyObject *module, int watcher_id, PyObject *type) +/*[clinic end generated code: output=0389672d4ad5f68b input=6701911fb45edc9e]*/ { - PyObject *type; - int watcher_id; - if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) { - return NULL; - } if (PyType_Unwatch(watcher_id, type)) { return NULL; } @@ -605,29 +618,34 @@ allocate_too_many_func_watchers(PyObject *self, PyObject *args) Py_RETURN_NONE; } +/*[clinic input] +_testcapi.set_func_defaults_via_capi + func: object + defaults: object + / +[clinic start generated code]*/ + static PyObject * -set_func_defaults(PyObject *self, PyObject *args) +_testcapi_set_func_defaults_via_capi_impl(PyObject *module, PyObject *func, + PyObject *defaults) +/*[clinic end generated code: output=caf0cb39db31ac24 input=e04a8508ca9d42fc]*/ { - PyObject *func = NULL; - PyObject *defaults = NULL; - if (!PyArg_ParseTuple(args, "OO", &func, &defaults)) { - return NULL; - } if (PyFunction_SetDefaults(func, defaults) < 0) { return NULL; } Py_RETURN_NONE; } +/*[clinic input] +_testcapi.set_func_kwdefaults_via_capi = _testcapi.set_func_defaults_via_capi +[clinic start generated code]*/ + static PyObject * -set_func_kwdefaults(PyObject *self, PyObject *args) +_testcapi_set_func_kwdefaults_via_capi_impl(PyObject *module, PyObject *func, + PyObject *defaults) +/*[clinic end generated code: output=9ed3b08177025070 input=f3cd1ca3c18de8ce]*/ { - PyObject *func = NULL; - PyObject *kwdefaults = NULL; - if (!PyArg_ParseTuple(args, "OO", &func, &kwdefaults)) { - return NULL; - } - if (PyFunction_SetKwDefaults(func, kwdefaults) < 0) { + if (PyFunction_SetKwDefaults(func, defaults) < 0) { return NULL; } Py_RETURN_NONE; @@ -637,16 +655,16 @@ static PyMethodDef test_methods[] = { // Dict watchers. {"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}, + _TESTCAPI_WATCH_DICT_METHODDEF + _TESTCAPI_UNWATCH_DICT_METHODDEF {"get_dict_watcher_events", (PyCFunction) get_dict_watcher_events, METH_NOARGS, NULL}, // Type watchers. {"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}, + _TESTCAPI_WATCH_TYPE_METHODDEF + _TESTCAPI_UNWATCH_TYPE_METHODDEF {"get_type_modified_events", (PyCFunction) get_type_modified_events, METH_NOARGS, NULL}, @@ -663,8 +681,8 @@ static PyMethodDef test_methods[] = { // Function watchers. {"add_func_watcher", add_func_watcher, METH_O, NULL}, {"clear_func_watcher", clear_func_watcher, METH_O, NULL}, - {"set_func_defaults_via_capi", set_func_defaults, METH_VARARGS, NULL}, - {"set_func_kwdefaults_via_capi", set_func_kwdefaults, METH_VARARGS, NULL}, + _TESTCAPI_SET_FUNC_DEFAULTS_VIA_CAPI_METHODDEF + _TESTCAPI_SET_FUNC_KWDEFAULTS_VIA_CAPI_METHODDEF {"allocate_too_many_func_watchers", allocate_too_many_func_watchers, METH_NOARGS, NULL}, {NULL},