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_dict.h" // _PyDict_DelItemIf()
3
+ #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
4
+ #include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
5
+ #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION
6
6
7
7
#define GET_WEAKREFS_LISTPTR (o ) \
8
8
((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
@@ -32,9 +32,11 @@ _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_BEGIN_CRITICAL_SECTION ( object );
36
36
list = GET_WEAKREFS_LISTPTR (object );
37
- return _PyWeakref_GetWeakrefCount (* list );
37
+ Py_ssize_t count = _PyWeakref_GetWeakrefCount (* list );
38
+ Py_END_CRITICAL_SECTION ();
39
+ return count ;
38
40
}
39
41
40
42
@@ -94,6 +96,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
94
96
return PyList_New (0 );
95
97
}
96
98
99
+ Py_BEGIN_CRITICAL_SECTION (object );
97
100
PyWeakReference * * list = GET_WEAKREFS_LISTPTR (object );
98
101
Py_ssize_t count = _PyWeakref_GetWeakrefCount (* list );
99
102
@@ -107,6 +110,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
107
110
PyList_SET_ITEM (result , i , Py_NewRef (current ));
108
111
current = current -> wr_next ;
109
112
}
113
+ Py_END_CRITICAL_SECTION ();
110
114
return result ;
111
115
}
112
116
0 commit comments