Skip to content

gh-112087: Make list_repr and list_length to be thread-safe #114582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Include/cpython/listobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
27 changes: 17 additions & 10 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -439,10 +432,24 @@ list_repr(PyObject *self)
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
Expand Down