Skip to content

Commit 8e91b7b

Browse files
committed
pythongh-112213: Update _weakref module to use new AC feature
1 parent e52cc80 commit 8e91b7b

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

Modules/_weakref.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module _weakref
1515
#include "clinic/_weakref.c.h"
1616

1717
/*[clinic input]
18-
18+
@critical_section object
1919
_weakref.getweakrefcount -> Py_ssize_t
2020
2121
object: object
@@ -26,17 +26,13 @@ Return the number of weak references to 'object'.
2626

2727
static Py_ssize_t
2828
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
29-
/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
29+
/*[clinic end generated code: output=301806d59558ff3e input=6535a580f1d0ebdc]*/
3030
{
31-
PyWeakReference **list;
32-
33-
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
31+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
3432
return 0;
35-
Py_ssize_t count;
36-
Py_BEGIN_CRITICAL_SECTION(object);
37-
list = GET_WEAKREFS_LISTPTR(object);
38-
count = _PyWeakref_GetWeakrefCount(*list);
39-
Py_END_CRITICAL_SECTION();
33+
}
34+
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
35+
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
4036
return count;
4137
}
4238

@@ -86,6 +82,7 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
8682

8783

8884
/*[clinic input]
85+
@critical_section object
8986
_weakref.getweakrefs
9087
object: object
9188
/
@@ -94,30 +91,26 @@ Return a list of all weak reference objects pointing to 'object'.
9491
[clinic start generated code]*/
9592

9693
static PyObject *
97-
_weakref_getweakrefs(PyObject *module, PyObject *object)
98-
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
94+
_weakref_getweakrefs_impl(PyObject *module, PyObject *object)
95+
/*[clinic end generated code: output=5ec268989fb8f035 input=3dea95b8f5b31bbb]*/
9996
{
10097
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
10198
return PyList_New(0);
10299
}
103100

104-
PyObject *result;
105-
Py_BEGIN_CRITICAL_SECTION(object);
106101
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
107102
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
108103

109-
result = PyList_New(count);
104+
PyObject *result = PyList_New(count);
110105
if (result == NULL) {
111-
goto exit;
106+
return NULL;
112107
}
113108

114109
PyWeakReference *current = *list;
115110
for (Py_ssize_t i = 0; i < count; ++i) {
116111
PyList_SET_ITEM(result, i, Py_NewRef(current));
117112
current = current->wr_next;
118113
}
119-
exit:
120-
Py_END_CRITICAL_SECTION();
121114
return result;
122115
}
123116

Modules/clinic/_weakref.c.h

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)