Skip to content

Commit 6a80664

Browse files
authored
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.
1 parent 7b3ed5b commit 6a80664

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

Include/cpython/weakrefobject.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ struct _PyWeakReference {
3232
vectorcallfunc vectorcall;
3333
};
3434

35-
PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
36-
37-
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
38-
3935
static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj) {
4036
PyWeakReference *ref;
4137
PyObject *obj;

Include/internal/pycore_weakref.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
4646
return (Py_REFCNT(obj) == 0);
4747
}
4848

49+
extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);
50+
51+
extern void _PyWeakref_ClearRef(PyWeakReference *self);
52+
4953
#ifdef __cplusplus
5054
}
5155
#endif

Modules/_weakref.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,22 @@ static PyObject *
8989
_weakref_getweakrefs(PyObject *module, PyObject *object)
9090
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
9191
{
92-
PyObject *result = NULL;
93-
94-
if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
95-
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
96-
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
97-
98-
result = PyList_New(count);
99-
if (result != NULL) {
100-
PyWeakReference *current = *list;
101-
Py_ssize_t i;
102-
for (i = 0; i < count; ++i) {
103-
PyList_SET_ITEM(result, i, (PyObject *) current);
104-
Py_INCREF(current);
105-
current = current->wr_next;
106-
}
107-
}
92+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
93+
return PyList_New(0);
10894
}
109-
else {
110-
result = PyList_New(0);
95+
96+
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
97+
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
98+
99+
PyObject *result = PyList_New(count);
100+
if (result == NULL) {
101+
return NULL;
102+
}
103+
104+
PyWeakReference *current = *list;
105+
for (Py_ssize_t i = 0; i < count; ++i) {
106+
PyList_SET_ITEM(result, i, Py_NewRef(current));
107+
current = current->wr_next;
111108
}
112109
return result;
113110
}

Modules/gcmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "pycore_object.h"
3131
#include "pycore_pyerrors.h"
3232
#include "pycore_pystate.h" // _PyThreadState_GET()
33+
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
3334
#include "pydtrace.h"
3435

3536
typedef struct _gc_runtime_state GCState;

0 commit comments

Comments
 (0)