diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 4e2862e7467c3d3..f751b69c1e4a1aa 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -32,9 +32,11 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) return 0; - + Py_BEGIN_CRITICAL_SECTION(object); list = GET_WEAKREFS_LISTPTR(object); - return _PyWeakref_GetWeakrefCount(*list); + Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); + Py_END_CRITICAL_SECTION(); + return count; } @@ -94,6 +96,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) return PyList_New(0); } + Py_BEGIN_CRITICAL_SECTION(object); PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); @@ -107,6 +110,8 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) PyList_SET_ITEM(result, i, Py_NewRef(current)); current = current->wr_next; } + + Py_END_CRITICAL_SECTION(); return result; }