Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
79a2ae5
gh-158660: Add `PyListWriter` C API
vstinner Oct 21, 2026
d8be30d
Inline PyList_SET_ITEM()
vstinner Oct 21, 2026
398c4b3
Remove redundant test_list_writer()
vstinner Oct 21, 2026
3df6e01
Move _PyObject_GC_TRACK() to PyListWriter_Finish()
vstinner Oct 21, 2026
b0edd12
Merge branch 'main' into list_writer
vstinner Oct 22, 2026
f9d7145
Clarify PyListWriter_ExtendArray() doc
vstinner Oct 22, 2026
d096f46
Use list_new_prealloc() in PyListWriter_Create()
vstinner Oct 22, 2026
a8c24ee
Update PyListWriter_Create() comment
vstinner Oct 22, 2026
31e5f1b
Add PyListWriter_ExtendArraySteal() function
vstinner Oct 22, 2026
db8eb51
Fix doc: use :term: not :ref:
vstinner Oct 23, 2026
da86200
Avoid undefined behavior: memset() with size=0
vstinner Oct 23, 2026
748ac24
Finish() shrinks the list
vstinner Oct 29, 2026
9efaf87
On error, Extend() restores the writer to its previous state
vstinner Nov 3, 2026
8404760
Add PyListWriter_FromFormat()
vstinner Nov 3, 2026
9313299
Fix undefined behavior in listwriter_extend_array() with nitem=0
vstinner Nov 3, 2026
8c7ec58
Inline listwriter_extend_array()
vstinner Nov 3, 2026
e9dbc5e
Add PyListWriter_FromBuffer() function
vstinner Nov 3, 2026
995f2d5
Fix doc
vstinner Nov 3, 2026
c808cb7
Optimize PyListWriter_FromBuffer()
vstinner Nov 3, 2026
8e07fa1
Resize the list once in PyListWriter_FromBuffer()
vstinner Nov 3, 2026
04c4d30
FromBuffer() tests compare to mview.tolist() and array.tolist()
vstinner Nov 4, 2026
9155b17
Replace PyListWriter with PyListBuilder
vstinner Nov 26, 2026
4b3f1bb
Merge branch 'main' into list_writer
vstinner Nov 26, 2026
4a0c537
Remove PyListBuilder_FromFormat()
vstinner Nov 26, 2026
579efe2
Remove PyListBuilder_FromBuffer()
vstinner Nov 26, 2026
d55dd15
Replace writer with builder
vstinner Nov 26, 2026
f93fe9c
Remove PyListBuilder_FromBuffer() remaining code
vstinner Nov 26, 2026
fa01664
Fix compiler warning when building in release mode
vstinner Nov 27, 2026
102756f
Describe PyListBuilder_ExtendArray() error in the doc
vstinner Nov 27, 2026
0d88333
Cleanup
vstinner Nov 27, 2026
3e9a201
Empty commit to trigger the CI
vstinner Nov 27, 2026
7f08f2b
Merge branch 'main' into list_writer
vstinner Nov 27, 2026
42190b8
Run prek --all-files: remove unused import in test_list.py
vstinner Nov 27, 2026
05db290
Fix list_new_prealloc() integer overflow check
vstinner Nov 28, 2026
297af71
Fix _PyListBuilder_INIT_ITEMS
vstinner Nov 28, 2026
ba04abb
Fix PyListBuilder_ExtendArraySteal()
vstinner Nov 28, 2026
d36b2b3
Cleanup
vstinner Nov 28, 2026
4d48166
Fix PyListBuilder_ExtendArraySteal() if an item is NULL
vstinner Nov 30, 2026
3a02f71
Finish() always calls list_resize()
vstinner Nov 30, 2026
5805959
Apply batched suggestions from code review
vstinner Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Doc/c-api/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,107 @@ List Objects

Return a new tuple object containing the contents of *list*; equivalent to
``tuple(list)``.


.. _pylistbuilder:

PyListBuilder
-------------

The :c:type:`PyListBuilder` API can be used to create a Python :class:`list`
object.

On the free-threaded build, it's more efficient than the ``PyList`` C API to
create a :class:`list`, because it can avoid the critical section on the
created list.

.. versionadded:: next


.. c:type:: PyListBuilder

A list builder object.

The API is **not thread safe**. To share a builder with multiple threads,
external synchronization is needed.

The instance must be destroyed by :c:func:`PyListBuilder_Finish` on
success, or :c:func:`PyListBuilder_Discard` on error.


.. c:function:: PyListBuilder* PyListBuilder_Create(Py_ssize_t size)

Create a :c:type:`PyListBuilder` and preallocate *size* items.

On error, set an exception and return ``NULL``.

*size* must be positive or zero.

.. c:function:: PyObject* PyListBuilder_Finish(PyListBuilder *builder)

Finish a :c:type:`PyListBuilder` created by
:c:func:`PyListBuilder_Create`.

On success, return a Python :class:`list` object.
On error, set an exception and return ``NULL``.

The builder instance is invalid after the call in any case.
No API can be called on the builder after :c:func:`PyListBuilder_Finish`.

.. c:function:: void PyListBuilder_Discard(PyListBuilder *builder)

Discard a :c:type:`PyListBuilder` created by :c:func:`PyListBuilder_Create`.

Do nothing if *builder* is ``NULL``.

The builder instance is invalid after the call.
No API can be called on the builder after :c:func:`PyListBuilder_Discard`.

.. c:function:: int PyListBuilder_Append(PyListBuilder *builder, PyObject *item)

