@@ -227,3 +227,107 @@ List Objects
227227
228228 Return a new tuple object containing the contents of *list *; equivalent to
229229 ``tuple(list) ``.
230+
231+
232+ .. _pylistbuilder :
233+
234+ PyListBuilder
235+ -------------
236+
237+ The :c:type: `PyListBuilder ` API can be used to create a Python :class: `list `
238+ object.
239+
240+ On the free-threaded build, it's more efficient than the ``PyList `` C API to
241+ create a :class: `list `, because it can avoid the critical section on the
242+ created list.
243+
244+ .. versionadded :: next
245+
246+
247+ .. c :type :: PyListBuilder
248+
249+ A list builder object.
250+
251+ The API is **not thread safe **. To share a builder with multiple threads,
252+ external synchronization is needed.
253+
254+ The instance must be destroyed by :c:func: `PyListBuilder_Finish ` on
255+ success, or :c:func: `PyListBuilder_Discard ` on error.
256+
257+
258+ .. c :function :: PyListBuilder* PyListBuilder_Create (Py_ssize_t size)
259+
260+ Create a :c:type:`PyListBuilder` and preallocate *size* items.
261+
262+ On error, set an exception and return ``NULL``.
263+
264+ *size* must be positive or zero.
265+
266+ .. c:function:: PyObject* PyListBuilder_Finish(PyListBuilder *builder)
267+
268+ Finish a :c:type: `PyListBuilder ` created by
269+ :c:func: `PyListBuilder_Create `.
270+
271+ On success, return a Python :class: `list ` object.
272+ On error, set an exception and return ``NULL ``.
273+
274+ The builder instance is invalid after the call in any case.
275+ No API can be called on the builder after :c:func: `PyListBuilder_Finish `.
276+
277+ .. c :function :: void PyListBuilder_Discard (PyListBuilder *builder)
278+
279+ Discard a :c:type: `PyListBuilder ` created by :c:func: `PyListBuilder_Create `.
280+
281+ Do nothing if *builder * is ``NULL ``.
282+
283+ The builder instance is invalid after the call.
284+ No API can be called on the builder after :c:func: `PyListBuilder_Discard `.
285+
286+ .. c :function :: int PyListBuilder_Append (PyListBuilder *builder, PyObject *item)
287+
288+ Append an item to the list builder.
289+ Similar to :c:func: `PyList_Append `.
290+
291+ On success, return ``0 ``.
292+ On error, set an exception, leave the builder unchanged, and return ``-1 ``.
293+
294+ .. c :function :: int PyListBuilder_AppendSteal (PyListBuilder *builder, PyObject *item)
295+
296+ Same as :c:func: `PyListBuilder_Append `, except this function
297+ :term: `steals <steal> ` the reference to *item *.
298+
299+ .. c :function :: int PyListBuilder_Extend (PyListBuilder *builder, PyObject *iterable)
300+
301+ Extend the list builder with the contents of *iterable *.
302+ Similar to :c:func: `PyList_Extend `.
303+
304+ On success, return ``0 ``.
305+ On error, set an exception, leave the builder unchanged, and return ``-1 ``.
306+
307+ If consuming *iterable * raises an exception, the builder is restored to its
308+ previous state, that is, items added by a failed call are discarded.
309+ However, note that *iterable * is not reverted: items consumed from an
310+ iterator are not added back.
311+
312+ .. c :function :: int PyListBuilder_ExtendArray (PyListBuilder *builder, PyObject *const *items, Py_ssize_t nitem)
313+
314+ Extend the list builder with the contents of *items *: copy references from
315+ *items * to the list builder.
316+
317+ On success, return ``0 ``.
318+ On error, set an exception, leave the builder unchanged, and return ``-1 ``.
319+
320+ *nitem * must not be negative.
321+
322+ On failure, the builder is restored to its previous state. That is, items
323+ added by a failed call are discarded from the builder.
324+
325+ .. c :function :: int PyListBuilder_ExtendArraySteal (PyListBuilder *builder, PyObject *const *items, Py_ssize_t nitem)
326+
327+ Same as :c:func: `PyListBuilder_ExtendArray `, but :term: `steal ` references
328+ instead of copying them.
329+
330+ The caller must hold a strong reference to each item in *item *.
331+ Both on success and on error, these references are transferred to the builder;
332+ the caller must not use them after the call.
333+ (To clarify: on error, the references are "lost".)
0 commit comments