Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-112087: Make PyList_{Append,Size,GetSlice} to be thread-safe #114651

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 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-112087: Make PyList_{Append,GetItem,Size,GetSlice} to be thread-safe
  • Loading branch information
corona10 committed Jan 27, 2024
commit 393cbefe027440f41f9099da7c4915d0e7e8529f
4 changes: 4 additions & 0 deletions 4 Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,11 @@ static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
assert(ob->ob_base.ob_type != &PyLong_Type);
assert(ob->ob_base.ob_type != &PyBool_Type);
#ifdef Py_GIL_DISABLED
_Py_atomic_store_ssize_relaxed(&(_PyVarObject_CAST(ob)->ob_size), size);
corona10 marked this conversation as resolved.
Show resolved Hide resolved
#else
ob->ob_size = size;
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
Expand Down
37 changes: 29 additions & 8 deletions 37 Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ get_list_state(void)
}
#endif

#ifdef Py_GIL_DISABLED
#define _Py_SET_ITEMREF(op, i, v) \
_Py_atomic_store_ptr_relaxed(&((PyListObject *)(op))->ob_item[i], Py_NewRef(v))
#else
#define _Py_SET_ITEMREF(op, i, v) \
((PyListObject *)(op))->ob_item[i] = Py_NewRef(v)
#endif


/* Ensure ob_item has room for at least newsize elements, and set
* ob_size to newsize. If newsize > ob_size on entry, the content
Expand Down Expand Up @@ -221,8 +229,9 @@ PyList_Size(PyObject *op)
PyErr_BadInternalCall();
return -1;
}
else
return Py_SIZE(op);
else {
return PyList_GET_SIZE(op);
}
}

static inline int
Expand All @@ -245,12 +254,16 @@ PyList_GetItem(PyObject *op, Py_ssize_t i)
PyErr_BadInternalCall();
return NULL;
}
if (!valid_index(i, Py_SIZE(op))) {
PyObject *ret = NULL;
if (!valid_index(i, PyList_GET_SIZE(op))) {
corona10 marked this conversation as resolved.
Show resolved Hide resolved
_Py_DECLARE_STR(list_err, "list index out of range");
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
return NULL;
}
return ((PyListObject *)op) -> ob_item[i];
Py_BEGIN_CRITICAL_SECTION(op);
ret = ((PyListObject *)op) -> ob_item[i];
Py_END_CRITICAL_SECTION();
return ret;
}

int
Expand Down Expand Up @@ -341,11 +354,15 @@ _PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
int
PyList_Append(PyObject *op, PyObject *newitem)
{
int ret = -1;
if (PyList_Check(op) && (newitem != NULL)) {
return _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
Py_BEGIN_CRITICAL_SECTION(op);
ret = _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
Py_END_CRITICAL_SECTION();
return ret;
}
PyErr_BadInternalCall();
return -1;
return ret;
corona10 marked this conversation as resolved.
Show resolved Hide resolved
}

/* Methods */
Expand Down Expand Up @@ -498,7 +515,7 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
dest = np->ob_item;
for (i = 0; i < len; i++) {
PyObject *v = src[i];
dest[i] = Py_NewRef(v);
_Py_SET_ITEMREF(np, i, v);
corona10 marked this conversation as resolved.
Show resolved Hide resolved
}
Py_SET_SIZE(np, len);
return (PyObject *)np;
Expand All @@ -511,6 +528,8 @@ PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
PyErr_BadInternalCall();
return NULL;
}
PyObject *ret;
Py_BEGIN_CRITICAL_SECTION(a);
if (ilow < 0) {
ilow = 0;
}
Expand All @@ -523,7 +542,9 @@ PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
else if (ihigh > Py_SIZE(a)) {
ihigh = Py_SIZE(a);
}
return list_slice((PyListObject *)a, ilow, ihigh);
ret = list_slice((PyListObject *)a, ilow, ihigh);
Py_END_CRITICAL_SECTION();
return ret;
}

static PyObject *
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.