https://github.com/python/cpython/commit/8fde8faffe859bc453c84bbd9a27628f96568f40
commit: 8fde8faffe859bc453c84bbd9a27628f96568f40
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-03-27T19:21:05+05:30
summary:

[3.14] gh-142518: add thread safety annotations for bytearray C-API (GH-146514) 
(#146516)

gh-142518: add thread safety annotations for bytearray C-API (GH-146514)
(cherry picked from commit 5466f57eaddeec7f07a681993b22167e42c9807a)

Co-authored-by: Kumar Aditya <[email protected]>

files:
M Doc/c-api/bytearray.rst
M Doc/data/threadsafety.dat

diff --git a/Doc/c-api/bytearray.rst b/Doc/c-api/bytearray.rst
index e2b22ec3c794ae..2b36da997d4295 100644
--- a/Doc/c-api/bytearray.rst
+++ b/Doc/c-api/bytearray.rst
@@ -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)
 
diff --git a/Doc/data/threadsafety.dat b/Doc/data/threadsafety.dat
index 1210ab17aaa35b..afb053adf5c62b 100644
--- a/Doc/data/threadsafety.dat
+++ b/Doc/data/threadsafety.dat
@@ -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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to