diff options
author | Victor Stinner <vstinner@python.org> | 2023-06-23 03:00:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-23 03:00:56 (GMT) |
commit | 6a80664ef1200008d5beb1584e03d779ef30cc58 (patch) | |
tree | e5ad14c21ed2aa9b040de42ca7fac6b6caa62b46 /Modules | |
parent | 7b3ed5b29fd33ecfdaedc3bb0b8f6c05ded68361 (diff) | |
download | cpython-6a80664ef1200008d5beb1584e03d779ef30cc58.zip cpython-6a80664ef1200008d5beb1584e03d779ef30cc58.tar.gz cpython-6a80664ef1200008d5beb1584e03d779ef30cc58.tar.bz2 |
gh-105927: Remove _PyWeakref_GetWeakrefCount() (#106007)
Remove _PyWeakref_GetWeakrefCount() and _PyWeakref_ClearRef() from
the public C API: move them to the internal C API.
Refactor also _weakref_getweakrefs() code to make it more readable.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_weakref.c | 33 | ||||
-rw-r--r-- | Modules/gcmodule.c | 1 |
2 files changed, 16 insertions, 18 deletions
diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 21ff0e2..b5d80cb 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -89,25 +89,22 @@ static PyObject * _weakref_getweakrefs(PyObject *module, PyObject *object) /*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/ { - PyObject *result = NULL; - - if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { - PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); - Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); - - result = PyList_New(count); - if (result != NULL) { - PyWeakReference *current = *list; - Py_ssize_t i; - for (i = 0; i < count; ++i) { - PyList_SET_ITEM(result, i, (PyObject *) current); - Py_INCREF(current); - current = current->wr_next; - } - } + if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { + return PyList_New(0); } - else { - result = PyList_New(0); + + PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); + Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); + + PyObject *result = PyList_New(count); + if (result == NULL) { + return NULL; + } + + PyWeakReference *current = *list; + for (Py_ssize_t i = 0; i < count; ++i) { + PyList_SET_ITEM(result, i, Py_NewRef(current)); + current = current->wr_next; } return result; } diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index c51c100..97644a7 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -30,6 +30,7 @@ #include "pycore_object.h" #include "pycore_pyerrors.h" #include "pycore_pystate.h" // _PyThreadState_GET() +#include "pycore_weakref.h" // _PyWeakref_ClearRef() #include "pydtrace.h" typedef struct _gc_runtime_state GCState; |