From 9b8c0c7baf48845ac77c10676e9814c02b8bac0a Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 26 Jan 2024 17:11:16 +0900 Subject: [PATCH 1/3] gh-112087: Make list_repr and list_length to be thread-safe --- Objects/listobject.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 401d1026133f4e..00fb0bd5cbaa96 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -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,28 @@ list_repr(PyObject *self) return NULL; } +static PyObject * +list_repr(PyObject *self) +{ + PyListObject *v = (PyListObject *)self; + PyObject *ret = NULL; + if (Py_SIZE(v) == 0) { + return PyUnicode_FromString("[]"); + } + Py_BEGIN_CRITICAL_SECTION(v); + ret = list_repr_impl(v); + Py_END_CRITICAL_SECTION(); + return ret; +} + static Py_ssize_t list_length(PyObject *a) { +#ifdef Py_GIL_DISABLED + return _Py_atomic_load_ssize_relaxed(&(_PyVarObject_CAST(a)->ob_size)); +#else return Py_SIZE(a); +#endif } static int From 7009ceca1376e26c9cd664e06e5c475041ff381c Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 27 Jan 2024 00:20:11 +0900 Subject: [PATCH 2/3] Address code review --- Include/cpython/listobject.h | 4 ++++ Objects/listobject.c | 8 ++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Include/cpython/listobject.h b/Include/cpython/listobject.h index 8ade1b164681f9..49f5e8d6d1a0d6 100644 --- a/Include/cpython/listobject.h +++ b/Include/cpython/listobject.h @@ -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)) diff --git a/Objects/listobject.c b/Objects/listobject.c index 00fb0bd5cbaa96..86ca20a99b399e 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -437,7 +437,7 @@ list_repr(PyObject *self) { PyListObject *v = (PyListObject *)self; PyObject *ret = NULL; - if (Py_SIZE(v) == 0) { + if (PyList_GET_SIZE(v) == 0) { return PyUnicode_FromString("[]"); } Py_BEGIN_CRITICAL_SECTION(v); @@ -449,11 +449,7 @@ list_repr(PyObject *self) static Py_ssize_t list_length(PyObject *a) { -#ifdef Py_GIL_DISABLED - return _Py_atomic_load_ssize_relaxed(&(_PyVarObject_CAST(a)->ob_size)); -#else - return Py_SIZE(a); -#endif + return PyList_GET_SIZE(a); } static int From 4d0fe6a6d1bf3eff490e155a786d812dc5131ab2 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 27 Jan 2024 00:24:21 +0900 Subject: [PATCH 3/3] nit --- Objects/listobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 86ca20a99b399e..b8905ee1d179bb 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -435,11 +435,11 @@ list_repr_impl(PyListObject *v) static PyObject * list_repr(PyObject *self) { - PyListObject *v = (PyListObject *)self; - PyObject *ret = NULL; - if (PyList_GET_SIZE(v) == 0) { + 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();