Skip to content

gh-105927: Remove _PyWeakref_GetWeakrefCount() #106007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Include/cpython/weakrefobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_weakref.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 15 additions & 18 deletions Modules/_weakref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down