1
1
#include "Python.h"
2
- #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
3
2
#include "pycore_dict.h" // _PyDict_DelItemIf()
4
3
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR()
5
4
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
@@ -15,7 +14,7 @@ module _weakref
15
14
#include "clinic/_weakref.c.h"
16
15
17
16
/*[clinic input]
18
-
17
+ @critical_section object
19
18
_weakref.getweakrefcount -> Py_ssize_t
20
19
21
20
object: object
@@ -26,17 +25,13 @@ Return the number of weak references to 'object'.
26
25
27
26
static Py_ssize_t
28
27
_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 ]*/
30
29
{
31
- PyWeakReference * * list ;
32
-
33
- if (!_PyType_SUPPORTS_WEAKREFS (Py_TYPE (object )))
30
+ if (!_PyType_SUPPORTS_WEAKREFS (Py_TYPE (object ))) {
34
31
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 );
40
35
return count ;
41
36
}
42
37
@@ -48,11 +43,7 @@ is_dead_weakref(PyObject *value)
48
43
PyErr_SetString (PyExc_TypeError , "not a weakref" );
49
44
return -1 ;
50
45
}
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 );
56
47
}
57
48
58
49
/*[clinic input]
@@ -86,6 +77,7 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
86
77
87
78
88
79
/*[clinic input]
80
+ @critical_section object
89
81
_weakref.getweakrefs
90
82
object: object
91
83
/
@@ -94,30 +86,26 @@ Return a list of all weak reference objects pointing to 'object'.
94
86
[clinic start generated code]*/
95
87
96
88
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 ]*/
99
91
{
100
92
if (!_PyType_SUPPORTS_WEAKREFS (Py_TYPE (object ))) {
101
93
return PyList_New (0 );
102
94
}
103
95
104
- PyObject * result ;
105
- Py_BEGIN_CRITICAL_SECTION (object );
106
96
PyWeakReference * * list = GET_WEAKREFS_LISTPTR (object );
107
97
Py_ssize_t count = _PyWeakref_GetWeakrefCount (* list );
108
98
109
- result = PyList_New (count );
99
+ PyObject * result = PyList_New (count );
110
100
if (result == NULL ) {
111
- goto exit ;
101
+ return NULL ;
112
102
}
113
103
114
104
PyWeakReference * current = * list ;
115
105
for (Py_ssize_t i = 0 ; i < count ; ++ i ) {
116
106
PyList_SET_ITEM (result , i , Py_NewRef (current ));
117
107
current = current -> wr_next ;
118
108
}
119
- exit :
120
- Py_END_CRITICAL_SECTION ();
121
109
return result ;
122
110
}
123
111
0 commit comments