Skip to content

Commit 456d566

Browse files
authored
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
1 parent 8a3702f commit 456d566

File tree

2 files changed

+259
-43
lines changed

2 files changed

+259
-43
lines changed

Modules/_testcapi/clinic/watchers.c.h

Lines changed: 198 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_testcapi/watchers.c

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#include "parts.h"
22

3+
#include "clinic/watchers.c.h"
4+
35
#define Py_BUILD_CORE
46
#include "pycore_function.h" // FUNC_MAX_WATCHERS
57
#include "pycore_code.h" // CODE_MAX_WATCHERS
68

9+
/*[clinic input]
10+
module _testcapi
11+
[clinic start generated code]*/
12+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/
13+
714
// Test dict watching
815
static PyObject *g_dict_watch_events;
916
static int g_dict_watchers_installed;
@@ -119,28 +126,31 @@ clear_dict_watcher(PyObject *self, PyObject *watcher_id)
119126
Py_RETURN_NONE;
120127
}
121128

129+
/*[clinic input]
130+
_testcapi.watch_dict
131+
watcher_id: int
132+
dict: object
133+
/
134+
[clinic start generated code]*/
135+
122136
static PyObject *
123-
watch_dict(PyObject *self, PyObject *args)
137+
_testcapi_watch_dict_impl(PyObject *module, int watcher_id, PyObject *dict)
138+
/*[clinic end generated code: output=1426e0273cebe2d8 input=269b006d60c358bd]*/
124139
{
125-
PyObject *dict;
126-
int watcher_id;
127-
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) {
128-
return NULL;
129-
}
130140
if (PyDict_Watch(watcher_id, dict)) {
131141
return NULL;
132142
}
133143
Py_RETURN_NONE;
134144
}
135145

146+
/*[clinic input]
147+
_testcapi.unwatch_dict = _testcapi.watch_dict
148+
[clinic start generated code]*/
149+
136150
static PyObject *
137-
unwatch_dict(PyObject *self, PyObject *args)
151+
_testcapi_unwatch_dict_impl(PyObject *module, int watcher_id, PyObject *dict)
152+
/*[clinic end generated code: output=512b1a71ae33c351 input=cae7dc1b6f7713b8]*/
138153
{
139-
PyObject *dict;
140-
int watcher_id;
141-
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &dict)) {
142-
return NULL;
143-
}
144154
if (PyDict_Unwatch(watcher_id, dict)) {
145155
return NULL;
146156
}
@@ -250,28 +260,31 @@ get_type_modified_events(PyObject *self, PyObject *Py_UNUSED(args))
250260
return Py_NewRef(g_type_modified_events);
251261
}
252262

263+
/*[clinic input]
264+
_testcapi.watch_type
265+
watcher_id: int
266+
type: object
267+
/
268+
[clinic start generated code]*/
269+
253270
static PyObject *
254-
watch_type(PyObject *self, PyObject *args)
271+
_testcapi_watch_type_impl(PyObject *module, int watcher_id, PyObject *type)
272+
/*[clinic end generated code: output=fdf4777126724fc4 input=5a808bf12be7e3ed]*/
255273
{
256-
PyObject *type;
257-
int watcher_id;
258-
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) {
259-
return NULL;
260-
}
261274
if (PyType_Watch(watcher_id, type)) {
262275
return NULL;
263276
}
264277
Py_RETURN_NONE;
265278
}
266279

280+
/*[clinic input]
281+
_testcapi.unwatch_type = _testcapi.watch_type
282+
[clinic start generated code]*/
283+
267284
static PyObject *
268-
unwatch_type(PyObject *self, PyObject *args)
285+
_testcapi_unwatch_type_impl(PyObject *module, int watcher_id, PyObject *type)
286+
/*[clinic end generated code: output=0389672d4ad5f68b input=6701911fb45edc9e]*/
269287
{
270-
PyObject *type;
271-
int watcher_id;
272-
if (!PyArg_ParseTuple(args, "iO", &watcher_id, &type)) {
273-
return NULL;
274-
}
275288
if (PyType_Unwatch(watcher_id, type)) {
276289
return NULL;
277290
}
@@ -605,29 +618,34 @@ allocate_too_many_func_watchers(PyObject *self, PyObject *args)
605618
Py_RETURN_NONE;
606619
}
607620

