]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129107: fix thread safety of `bytearray` where two critical sections are needed...
authorTomasz Pytel <tompytel@gmail.com>
Thu, 27 Feb 2025 14:59:58 +0000 (09:59 -0500)
committerGitHub <noreply@github.com>
Thu, 27 Feb 2025 14:59:58 +0000 (20:29 +0530)
Lib/test/test_bytes.py
Misc/NEWS.d/next/Core_and_Builtins/2025-02-17-15-32-26.gh-issue-129107.fPPBLw.rst [new file with mode: 0644]
Objects/bytearrayobject.c

index 907988c27eba0a67a0f7a6f2c50be148270eebe2..f6ffe83c5d69e805e56bb69b8f4c4b06105977ef 100644 (file)
@@ -2380,12 +2380,22 @@ class FreeThreadingTest(unittest.TestCase):
             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)
@@ -2503,7 +2513,9 @@ class FreeThreadingTest(unittest.TestCase):
 
         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))
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-17-15-32-26.gh-issue-129107.fPPBLw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-17-15-32-26.gh-issue-129107.fPPBLw.rst
new file mode 100644 (file)
index 0000000..ca921f4
--- /dev/null
@@ -0,0 +1 @@
+Fix two more :class:`bytearray` functions for :term:`free threading`.
index f2cfd4aed3979f560c51917e1a1c1e0b139cf011..34f43eb8c31315bce93e7f64a277e2e378c19f66 100644 (file)
@@ -864,9 +864,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 && PyByteArray_Check(values)) {
+        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;
 }
 
@@ -2751,9 +2758,16 @@ static PyObject *
 bytearray_mod(PyObject *v, PyObject *w)
 {
     PyObject *ret;
-    Py_BEGIN_CRITICAL_SECTION(v);
-    ret = bytearray_mod_lock_held(v, w);
-    Py_END_CRITICAL_SECTION();
+    if (PyByteArray_Check(w)) {
+        Py_BEGIN_CRITICAL_SECTION2(v, w);
+        ret = bytearray_mod_lock_held(v, w);
+        Py_END_CRITICAL_SECTION2();
+    }
+    else {
+        Py_BEGIN_CRITICAL_SECTION(v);
+        ret = bytearray_mod_lock_held(v, w);
+        Py_END_CRITICAL_SECTION();
+    }
     return ret;
 }