Skip to content

Commit 2bcc0f7

Browse files
authored
gh-112213: Update _weakref module to use new AC feature (gh-112250)
1 parent b8c952a commit 2bcc0f7

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

Diff for: Modules/_weakref.c

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "Python.h"
2-
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
32
#include "pycore_dict.h" // _PyDict_DelItemIf()
43
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR()
54
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
@@ -15,7 +14,7 @@ module _weakref
1514
#include "clinic/_weakref.c.h"
1615

1716
/*[clinic input]
18-
17+
@critical_section object
1918
_weakref.getweakrefcount -> Py_ssize_t
2019
2120
object: object
@@ -26,17 +25,13 @@ Return the number of weak references to 'object'.
2625

2726
static Py_ssize_t
2827
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
29-
/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
28+
/*[clinic end generated code: output=301806d59558ff3e input=6535a580f1d0ebdc]*/
3029
{
31-
PyWeakReference **list;
32-
33-
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
30+
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
3431
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();
32+
}
33+
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
34+
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
4035
return count;
4136
}
4237

@@ -48,11 +43,7 @@ is_dead_weakref(PyObject *value)
4843
PyErr_SetString(PyExc_TypeError, "not a weakref");
4944
return -1;
5045
}
51-
int is_dead;
52-
Py_BEGIN_CRITICAL_SECTION(value);
53-
is_dead = _PyWeakref_IS_DEAD(value);
54-
Py_END_CRITICAL_SECTION();
55-
return is_dead;
46+
return _PyWeakref_IS_DEAD(value);
5647
}
5748

5849
/*[clinic input]
@@ -86,6 +77,7 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
8677

8778

8879
/*[clinic input]
80+
@critical_section object
8981
_weakref.getweakrefs
9082
object: object
9183
/
@@ -94,30 +86,26 @@ Return a list of all weak reference objects pointing to 'object'.
9486
[clinic start generated code]*/
9587

9688
static PyObject *
97-
_weakref_getweakrefs(PyObject *module, PyObject *object)
98-
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
89+
_weakref_getweakrefs_impl(PyObject *module, PyObject *object)
90+
/*[clinic end generated code: output=5ec268989fb8f035 input=3dea95b8f5b31bbb]*/
9991
{
10092
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
10193
return PyList_New(0);
10294
}
10395

104-
PyObject *result;
105-
Py_BEGIN_CRITICAL_SECTION(object);
10696
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
10797
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
10898

109-
result = PyList_New(count);
99+
PyObject *result = PyList_New(count);
110100
if (result == NULL) {
111-
goto exit;
101+
return NULL;
112102
}
113103

114104
PyWeakReference *current = *list;
115105
for (Py_ssize_t i = 0; i < count; ++i) {
116106
PyList_SET_ITEM(result, i, Py_NewRef(current));
117107
current = current->wr_next;
118108
}
119-
exit:
120-
Py_END_CRITICAL_SECTION();
121109
return result;
122110
}
123111

Diff for: Modules/clinic/_weakref.c.h

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)