Skip to content

Commit b9cab78

Browse files
committed
gh-160147: Add PySetBuilder C API
Use the new API in: * Modules/_abc.c * Modules/_asynciomodule.c * Modules/_hashopenssl.c * Modules/_ssl.c
1 parent 4743be9 commit b9cab78

11 files changed

Lines changed: 923 additions & 36 deletions

File tree

‎Doc/c-api/set.rst‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ the constructor functions work with any iterable Python object.
8989
actually iterable. The constructor is also useful for copying a set
9090
(``c=set(s)``).
9191
92+
See also the :ref:`PySetBuilder API <pysetbuilder>`.
93+
9294
.. note::
9395
9496
The operation is atomic on :term:`free threading <free-threaded build>`
@@ -102,6 +104,8 @@ the constructor functions work with any iterable Python object.
102104
set on success or ``NULL`` on failure. Raise :exc:`TypeError` if *iterable* is
103105
not actually iterable.
104106
107+
See also the :ref:`PySetBuilder API <pysetbuilder>`.
108+
105109
.. note::
106110
107111
The operation is atomic on :term:`free threading <free-threaded build>`
@@ -213,3 +217,104 @@ Deprecated API
213217
If looking for the size of a set, use :c:func:`PySet_Size` instead.
214218
215219
.. soft-deprecated:: 3.14
220+
221+
222+
.. _pysetbuilder:
223+
224+
PySetBuilder
225+
------------
226+
227+
The :c:type:`PySetBuilder` API can be used to create a :class:`set` object
228+
or a :class:`frozenset` object.
229+
230+
See also the :c:func:`PySet_New` and :c:func:`PyFrozenSet_New` functions.
231+
232+
.. versionadded:: next
233+
234+
235+
.. c:type:: PySetBuilder
236+
237+
A :class:`set` or :class:`frozenset` builder object.
238+
239+
The API is **not thread safe**. To share a builder with multiple threads, a
240+
critical section or a lock is needed.
241+
242+
The instance must be destroyed by :c:func:`PySetBuilder_Finish` on
243+
success, or :c:func:`PySetBuilder_Discard` on error.
244+
245+
246+
.. c:function:: PySetBuilder* PySetBuilder_CreateSet(Py_ssize_t size)
247+
248+
Create a :class:`set` using a :c:type:`PySetBuilder`.
249+
250+
If *size* is greater than zero, allocate an internal table big enough to
251+
store *size* items.
252+
253+
On error, set an exception and return ``NULL``.
254+
255+
*size* must be positive or zero.
256+
257+
.. c:function:: PySetBuilder* PySetBuilder_CreateFrozenSet(Py_ssize_t size)
258+
259+
Similar to :c:func:`PySetBuilder_CreateSet`, but create a :class:`frozenset`
260+
object instead of a :class:`set` object.
261+
262+
.. c:function:: PyObject* PySetBuilder_Finish(PySetBuilder *builder)
263+
264+
Finish a :c:type:`PySetBuilder` created by
265+
:c:func:`PySetBuilder_CreateFrozenSet`.
266+
267+
On success, return a :class:`set` object (if
268+
:c:func:`PySetBuilder_CreateSet` was used) or a :class:`frozenset` object
269+
(if :c:func:`PySetBuilder_CreateFrozenSet` was used).
270+
On error, set an exception and return ``NULL``.
271+
272+
The builder instance is invalid after the call in any case.
273+
No API can be called on the builder after :c:func:`PySetBuilder_Finish`.
274+
275+
.. c:function:: void PySetBuilder_Discard(PySetBuilder *builder)
276+
277+
Discard a :c:type:`PySetBuilder` created by :c:func:`PySetBuilder_CreateFrozenSet`.
278+
279+
Do nothing if *builder* is ``NULL``.
280+
281+
The builder instance is invalid after the call.
282+
No API can be called on the builder after :c:func:`PySetBuilder_Discard`.
283+
284+
.. c:function:: int PySetBuilder_Add(PySetBuilder *builder, PyObject *item)
285+
286+
Add *item* to a set builder.
287+
288+
On success, return ``0``.
289+
On error, set an exception and return ``-1``.
290+
291+
.. c:function:: int PySetBuilder_AddSteal(PySetBuilder *builder, PyObject *item)
292+
293+
Similar to :c:func:`PySetBuilder_Add`, but :term:`steal` the *item*
294+
reference.
295+
296+
.. c:function:: int PySetBuilder_Update(PySetBuilder *builder, PyObject *iterable)
297+
298+
Update the set builder, adding elements from *iterable*.
299+
300+
On success, return ``0``.
301+
On error, set an exception and return ``-1``.
302+
303+
On error, elements already added are kept.
304+
305+
.. c:function:: int PySetBuilder_UpdateArray(PySetBuilder *builder, PyObject *const *items, Py_ssize_t nitem)
306+
307+
Update the set builder, adding *nitem* elements from *items* array:
308+
copy references from *items* to the builder.
309+
310+
On success, return ``0``.
311+
On error, set an exception and return ``-1``.
312+
313+
On error, elements already added are kept.
314+
315+
*nitem* must be positive or zero.
316+
317+
.. c:function:: int PySetBuilder_UpdateArraySteal(PySetBuilder *builder, PyObject *const *items, Py_ssize_t nitem)
318+
319+
Similar to :c:func:`PySetBuilder_UpdateArray`, but :term:`steal`
320+
references instead of copying references.

