From 5c06dba21b9767127f042b8a168703f06338c3f4 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 7 Mar 2022 15:27:45 +0530 Subject: [PATCH] bpo-46937: convert remaining functions to AC in _weakref (GH-31705) --- Modules/_weakref.c | 47 ++++++++++++++++++++---------------- Modules/clinic/_weakref.c.h | 48 ++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index edc09b949fd..157a852ae9a 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -76,12 +76,17 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct, } -PyDoc_STRVAR(weakref_getweakrefs__doc__, -"getweakrefs(object) -- return a list of all weak reference objects\n" -"that point to 'object'."); +/*[clinic input] +_weakref.getweakrefs + object: object + / + +Return a list of all weak reference objects pointing to 'object'. +[clinic start generated code]*/ static PyObject * -weakref_getweakrefs(PyObject *self, PyObject *object) +_weakref_getweakrefs(PyObject *module, PyObject *object) +/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/ { PyObject *result = NULL; @@ -107,22 +112,24 @@ weakref_getweakrefs(PyObject *self, PyObject *object) } -PyDoc_STRVAR(weakref_proxy__doc__, -"proxy(object[, callback]) -- create a proxy object that weakly\n" -"references 'object'. 'callback', if given, is called with a\n" -"reference to the proxy when 'object' is about to be finalized."); +/*[clinic input] + +_weakref.proxy + object: object + callback: object(c_default="NULL") = None + / + +Create a proxy object that weakly references 'object'. + +'callback', if given, is called with a reference to the +proxy when 'object' is about to be finalized. +[clinic start generated code]*/ static PyObject * -weakref_proxy(PyObject *self, PyObject *args) +_weakref_proxy_impl(PyObject *module, PyObject *object, PyObject *callback) +/*[clinic end generated code: output=d68fa4ad9ea40519 input=4808adf22fd137e7]*/ { - PyObject *object; - PyObject *callback = NULL; - PyObject *result = NULL; - - if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { - result = PyWeakref_NewProxy(object, callback); - } - return result; + return PyWeakref_NewProxy(object, callback); } @@ -130,10 +137,8 @@ static PyMethodDef weakref_functions[] = { _WEAKREF_GETWEAKREFCOUNT_METHODDEF _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF - {"getweakrefs", weakref_getweakrefs, METH_O, - weakref_getweakrefs__doc__}, - {"proxy", weakref_proxy, METH_VARARGS, - weakref_proxy__doc__}, + _WEAKREF_GETWEAKREFS_METHODDEF + _WEAKREF_PROXY_METHODDEF {NULL, NULL, 0, NULL} }; diff --git a/Modules/clinic/_weakref.c.h b/Modules/clinic/_weakref.c.h index c3a908fa6a1..02f0168937c 100644 --- a/Modules/clinic/_weakref.c.h +++ b/Modules/clinic/_weakref.c.h @@ -64,4 +64,50 @@ _weakref__remove_dead_weakref(PyObject *module, PyObject *const *args, Py_ssize_ exit: return return_value; } -/*[clinic end generated code: output=c543dc2cd6ece975 input=a9049054013a1b77]*/ + +PyDoc_STRVAR(_weakref_getweakrefs__doc__, +"getweakrefs($module, object, /)\n" +"--\n" +"\n" +"Return a list of all weak reference objects pointing to \'object\'."); + +#define _WEAKREF_GETWEAKREFS_METHODDEF \ + {"getweakrefs", (PyCFunction)_weakref_getweakrefs, METH_O, _weakref_getweakrefs__doc__}, + +PyDoc_STRVAR(_weakref_proxy__doc__, +"proxy($module, object, callback=None, /)\n" +"--\n" +"\n" +"Create a proxy object that weakly references \'object\'.\n" +"\n" +"\'callback\', if given, is called with a reference to the\n" +"proxy when \'object\' is about to be finalized."); + +#define _WEAKREF_PROXY_METHODDEF \ + {"proxy", (PyCFunction)(void(*)(void))_weakref_proxy, METH_FASTCALL, _weakref_proxy__doc__}, + +static PyObject * +_weakref_proxy_impl(PyObject *module, PyObject *object, PyObject *callback); + +static PyObject * +_weakref_proxy(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *object; + PyObject *callback = NULL; + + if (!_PyArg_CheckPositional("proxy", nargs, 1, 2)) { + goto exit; + } + object = args[0]; + if (nargs < 2) { + goto skip_optional; + } + callback = args[1]; +skip_optional: + return_value = _weakref_proxy_impl(module, object, callback); + +exit: + return return_value; +} +/*[clinic end generated code: output=5a10a1fa43722399 input=a9049054013a1b77]*/