Skip to content

Commit c92c681

Browse files
address review
1 parent a8d04a4 commit c92c681

File tree

2 files changed

+35
-52
lines changed

2 files changed

+35
-52
lines changed

Doc/c-api/dict.rst

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ Dictionary objects
7676
7777
The first argument can be a :class:`dict` or a :class:`frozendict`.
7878
79+
.. note::
80+
81+
The operation is atomic in the :term:`free-threaded build`, if *key*
82+
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
83+
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
84+
7985
.. versionchanged:: next
8086
Also accept :class:`frozendict`.
8187
@@ -107,12 +113,9 @@ Dictionary objects
107113
108114
.. note::
109115
110-
In the :term:`free-threaded build`, key hashing via
111-
:meth:`~object.__hash__` and key comparison via :meth:`~object.__eq__`
112-
can execute arbitrary Python code, during which the :term:`per-object
113-
lock` may be temporarily released. For built-in key types
114-
(:class:`str`, :class:`int`, :class:`float`), the lock is not released
115-
during comparison.
116+
The operation is atomic in the :term:`free-threaded build`, if *key*
117+
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
118+
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
116119
117120
118121
.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
@@ -121,15 +124,6 @@ Dictionary objects
121124
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
122125
rather than a :c:expr:`PyObject*`.
123126
124-
.. note::
125-
126-
In the :term:`free-threaded build`, key hashing via
127-
:meth:`~object.__hash__` and key comparison via :meth:`~object.__eq__`
128-
can execute arbitrary Python code, during which the :term:`per-object
129-
lock` may be temporarily released. For built-in key types
130-
(:class:`str`, :class:`int`, :class:`float`), the lock is not released
131-
during comparison.
132-
133127
134128
.. c:function:: int PyDict_DelItem(PyObject *p, PyObject *key)
135129
@@ -140,12 +134,9 @@ Dictionary objects
140134
141135
.. note::
142136
143-
In the :term:`free-threaded build`, key hashing via
144-
:meth:`~object.__hash__` and key comparison via :meth:`~object.__eq__`
145-
can execute arbitrary Python code, during which the :term:`per-object
146-
lock` may be temporarily released. For built-in key types
147-
(:class:`str`, :class:`int`, :class:`float`), the lock is not released
148-
during comparison.
137+
The operation is atomic in the :term:`free-threaded build`, if *key*
138+
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
139+
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
149140
150141
151142
.. c:function:: int PyDict_DelItemString(PyObject *p, const char *key)
@@ -154,15 +145,6 @@ Dictionary objects
154145
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
155146
rather than a :c:expr:`PyObject*`.
156147
157-
.. note::
158-
159-
In the :term:`free-threaded build`, key hashing via
160-
:meth:`~object.__hash__` and key comparison via :meth:`~object.__eq__`
161-
can execute arbitrary Python code, during which the :term:`per-object
162-
lock` may be temporarily released. For built-in key types
163-
(:class:`str`, :class:`int`, :class:`float`), the lock is not released
164-
during comparison.
165-
166148
167149
.. c:function:: int PyDict_GetItemRef(PyObject *p, PyObject *key, PyObject **result)
168150
@@ -176,6 +158,12 @@ Dictionary objects
176158
177159
The first argument can be a :class:`dict` or a :class:`frozendict`.
178160
161+
.. note::
162+
163+
The operation is atomic in the :term:`free-threaded build`, if *key*
164+
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
165+
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
166+
179167
.. versionadded:: 3.13
180168
181169
.. versionchanged:: next
@@ -305,6 +293,12 @@ Dictionary objects
305293
These may refer to the same object: in that case you hold two separate
306294
references to it.
307295
296+
.. note::
297+
298+
The operation is atomic in the :term:`free-threaded build`, if *key*
299+
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
300+
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
301+
308302
.. versionadded:: 3.13
309303
310304
@@ -322,16 +316,13 @@ Dictionary objects
322316
Similar to :meth:`dict.pop`, but without the default value and
323317
not raising :exc:`KeyError` if the key is missing.
324318
325-
.. versionadded:: 3.13
326-
327319
.. note::
328320
329-
In the :term:`free-threaded build`, key hashing via
330-
:meth:`~object.__hash__` and key comparison via :meth:`~object.__eq__`
331-
can execute arbitrary Python code, during which the :term:`per-object
332-
lock` may be temporarily released. For built-in key types
333-
(:class:`str`, :class:`int`, :class:`float`), the lock is not released
334-
during comparison.
321+
The operation is atomic in the :term:`free-threaded build`, if *key*
322+
is a builtin type (e.g. :class:`str`, :class:`int`, :class:`float`) or any
323+
other object which does not define :meth:`~object.__hash__` and :meth:`~object.__eq__` methods.
324+
325+
.. versionadded:: 3.13
335326
336327
337328
.. c:function:: int PyDict_PopString(PyObject *p, const char *key, PyObject **result)
@@ -342,15 +333,6 @@ Dictionary objects
342333
343334
.. versionadded:: 3.13
344335
345-
.. note::
346-
347-
In the :term:`free-threaded build`, key hashing via
348-
:meth:`~object.__hash__` and key comparison via :meth:`~object.__eq__`
349-
can execute arbitrary Python code, during which the :term:`per-object
350-
lock` may be temporarily released. For built-in key types
351-
(:class:`str`, :class:`int`, :class:`float`), the lock is not released
352-
during comparison.
353-
354336
355337
.. c:function:: PyObject* PyDict_Items(PyObject *p)
356338

Doc/data/threadsafety.dat

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ PyDict_CheckExact:atomic:
2929
PyDict_New:atomic:
3030

3131
# Lock-free lookups - use _Py_dict_lookup_threadsafe(), no locking
32-
PyDict_Contains:atomic:
32+
# atomic with simple types
33+
PyDict_Contains:shared:
3334
PyDict_ContainsString:atomic:
34-
PyDict_GetItemRef:atomic:
35+
PyDict_GetItemRef:shared:
3536
PyDict_GetItemStringRef:atomic:
3637
PyDict_Size:atomic:
3738
PyDict_GET_SIZE:atomic:
@@ -49,12 +50,12 @@ PyDict_Next:compatible:
4950

5051
# Single-item mutations - protected by per-object critical section
5152
PyDict_SetItem:shared:
52-
PyDict_SetItemString:shared:
53+
PyDict_SetItemString:atomic:
5354
PyDict_DelItem:shared:
54-
PyDict_DelItemString:shared:
55+
PyDict_DelItemString:atomic:
5556
PyDict_SetDefaultRef:shared:
5657
PyDict_Pop:shared:
57-
PyDict_PopString:shared:
58+
PyDict_PopString:atomic:
5859

5960
# Bulk reads - hold per-object lock for duration
6061
PyDict_Clear:atomic:

0 commit comments

Comments
 (0)