Skip to content

Commit 8fde8fa

Browse files
[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 5466f57) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parent 67862fc commit 8fde8fa

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Doc/c-api/bytearray.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Direct API functions
4444
4545
On failure, return ``NULL`` with an exception set.
4646
47+
.. note::
48+
If the object implements the buffer protocol, then the buffer
49+
must not be mutated while the bytearray object is being created.
50+
4751
4852
.. c:function:: PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
4953
@@ -58,6 +62,10 @@ Direct API functions
5862
5963
On failure, return ``NULL`` with an exception set.
6064
65+
.. note::
66+
If the object implements the buffer protocol, then the buffer
67+
must not be mutated while the bytearray object is being created.
68+
6169
6270
.. c:function:: Py_ssize_t PyByteArray_Size(PyObject *bytearray)
6371
@@ -70,6 +78,9 @@ Direct API functions
7078
``NULL`` pointer. The returned array always has an extra
7179
null byte appended.
7280
81+
.. note::
82+
It is not thread-safe to mutate the bytearray object while using the returned char array.
83+
7384
7485
.. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
7586
@@ -89,6 +100,9 @@ These macros trade safety for speed and they don't check pointers.
89100
90101
Similar to :c:func:`PyByteArray_AsString`, but without error checking.
91102
103+
.. note::
104+
It is not thread-safe to mutate the bytearray object while using the returned char array.
105+
92106
93107
.. c:function:: Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
94108

Doc/data/threadsafety.dat

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,20 @@ _PyBytes_Resize:distinct:
107107

108108
# Repr - atomic as bytes are immutable
109109
PyBytes_Repr:atomic:
110+
111+
# Creation from object - may call arbitrary code
112+
PyByteArray_FromObject:shared:
113+
114+
# Creation - pure allocation, no shared state
115+
PyByteArray_FromStringAndSize:atomic:
116+
117+
# Concatenation - uses buffer protocol; safe as long as buffer is not mutated by another thread during the operation
118+
PyByteArray_Concat:shared:
119+
120+
# Size - uses atomic load on free-threaded builds
121+
PyByteArray_Size:atomic:
122+
PyByteArray_GET_SIZE:atomic:
123+
124+
# Raw data - no locking; mutating it is unsafe if the bytearray object is shared between threads
125+
PyByteArray_AsString:compatible:
126+
PyByteArray_AS_STRING:compatible:

0 commit comments

Comments
 (0)