Append an item to the list builder.
Similar to :c:func:`PyList_Append`.

On success, return ``0``.
On error, set an exception, leave the builder unchanged, and return ``-1``.

.. c:function:: int PyListBuilder_AppendSteal(PyListBuilder *builder, PyObject *item)

Same as :c:func:`PyListBuilder_Append`, except this function
:term:`steals <steal>` the reference to *item*.

.. c:function:: int PyListBuilder_Extend(PyListBuilder *builder, PyObject *iterable)

Extend the list builder with the contents of *iterable*.
Similar to :c:func:`PyList_Extend`.

On success, return ``0``.
On error, set an exception, leave the builder unchanged, and return ``-1``.

If consuming *iterable* raises an exception, the builder is restored to its
previous state, that is, items added by a failed call are discarded.
However, note that *iterable* is not reverted: items consumed from an
iterator are not added back.

.. c:function:: int PyListBuilder_ExtendArray(PyListBuilder *builder, PyObject *const *items, Py_ssize_t nitem)

Extend the list builder with the contents of *items*: copy references from
*items* to the list builder.

On success, return ``0``.
On error, set an exception, leave the builder unchanged, and return ``-1``.

*nitem* must not be negative.

On failure, the builder is restored to its previous state. That is, items
added by a failed call are discarded from the builder.

.. c:function:: int PyListBuilder_ExtendArraySteal(PyListBuilder *builder, PyObject *const *items, Py_ssize_t nitem)

Same as :c:func:`PyListBuilder_ExtendArray`, but :term:`steal` references
instead of copying them.

The caller must hold a strong reference to each item in *item*.
Both on success and on error, these references are transferred to the builder;
the caller must not use them after the call.
(To clarify: on error, the references are "lost".)
16 changes: 16 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,22 @@ New features
* Add :c:func:`Py_HashBuffer` to the limited C API.
(Contributed by Victor Stinner in :gh:`148233`.)

* Add a new :ref:`PyListBuilder <pylistbuilder>` C API to create a :class:`list`
object. On the free-threaded build, it's more efficient than the ``PyList`` C
API to create a :class:`list`, because it can avoid locking the
list as it's being built. Add functions:

* :c:func:`PyListBuilder_Create`
* :c:func:`PyListBuilder_Finish`
* :c:func:`PyListBuilder_Discard`
* :c:func:`PyListBuilder_Append`
* :c:func:`PyListBuilder_AppendSteal`
* :c:func:`PyListBuilder_Extend`
* :c:func:`PyListBuilder_ExtendArray`
* :c:func:`PyListBuilder_ExtendArraySteal`

(Contributed by Victor Stinner in :gh:`158660`.)

* Add :c:macro:`Py_SETREF` and :c:macro:`Py_XSETREF` macros to the limited C
API.
(Contributed by Nathan Goldbaum and Victor Stinner in :gh:`158842`.)
Expand Down
26 changes: 26 additions & 0 deletions Include/cpython/listobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,29 @@ PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {

PyAPI_FUNC(int) PyList_Extend(PyObject *self, PyObject *iterable);
PyAPI_FUNC(int) PyList_Clear(PyObject *self);


/* --- PyListBuilder API -------------------------------------------------- */

typedef struct PyListBuilder PyListBuilder;

PyAPI_FUNC(PyListBuilder*) PyListBuilder_Create(Py_ssize_t size);
PyAPI_FUNC(PyObject*) PyListBuilder_Finish(PyListBuilder *builder);
PyAPI_FUNC(void) PyListBuilder_Discard(PyListBuilder *builder);
PyAPI_FUNC(int) PyListBuilder_Append(
PyListBuilder *builder,
PyObject *item);
PyAPI_FUNC(int) PyListBuilder_AppendSteal(
PyListBuilder *builder,
PyObject *item);
PyAPI_FUNC(int) PyListBuilder_Extend(
PyListBuilder *builder,
PyObject *iterable);
PyAPI_FUNC(int) PyListBuilder_ExtendArray(
PyListBuilder *builder,
PyObject *const *items,
Py_ssize_t nitem);
PyAPI_FUNC(int) PyListBuilder_ExtendArraySteal(
PyListBuilder *builder,
PyObject *const *items,
Py_ssize_t nitem);
12 changes: 12 additions & 0 deletions Include/internal/pycore_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern "C" {
#include "pycore_stackref.h"
#endif

#define _Py_LIST_SMALL_ALLOCATED 32

PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject *, PyObject *);
PyAPI_FUNC(PyObject) *_PyList_SliceSubscript(PyObject*, PyObject*);
PyAPI_FUNC(PyObject *) _PyList_BinarySlice(PyObject *, PyObject *, PyObject *);
Expand Down Expand Up @@ -77,6 +79,16 @@ union _PyStackRef;
PyAPI_FUNC(PyObject *)_PyList_FromStackRefStealOnSuccess(const union _PyStackRef *src, Py_ssize_t n);
PyAPI_FUNC(PyObject *)_PyList_AsTupleAndClear(PyListObject *v);

#ifdef Py_DEBUG
// In debug mode, PyListBuilder_Finish() makes sure that all list items
// are initialized.
# define _PyListBuilder_INIT_ITEMS 1
#else
// In release mode, list items are not initialized for best performance.
// The caller is responsible to initialize all items.
# define _PyListBuilder_INIT_ITEMS 0
#endif

#ifdef __cplusplus
}
#endif
Expand Down
Loading
Loading