]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105927: Remove _PyWeakref_GetWeakrefCount() (#106007)
authorVictor Stinner <vstinner@python.org>
Fri, 23 Jun 2023 03:00:56 +0000 (05:00 +0200)
committerGitHub <noreply@github.com>
Fri, 23 Jun 2023 03:00:56 +0000 (03:00 +0000)
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.

Include/cpython/weakrefobject.h
Include/internal/pycore_weakref.h
Modules/_weakref.c
Modules/gcmodule.c

index fd79fdc2dcc46882715173df66645014528dd1ef..6bf165538142529a5a960955c257236980374968 100644 (file)
@@ -32,10 +32,6 @@ struct _PyWeakReference {
     vectorcallfunc vectorcall;
 };
 
-PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
-
-PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
-
 static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
     PyWeakReference *ref;
     PyObject *obj;
index f31b12463bb3e57d4b38c12bb4a3a9bda9e2d0ae..51b2bb6b11ede96422000aae34da045c636329e0 100644 (file)
@@ -46,6 +46,10 @@ static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
     return (Py_REFCNT(obj) == 0);
 }
 
+extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);
+
+extern void _PyWeakref_ClearRef(PyWeakReference *self);
+
 #ifdef __cplusplus
 }
 #endif
index 21ff0e2b7a984be8556b4fb656d3acae0fe9086e..b5d80cbd731a281a8ee3d3b454ed9bb238289a43 100644 (file)
@@ -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;
 }
index c51c100be8361defcfd11b6c34a5568eefa55b95..97644a7cee774f107c70385e8ba57efbd8f1eee7 100644 (file)
@@ -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;