diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 90c7afcc248..7225dbc9ce4 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -1,5 +1,4 @@ #include "Python.h" -#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() #include "pycore_dict.h" // _PyDict_DelItemIf() #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR() #include "pycore_weakref.h" // _PyWeakref_IS_DEAD() @@ -15,7 +14,7 @@ module _weakref #include "clinic/_weakref.c.h" /*[clinic input] - +@critical_section object _weakref.getweakrefcount -> Py_ssize_t object: object @@ -26,17 +25,13 @@ Return the number of weak references to 'object'. static Py_ssize_t _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) -/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/ +/*[clinic end generated code: output=301806d59558ff3e input=6535a580f1d0ebdc]*/ { - PyWeakReference **list; - - if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) + if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { return 0; - Py_ssize_t count; - Py_BEGIN_CRITICAL_SECTION(object); - list = GET_WEAKREFS_LISTPTR(object); - count = _PyWeakref_GetWeakrefCount(*list); - Py_END_CRITICAL_SECTION(); + } + PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); + Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); return count; } @@ -48,11 +43,7 @@ is_dead_weakref(PyObject *value) PyErr_SetString(PyExc_TypeError, "not a weakref"); return -1; } - int is_dead; - Py_BEGIN_CRITICAL_SECTION(value); - is_dead = _PyWeakref_IS_DEAD(value); - Py_END_CRITICAL_SECTION(); - return is_dead; + return _PyWeakref_IS_DEAD(value); } /*[clinic input] @@ -86,6 +77,7 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct, /*[clinic input] +@critical_section object _weakref.getweakrefs object: object / @@ -94,21 +86,19 @@ Return a list of all weak reference objects pointing to 'object'. [clinic start generated code]*/ static PyObject * -_weakref_getweakrefs(PyObject *module, PyObject *object) -/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/ +_weakref_getweakrefs_impl(PyObject *module, PyObject *object) +/*[clinic end generated code: output=5ec268989fb8f035 input=3dea95b8f5b31bbb]*/ { if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) { return PyList_New(0); } - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(object); PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); - result = PyList_New(count); + PyObject *result = PyList_New(count); if (result == NULL) { - goto exit; + return NULL; } PyWeakReference *current = *list; @@ -116,8 +106,6 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) PyList_SET_ITEM(result, i, Py_NewRef(current)); current = current->wr_next; } -exit: - Py_END_CRITICAL_SECTION(); return result; } diff --git a/Modules/clinic/_weakref.c.h b/Modules/clinic/_weakref.c.h index 8d7bc5dc936..550b6c4d71a 100644 --- a/Modules/clinic/_weakref.c.h +++ b/Modules/clinic/_weakref.c.h @@ -2,6 +2,7 @@ preserve [clinic start generated code]*/ +#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() PyDoc_STRVAR(_weakref_getweakrefcount__doc__, @@ -22,7 +23,9 @@ _weakref_getweakrefcount(PyObject *module, PyObject *object) PyObject *return_value = NULL; Py_ssize_t _return_value; + Py_BEGIN_CRITICAL_SECTION(object); _return_value = _weakref_getweakrefcount_impl(module, object); + Py_END_CRITICAL_SECTION(); if ((_return_value == -1) && PyErr_Occurred()) { goto exit; } @@ -76,6 +79,21 @@ PyDoc_STRVAR(_weakref_getweakrefs__doc__, #define _WEAKREF_GETWEAKREFS_METHODDEF \ {"getweakrefs", (PyCFunction)_weakref_getweakrefs, METH_O, _weakref_getweakrefs__doc__}, +static PyObject * +_weakref_getweakrefs_impl(PyObject *module, PyObject *object); + +static PyObject * +_weakref_getweakrefs(PyObject *module, PyObject *object) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(object); + return_value = _weakref_getweakrefs_impl(module, object); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_weakref_proxy__doc__, "proxy($module, object, callback=None, /)\n" "--\n" @@ -112,4 +130,4 @@ skip_optional: exit: return return_value; } -/*[clinic end generated code: output=60f59adc1dc9eab8 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d5d30707212a9870 input=a9049054013a1b77]*/