Skip to content

Commit 6b73c34

Browse files
add thread safety annotations for bytearray
1 parent 0c7a75a commit 6b73c34

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,21 @@ PyList_Sort:shared:
7373
# Extend - lock target list; also lock source when it is a
7474
# list, set, or dict
7575
PyList_Extend:shared:
76+
77+
78+
# Creation from object - may call arbitrary code
79+
PyByteArray_FromObject:shared:
80+
81+
# Creation - pure allocation, no shared state
82+
PyByteArray_FromStringAndSize:atomic:
83+
84+
# Concatenation - uses buffer protocol; safe as long as buffer is not mutated by another thread during the operation
85+
PyByteArray_Concat:shared:
86+
87+
# Size - uses atomic load on free-threaded builds
88+
PyByteArray_Size:atomic:
89+
PyByteArray_GET_SIZE:atomic:
90+
91+
# Raw data - no locking; mutating it is unsafe if the bytearray object is shared between threads
92+
PyByteArray_AsString:compatible:
93+
PyByteArray_AS_STRING:compatible:

0 commit comments

Comments
 (0)