1
1
#include "Python.h"
2
- #include "pycore_dict .h" // _PyDict_DelItemIf ()
3
- #include "pycore_object .h" // _PyObject_GET_WEAKREFS_LISTPTR
4
- #include "pycore_weakref .h" // _PyWeakref_IS_DEAD ()
5
-
2
+ #include "pycore_critical_section .h" // Py_BEGIN_CRITICAL_SECTION ()
3
+ #include "pycore_dict .h" // _PyDict_DelItemIf()
4
+ #include "pycore_object .h" // _PyObject_GET_WEAKREFS_LISTPTR ()
5
+ #include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
6
6
7
7
#define GET_WEAKREFS_LISTPTR (o ) \
8
8
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
@@ -32,9 +32,12 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
32
32
33
33
if (!_PyType_SUPPORTS_WEAKREFS (Py_TYPE (object )))
34
34
return 0 ;
35
-
35
+ Py_ssize_t count ;
36
+ Py_BEGIN_CRITICAL_SECTION (object );
36
37
list = GET_WEAKREFS_LISTPTR (object );
37
- return _PyWeakref_GetWeakrefCount (* list );
38
+ count = _PyWeakref_GetWeakrefCount (* list );
39
+ Py_END_CRITICAL_SECTION ();
40
+ return count ;
38
41
}
39
42
40
43
@@ -45,7 +48,11 @@ is_dead_weakref(PyObject *value)
45
48
PyErr_SetString (PyExc_TypeError , "not a weakref" );
46
49
return -1 ;
47
50
}
48
- return _PyWeakref_IS_DEAD (value );
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 ;
49
56
}
50
57
51
58
/*[clinic input]
@@ -94,19 +101,23 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
94
101
return PyList_New (0 );
95
102
}
96
103
104
+ PyObject * result ;
105
+ Py_BEGIN_CRITICAL_SECTION (object );
97
106
PyWeakReference * * list = GET_WEAKREFS_LISTPTR (object );
98
107
Py_ssize_t count = _PyWeakref_GetWeakrefCount (* list );
99
108
100
- PyObject * result = PyList_New (count );
109
+ result = PyList_New (count );
101
110
if (result == NULL ) {
102
- return NULL ;
111
+ goto exit ;
103
112
}
104
113
105
114
PyWeakReference * current = * list ;
106
115
for (Py_ssize_t i = 0 ; i < count ; ++ i ) {
107
116
PyList_SET_ITEM (result , i , Py_NewRef (current ));
108
117
current = current -> wr_next ;
109
118
}
119
+ exit :
120
+ Py_END_CRITICAL_SECTION ();
110
121
return result ;
111
122
}
112
123
0 commit comments