]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142518: add thread safety annotations for bytearray C-API (#146514)
authorKumar Aditya <kumaraditya@python.org>
Fri, 27 Mar 2026 13:10:26 +0000 (18:40 +0530)
committerGitHub <noreply@github.com>
Fri, 27 Mar 2026 13:10:26 +0000 (18:40 +0530)
Doc/c-api/bytearray.rst
Doc/data/threadsafety.dat

index e2b22ec3c794ae4471dfebca5968d3cf11faee07..2b36da997d4295634b2ee4fc30d7427f4fc8a11d 100644 (file)
@@ -44,6 +44,10 @@ Direct API functions
 
    On failure, return ``NULL`` with an exception set.
 
+   .. note::
+      If the object implements the buffer protocol, then the buffer
+      must not be mutated while the bytearray object is being created.
+
 
 .. c:function:: PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
 
@@ -58,6 +62,10 @@ Direct API functions
 
    On failure, return ``NULL`` with an exception set.
 
+   .. note::
+      If the object implements the buffer protocol, then the buffer
+      must not be mutated while the bytearray object is being created.
+
 
 .. c:function:: Py_ssize_t PyByteArray_Size(PyObject *bytearray)
 
@@ -70,6 +78,9 @@ Direct API functions
    ``NULL`` pointer.  The returned array always has an extra
    null byte appended.
 
+   .. note::
+      It is not thread-safe to mutate the bytearray object while using the returned char array.
+
 
 .. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
 
@@ -89,6 +100,9 @@ These macros trade safety for speed and they don't check pointers.
 
    Similar to :c:func:`PyByteArray_AsString`, but without error checking.
 
+   .. note::
+      It is not thread-safe to mutate the bytearray object while using the returned char array.
+
 
 .. c:function:: Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
 
index 1210ab17aaa35b8482481b47acac762179f577be..afb053adf5c62b46bbf69acc02caccb0f3e26958 100644 (file)
@@ -107,3 +107,20 @@ _PyBytes_Resize:distinct:
 
 # Repr - atomic as bytes are immutable
 PyBytes_Repr:atomic:
+
+# Creation from object - may call arbitrary code
+PyByteArray_FromObject:shared:
+
+# Creation - pure allocation, no shared state
+PyByteArray_FromStringAndSize:atomic:
+
+# Concatenation - uses buffer protocol; safe as long as buffer is not mutated by another thread during the operation
+PyByteArray_Concat:shared:
+
+# Size - uses atomic load on free-threaded builds
+PyByteArray_Size:atomic:
+PyByteArray_GET_SIZE:atomic:
+
+# Raw data - no locking; mutating it is unsafe if the bytearray object is shared between threads
+PyByteArray_AsString:compatible:
+PyByteArray_AS_STRING:compatible:
\ No newline at end of file