mirror of
https://github.com/python/cpython.git
synced 2025-09-19 07:00:59 +00:00
gh-112087: Make list_repr and list_length to be thread-safe (gh-114582)
This commit is contained in:
parent
699779256e
commit
f9c505698a
2 changed files with 21 additions and 10 deletions
|
@ -29,7 +29,11 @@ typedef struct {
|
||||||
|
|
||||||
static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
|
static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
|
||||||
PyListObject *list = _PyList_CAST(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);
|
return Py_SIZE(list);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
|
#define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
|
||||||
|
|
||||||
|
|
|
@ -383,18 +383,11 @@ list_dealloc(PyObject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
list_repr(PyObject *self)
|
list_repr_impl(PyListObject *v)
|
||||||
{
|
{
|
||||||
PyListObject *v = (PyListObject *)self;
|
|
||||||
Py_ssize_t i;
|
|
||||||
PyObject *s;
|
PyObject *s;
|
||||||
_PyUnicodeWriter writer;
|
_PyUnicodeWriter writer;
|
||||||
|
Py_ssize_t i = Py_ReprEnter((PyObject*)v);
|
||||||
if (Py_SIZE(v) == 0) {
|
|
||||||
return PyUnicode_FromString("[]");
|
|
||||||
}
|
|
||||||
|
|
||||||
i = Py_ReprEnter((PyObject*)v);
|
|
||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
return i > 0 ? PyUnicode_FromString("[...]") : NULL;
|
return i > 0 ? PyUnicode_FromString("[...]") : NULL;
|
||||||
}
|
}
|
||||||
|
@ -439,10 +432,24 @@ error:
|
||||||
return NULL;
|
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
|
static Py_ssize_t
|
||||||
list_length(PyObject *a)
|
list_length(PyObject *a)
|
||||||
{
|
{
|
||||||
return Py_SIZE(a);
|
return PyList_GET_SIZE(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue