]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112087: Make list_repr and list_length to be thread-safe (gh-114582)
authorDonghee Na <donghee.na@python.org>
Fri, 26 Jan 2024 16:20:21 +0000 (01:20 +0900)
committerGitHub <noreply@github.com>
Fri, 26 Jan 2024 16:20:21 +0000 (01:20 +0900)
Include/cpython/listobject.h
Objects/listobject.c

index 8ade1b164681f9e4a648b576435a04025a1fc225..49f5e8d6d1a0d6c2863925daaac16c07b6066948 100644 (file)
@@ -29,7 +29,11 @@ typedef struct {
 
 static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
     PyListObject *list = _PyList_CAST(op);
+#ifdef Py_GIL_DISABLED
+    return _Py_atomic_load_ssize_relaxed(&(_PyVarObject_CAST(list)->ob_size));
+#else
     return Py_SIZE(list);
+#endif
 }
 #define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
 
index 1e885f9cb80c4ce1e4cb7299c2f30fdadf76b78f..56785e5f37a450f8fecd7618fb41913ccf881013 100644 (file)
@@ -383,18 +383,11 @@ list_dealloc(PyObject *self)
 }
 
 static PyObject *
-list_repr(PyObject *self)
+list_repr_impl(PyListObject *v)
 {
-    PyListObject *v = (PyListObject *)self;
-    Py_ssize_t i;
     PyObject *s;
     _PyUnicodeWriter writer;
-
-    if (Py_SIZE(v) == 0) {
-        return PyUnicode_FromString("[]");
-    }
-
-    i = Py_ReprEnter((PyObject*)v);
+    Py_ssize_t i = Py_ReprEnter((PyObject*)v);
     if (i != 0) {
         return i > 0 ? PyUnicode_FromString("[...]") : NULL;
     }
@@ -439,10 +432,24 @@ error:
     return NULL;
 }
 
+static PyObject *
+list_repr(PyObject *self)
+{
+    if (PyList_GET_SIZE(self) == 0) {
+        return PyUnicode_FromString("[]");
+    }
+    PyListObject *v = (PyListObject *)self;
+    PyObject *ret = NULL;
+    Py_BEGIN_CRITICAL_SECTION(v);
+    ret = list_repr_impl(v);
+    Py_END_CRITICAL_SECTION();
+    return ret;
+}
+
 static Py_ssize_t
 list_length(PyObject *a)
 {
-    return Py_SIZE(a);
+    return PyList_GET_SIZE(a);
 }
 
 static int