[3.14] gh-142518: add thread safety docs for dict and set APIs (#148392) · python/cpython@6112e2d · GitHub
Skip to content

Commit 6112e2d

Browse files
kumaraditya303lysnikolaouvstinner
authored
[3.14] gh-142518: add thread safety docs for dict and set APIs (#148392)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 620fb74 commit 6112e2d

3 files changed

Lines changed: 211 additions & 2 deletions

File tree

Doc/c-api/dict.rst

Lines changed: 94 additions & 0 deletions

Doc/c-api/set.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ the constructor functions work with any iterable Python object.
9292
actually iterable. The constructor is also useful for copying a set
9393
(``c=set(s)``).
9494
95+
.. note::
96+
97+
The operation is atomic on :term:`free threading <free-threaded build>`
98+
when *iterable* is a :class:`set`, :class:`frozenset` or :class:`dict`.
99+
95100
96101
.. c:function:: PyObject* PyFrozenSet_New(PyObject *iterable)
97102
@@ -100,6 +105,11 @@ the constructor functions work with any iterable Python object.
100105
set on success or ``NULL`` on failure. Raise :exc:`TypeError` if *iterable* is
101106
not actually iterable.
102107
108+
.. note::
109+
110+
The operation is atomic on :term:`free threading <free-threaded build>`
111+
when *iterable* is a :class:`set`, :class:`frozenset` or :class:`dict`.
112+
103113
104114
The following functions and macros are available for instances of :class:`set`
105115
or :class:`frozenset` or instances of their subtypes.
@@ -127,6 +137,10 @@ or :class:`frozenset` or instances of their subtypes.
127137
the *key* is unhashable. Raise :exc:`SystemError` if *anyset* is not a
128138
:class:`set`, :class:`frozenset`, or an instance of a subtype.
129139
140+
.. note::
141+
142+
The operation is atomic on :term:`free threading <free-threaded build>`
143+
when *key* is :class:`str`, :class:`int`, :class:`float`, :class:`bool` or :class:`bytes`.
130144
131145
.. c:function:: int PySet_Add(PyObject *set, PyObject *key)
132146
@@ -138,6 +152,12 @@ or :class:`frozenset` or instances of their subtypes.
138152
:exc:`SystemError` if *set* is not an instance of :class:`set` or its
139153
subtype.
140154
155+
.. note::
156+
157+
The operation is atomic on :term:`free threading <free-threaded build>`
158+
when *key* is :class:`str`, :class:`int`, :class:`float`, :class:`bool` or :class:`bytes`.
159+
160+
141161
142162
The following functions are available for instances of :class:`set` or its
143163
subtypes but not for instances of :class:`frozenset` or its subtypes.
@@ -152,6 +172,11 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
152172
temporary frozensets. Raise :exc:`SystemError` if *set* is not an
153173
instance of :class:`set` or its subtype.
154174
175+
.. note::
176+
177+
The operation is atomic on :term:`free threading <free-threaded build>`
178+
when *key* is :class:`str`, :class:`int`, :class:`float`, :class:`bool` or :class:`bytes`.
179+
155180
156181
.. c:function:: PyObject* PySet_Pop(PyObject *set)
157182
@@ -167,6 +192,12 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
167192
success. Return ``-1`` and raise :exc:`SystemError` if *set* is not an instance of
168193
:class:`set` or its subtype.
169194
195+
.. note::
196+
197+
In the :term:`free-threaded build`, the set is emptied before its entries
198+
are cleared, so other threads will observe an empty set rather than
199+
intermediate states.
200+
170201
171202
Deprecated API
172203
^^^^^^^^^^^^^^

Doc/data/threadsafety.dat

Lines changed: 86 additions & 2 deletions

0 commit comments

Comments
 (0)