621+
/*[clinic input]
622+
_testcapi.set_func_defaults_via_capi
623+
func: object
624+
defaults: object
625+
/
626+
[clinic start generated code]*/
627+
608628
static PyObject *
609-
set_func_defaults(PyObject *self, PyObject *args)
629+
_testcapi_set_func_defaults_via_capi_impl(PyObject *module, PyObject *func,
630+
PyObject *defaults)
631+
/*[clinic end generated code: output=caf0cb39db31ac24 input=e04a8508ca9d42fc]*/
610632
{
611-
PyObject *func = NULL;
612-
PyObject *defaults = NULL;
613-
if (!PyArg_ParseTuple(args, "OO", &func, &defaults)) {
614-
return NULL;
615-
}
616633
if (PyFunction_SetDefaults(func, defaults) < 0) {
617634
return NULL;
618635
}
619636
Py_RETURN_NONE;
620637
}
621638

639+
/*[clinic input]
640+
_testcapi.set_func_kwdefaults_via_capi = _testcapi.set_func_defaults_via_capi
641+
[clinic start generated code]*/
642+
622643
static PyObject *
623-
set_func_kwdefaults(PyObject *self, PyObject *args)
644+
_testcapi_set_func_kwdefaults_via_capi_impl(PyObject *module, PyObject *func,
645+
PyObject *defaults)
646+
/*[clinic end generated code: output=9ed3b08177025070 input=f3cd1ca3c18de8ce]*/
624647
{
625-
PyObject *func = NULL;
626-
PyObject *kwdefaults = NULL;
627-
if (!PyArg_ParseTuple(args, "OO", &func, &kwdefaults)) {
628-
return NULL;
629-
}
630-
if (PyFunction_SetKwDefaults(func, kwdefaults) < 0) {
648+
if (PyFunction_SetKwDefaults(func, defaults) < 0) {
631649
return NULL;
632650
}
633651
Py_RETURN_NONE;
@@ -637,16 +655,16 @@ static PyMethodDef test_methods[] = {
637655
// Dict watchers.
638656
{"add_dict_watcher", add_dict_watcher, METH_O, NULL},
639657
{"clear_dict_watcher", clear_dict_watcher, METH_O, NULL},
640-
{"watch_dict", watch_dict, METH_VARARGS, NULL},
641-
{"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
658+
_TESTCAPI_WATCH_DICT_METHODDEF
659+
_TESTCAPI_UNWATCH_DICT_METHODDEF
642660
{"get_dict_watcher_events",
643661
(PyCFunction) get_dict_watcher_events, METH_NOARGS, NULL},
644662

645663
// Type watchers.
646664
{"add_type_watcher", add_type_watcher, METH_O, NULL},
647665
{"clear_type_watcher", clear_type_watcher, METH_O, NULL},
648-
{"watch_type", watch_type, METH_VARARGS, NULL},
649-
{"unwatch_type", unwatch_type, METH_VARARGS, NULL},
666+
_TESTCAPI_WATCH_TYPE_METHODDEF
667+
_TESTCAPI_UNWATCH_TYPE_METHODDEF
650668
{"get_type_modified_events",
651669
(PyCFunction) get_type_modified_events, METH_NOARGS, NULL},
652670

@@ -663,8 +681,8 @@ static PyMethodDef test_methods[] = {
663681
// Function watchers.
664682
{"add_func_watcher", add_func_watcher, METH_O, NULL},
665683
{"clear_func_watcher", clear_func_watcher, METH_O, NULL},
666-
{"set_func_defaults_via_capi", set_func_defaults, METH_VARARGS, NULL},
667-
{"set_func_kwdefaults_via_capi", set_func_kwdefaults, METH_VARARGS, NULL},
684+
_TESTCAPI_SET_FUNC_DEFAULTS_VIA_CAPI_METHODDEF
685+
_TESTCAPI_SET_FUNC_KWDEFAULTS_VIA_CAPI_METHODDEF
668686
{"allocate_too_many_func_watchers", allocate_too_many_func_watchers,
669687
METH_NOARGS, NULL},
670688
{NULL},

0 commit comments

Comments
 (0)