-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
393cbef
gh-112087: Make PyList_{Append,GetItem,Size,GetSlice} to be thread-safe
corona10 df1661d
Fix compiler warn
corona10 6f37b61
nit
corona10 f67f936
fix
corona10 ba74dc5
Improve _PyList_AppendTakeRef
corona10 9075e61
nit
corona10 553db97
Address code review
corona10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,12 @@ 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 | ||
|
@@ -221,8 +227,9 @@ PyList_Size(PyObject *op) | |
PyErr_BadInternalCall(); | ||
return -1; | ||
} | ||
else | ||
return Py_SIZE(op); | ||
else { | ||
return PyList_GET_SIZE(op); | ||
} | ||
} | ||
|
||
static inline int | ||
|
@@ -245,7 +252,8 @@ 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; | ||
|
@@ -328,7 +336,7 @@ PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem) | |
int | ||
_PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem) | ||
{ | ||
Py_ssize_t len = PyList_GET_SIZE(self); | ||
Py_ssize_t len = Py_SIZE(self); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is only called by _PyList_AppendTakeRef, so it's safe. |
||
assert(self->allocated == -1 || self->allocated == len); | ||
if (list_resize(self, len + 1) < 0) { | ||
Py_DECREF(newitem); | ||
|
@@ -341,11 +349,15 @@ _PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem) | |
int | ||
PyList_Append(PyObject *op, PyObject *newitem) | ||
{< 10000 /span> | ||
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 */ | ||
|
@@ -473,7 +485,7 @@ static PyObject * | |
list_item(PyObject *aa, Py_ssize_t i) | ||
{ | ||
PyListObject *a = (PyListObject *)aa; | ||
if (!valid_index(i, Py_SIZE(a))) { | ||
if (!valid_index(i, PyList_GET_SIZE(a))) { | ||
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err)); | ||
return NULL; | ||
} | ||
|
@@ -484,7 +496,7 @@ static PyObject * | |
list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | ||
{ | ||
PyListObject *np; | ||
PyObject **src, **dest; | ||
PyObject **src; | ||
Py_ssize_t i, len; | ||
len = ihigh - ilow; | ||
if (len <= 0) { | ||
|
@@ -495,10 +507,9 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | |
return NULL; | ||
|
||
src = a->ob_item + ilow; | ||
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; | ||
|
@@ -511,6 +522,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; | ||
} | ||
|
@@ -523,7 +536,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 * | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.