‎Doc/whatsnew/3.16.rst‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,21 @@ New features
15841584
* Add :c:func:`Py_HashBuffer` to the limited C API.
15851585
(Contributed by Victor Stinner in :gh:`148233`.)
15861586

1587+
* Add a new :ref:`PySetBuilder <pysetbuilder>` C API to create
1588+
a :class:`set` object or a :class:`frozenset` object. Add functions:
1589+
1590+
* :c:func:`PySetBuilder_CreateSet`
1591+
* :c:func:`PySetBuilder_CreateFrozenSet`
1592+
* :c:func:`PySetBuilder_Finish`
1593+
* :c:func:`PySetBuilder_Discard`
1594+
* :c:func:`PySetBuilder_Add`
1595+
* :c:func:`PySetBuilder_AddSteal`
1596+
* :c:func:`PySetBuilder_Update`
1597+
* :c:func:`PySetBuilder_UpdateArray`
1598+
* :c:func:`PySetBuilder_UpdateArraySteal`
1599+
1600+
(Contributed by Victor Stinner in :gh:`160147`.)
1601+
15871602
* Add :c:macro:`Py_SETREF` and :c:macro:`Py_XSETREF` macros to the limited C
15881603
API.
15891604
(Contributed by Nathan Goldbaum and Victor Stinner in :gh:`158842`.)

‎Include/cpython/setobject.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,30 @@ static inline Py_ssize_t PySet_GET_SIZE(PyObject *so) {
6969
#endif
7070
}
7171
#define PySet_GET_SIZE(so) PySet_GET_SIZE(_PyObject_CAST(so))
72+
73+
74+
// --- PySetBuilder API ------------------------------------------------
75+
76+
typedef struct PySetBuilder PySetBuilder;
77+
78+
PyAPI_FUNC(PySetBuilder*) PySetBuilder_CreateSet(Py_ssize_t size);
79+
PyAPI_FUNC(PySetBuilder*) PySetBuilder_CreateFrozenSet(Py_ssize_t size);
80+
PyAPI_FUNC(PyObject*) PySetBuilder_Finish(PySetBuilder *builder);
81+
PyAPI_FUNC(void) PySetBuilder_Discard(PySetBuilder *builder);
82+
PyAPI_FUNC(int) PySetBuilder_Add(
83+
PySetBuilder *builder,
84+
PyObject *item);
85+
PyAPI_FUNC(int) PySetBuilder_AddSteal(
86+
PySetBuilder *builder,
87+
PyObject *item);
88+
PyAPI_FUNC(int) PySetBuilder_Update(
89+
PySetBuilder *builder,
90+
PyObject *iterable);
91+
PyAPI_FUNC(int) PySetBuilder_UpdateArray(
92+
PySetBuilder *builder,
93+
PyObject *const *items,
94+
Py_ssize_t nitem);
95+
PyAPI_FUNC(int) PySetBuilder_UpdateArraySteal(
96+
PySetBuilder *builder,
97+
PyObject *const *items,
98+
Py_ssize_t nitem);

0 commit comments

Comments
 (0)