Skip to content
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

gh-112213: Update _weakref module to use new AC feature #112250

Merged
merged 4 commits into from
Nov 19, 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
36 changes: 12 additions & 24 deletions Modules/_weakref.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Python.h"
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
#include "pycore_dict.h" // _PyDict_DelItemIf()
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR()
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
Expand All @@ -15,7 +14,7 @@ module _weakref
#include "clinic/_weakref.c.h"

/*[clinic input]

@critical_section object
_weakref.getweakrefcount -> Py_ssize_t

object: object
Expand All @@ -26,17 +25,13 @@ Return the number of weak references to 'object'.

static Py_ssize_t
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
/*[clinic end generated code: output=301806d59558ff3e input=6535a580f1d0ebdc]*/
{
PyWeakReference **list;

if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
return 0;
Py_ssize_t count;
Py_BEGIN_CRITICAL_SECTION(object);
list = GET_WEAKREFS_LISTPTR(object);
count = _PyWeakref_GetWeakrefCount(*list);
Py_END_CRITICAL_SECTION();
}
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
return count;
}

Expand All @@ -48,11 +43,7 @@ is_dead_weakref(PyObject *value)
PyErr_SetString(PyExc_TypeError, "not a weakref");
return -1;
}
int is_dead;
Py_BEGIN_CRITICAL_SECTION(value);
is_dead = _PyWeakref_IS_DEAD(value);
Py_END_CRITICAL_SECTION();
return is_dead;
return _PyWeakref_IS_DEAD(value);
}

/*[clinic input]
Expand Down Expand Up @@ -86,6 +77,7 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,


/*[clinic input]
@critical_section object
_weakref.getweakrefs
object: object
/
Expand All @@ -94,30 +86,26 @@ Return a list of all weak reference objects pointing to 'object'.
[clinic start generated code]*/

static PyObject *
_weakref_getweakrefs(PyObject *module, PyObject *object)
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
_weakref_getweakrefs_impl(PyObject *module, PyObject *object)
/*[clinic end generated code: output=5ec268989fb8f035 input=3dea95b8f5b31bbb]*/
{
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
return PyList_New(0);
}

PyObject *result;
Py_BEGIN_CRITICAL_SECTION(object);
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);

result = PyList_New(count);
PyObject *result = PyList_New(count);
if (result == NULL) {
goto exit;
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;
}
exit:
Py_END_CRITICAL_SECTION();
return result;
}

Expand Down
20 changes: 19 additions & 1 deletion Modules/clinic/_weakref.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.