Skip to content
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-129107: two cases where second operand needs lock
  • Loading branch information
tom-pytel committed Feb 17, 2025
commit 7e7bef56044874d9c1224915a7b1eaf74bd32db0
12 changes: 12 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,12 +2380,22 @@ def ass_subscript(b, a): # MODIFIES!
b.wait()
a[:] = c

def ass_subscript2(b, a, c): # MODIFIES!
b.wait()
a[:] = c
assert b'\xdd' not in a

def mod(b, a):
c = tuple(range(4096))
b.wait()
try: a % c
except TypeError: pass

def mod2(b, a, c):
b.wait()
d = a % c
assert b'\xdd' not in d

def repr_(b, a):
b.wait()
repr(a)
Expand Down Expand Up @@ -2506,7 +2516,9 @@ def check(funcs, a=None, *args):

check([clear] + [contains] * 10)
check([clear] + [subscript] * 10)
check([clear2] + [ass_subscript2] * 10, None, bytearray(b'0' * 0x400000))
check([clear] + [mod] * 10, bytearray(b'%d' * 4096))
check([clear2] + [mod2] * 10, bytearray(b'%s'), bytearray(b'0' * 0x400000))

check([clear] + [capitalize] * 10, bytearray(b'a' * 0x40000))
check([clear] + [center] * 10, bytearray(b'a' * 0x40000))
Expand Down
23 changes: 18 additions & 5 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@ static int
bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *values)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
#ifdef Py_DEBUG
if (values != NULL) {
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(values);
}
#endif
Comment thread
tom-pytel marked this conversation as resolved.
Outdated
PyByteArrayObject *self = _PyByteArray_CAST(op);
Py_ssize_t start, stop, step, slicelen;
char *buf = PyByteArray_AS_STRING(self);
Expand Down Expand Up @@ -864,9 +869,16 @@ static int
bytearray_ass_subscript(PyObject *op, PyObject *index, PyObject *values)
{
int ret;
Py_BEGIN_CRITICAL_SECTION(op);
ret = bytearray_ass_subscript_lock_held(op, index, values);
Py_END_CRITICAL_SECTION();
if (values != NULL) {
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
Py_BEGIN_CRITICAL_SECTION2(op, values);
ret = bytearray_ass_subscript_lock_held(op, index, values);
Py_END_CRITICAL_SECTION2();
}
else {
Py_BEGIN_CRITICAL_SECTION(op);
ret = bytearray_ass_subscript_lock_held(op, index, values);
Py_END_CRITICAL_SECTION();
}
return ret;
}

Expand Down Expand Up @@ -2742,6 +2754,7 @@ static PyObject *
bytearray_mod_lock_held(PyObject *v, PyObject *w)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(v);
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(w);
if (!PyByteArray_Check(v))
Py_RETURN_NOTIMPLEMENTED;
return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
Expand All @@ -2751,9 +2764,9 @@ static PyObject *
bytearray_mod(PyObject *v, PyObject *w)
{
PyObject *ret;
Py_BEGIN_CRITICAL_SECTION(v);
Py_BEGIN_CRITICAL_SECTION2(v, w);
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
ret = bytearray_mod_lock_held(v, w);
Py_END_CRITICAL_SECTION();
Py_END_CRITICAL_SECTION2();
return ret;
}

Expand Down