]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112213: Update _weakref module to use new AC feature (gh-112250)
authorDonghee Na <donghee.na@python.org>
Sun, 19 Nov 2023 01:43:51 +0000 (01:43 +0000)
committerGitHub <noreply@github.com>
Sun, 19 Nov 2023 01:43:51 +0000 (10:43 +0900)
Modules/_weakref.c
Modules/clinic/_weakref.c.h

index 90c7afcc248ff35efaea87e8ebdcf8a65dec4f8d..7225dbc9ce4a1b6cc69a380862c3bc2f092807b9 100644 (file)
@@ -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;
 }
 
index 8d7bc5dc936610253dde928e944f04c0028a8231..550b6c4d71a015f745271d1c58a75ac84c9a1202 100644 (file)
@@ -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]*/