Skip to content

Commit e2fc103

Browse files
authored
gh-158660: Add PyListBuilder to the limited C API (#162965)
Move PyListBuilder tests from _testcapi to _testlimitedcapi. Use PyObject_GetTypeData() and avoid PyObject_HEAD to support Free Threading.
1 parent 6740479 commit e2fc103

12 files changed

Lines changed: 345 additions & 291 deletions

File tree

‎Doc/data/stable_abi.dat‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Doc/whatsnew/3.16.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,9 @@ New features
16141614
* Add :c:func:`Py_HashBuffer` to the limited C API.
16151615
(Contributed by Victor Stinner in :gh:`148233`.)
16161616

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

‎Include/cpython/listobject.h‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,3 @@ PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
5151

5252
PyAPI_FUNC(int) PyList_Extend(PyObject *self, PyObject *iterable);
5353
PyAPI_FUNC(int) PyList_Clear(PyObject *self);
54-
55-
56-
/* --- PyListBuilder API -------------------------------------------------- */
57-
58-
typedef struct PyListBuilder PyListBuilder;
59-
60-
PyAPI_FUNC(PyListBuilder*) PyListBuilder_Create(Py_ssize_t size);
61-
PyAPI_FUNC(PyObject*) PyListBuilder_Finish(PyListBuilder *builder);
62-
PyAPI_FUNC(void) PyListBuilder_Discard(PyListBuilder *builder);
63-
PyAPI_FUNC(int) PyListBuilder_Append(
64-
PyListBuilder *builder,
65-
PyObject *item);
66-
PyAPI_FUNC(int) PyListBuilder_AppendSteal(
67-
PyListBuilder *builder,
68-
PyObject *item);
69-
PyAPI_FUNC(int) PyListBuilder_Extend(
70-
PyListBuilder *builder,
71-
PyObject *iterable);
72-
PyAPI_FUNC(int) PyListBuilder_ExtendArray(
73-
PyListBuilder *builder,
74-
PyObject *const *items,
75-
Py_ssize_t nitem);
76-
PyAPI_FUNC(int) PyListBuilder_ExtendArraySteal(
77-
PyListBuilder *builder,
78-
PyObject *const *items,
79-
Py_ssize_t nitem);

‎Include/listobject.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,37 @@ PyAPI_FUNC(int) PyList_Sort(PyObject *);
4343
PyAPI_FUNC(int) PyList_Reverse(PyObject *);
4444
PyAPI_FUNC(PyObject *) PyList_AsTuple(PyObject *);
4545

46+
47+
/* --- PyListBuilder API -------------------------------------------------- */
48+
49+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 16)
50+
typedef struct PyListBuilder PyListBuilder;
51+
52+
PyAPI_FUNC(PyListBuilder*) PyListBuilder_Create(Py_ssize_t size);
53+
PyAPI_FUNC(PyObject*) PyListBuilder_Finish(PyListBuilder *builder);
54+
PyAPI_FUNC(void) PyListBuilder_Discard(PyListBuilder *builder);
55+
PyAPI_FUNC(int) PyListBuilder_Append(
56+
PyListBuilder *builder,
57+
PyObject *item);
58+
PyAPI_FUNC(int) PyListBuilder_AppendSteal(
59+
PyListBuilder *builder,
60+
PyObject *item);
61+
PyAPI_FUNC(int) PyListBuilder_Extend(
62+
PyListBuilder *builder,
63+
PyObject *iterable);
64+
PyAPI_FUNC(int) PyListBuilder_ExtendArray(
65+
PyListBuilder *builder,
66+
PyObject *const *items,
67+
Py_ssize_t nitem);
68+
PyAPI_FUNC(int) PyListBuilder_ExtendArraySteal(
69+
PyListBuilder *builder,
70+
PyObject *const *items,
71+
Py_ssize_t nitem);
72+
#endif
73+
74+
75+
/* --- Non-limited API ---------------------------------------------------- */
76+
4677
#ifndef Py_LIMITED_API
4778
# define Py_CPYTHON_LISTOBJECT_H
4879
# include "cpython/listobject.h"

‎Lib/test/test_capi/test_list.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def test_uninitialized_list_repr(self):
368368
class ListBuilderTests(unittest.TestCase):
369369
def create_builder(self, size):
370370
# Test PyListBuilder_Create()
371-
return _testcapi.PyListBuilder(size)
371+
return _testlimitedcapi.PyListBuilder(size)
372372

373373
def list_equal(self, result, expected):
374374
if not isinstance(expected, list):

‎Lib/test/test_stable_abi_ctypes.py‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Misc/NEWS.d/next/C_API/2026-10-21-16-35-42.gh-issue-158660.KvixNp.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
Add a new :ref:`PyListBuilder <pylistbuilder>` C API to create a :class:`list`
2-
object. On the free-threaded build, it's more efficient than the ``PyList`` C
1+
Add a new :ref:`PyListBuilder <pylistbuilder>` API to the limited C API
2+
to create a :class:`list` object.
3+
On the free-threaded build, it's more efficient than the ``PyList`` C
34
API to create a :class:`list`, because it can avoid the critical section on the
45
created list. Add functions:
56

‎Misc/stable_abi.toml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,3 +2941,22 @@
29412941
added = '3.16'
29422942
[function.PyUnicodeWriter_DecodeUTF8Stateful]
29432943
added = '3.16'
2944+
[struct.PyListBuilder]
2945+
added = '3.16'
2946+
struct_abi_kind = 'opaque'
2947+
[function.PyListBuilder_Create]
2948+
added = '3.16'
2949+
[function.PyListBuilder_Finish]
2950+
added = '3.16'
2951+
[function.PyListBuilder_Discard]
2952+
added = '3.16'
2953+
[function.PyListBuilder_Append]
2954+
added = '3.16'
2955+
[function.PyListBuilder_AppendSteal]
2956+
added = '3.16'
2957+
[function.PyListBuilder_Extend]
2958+
added = '3.16'
2959+
[function.PyListBuilder_ExtendArray]
2960+
added = '3.16'
2961+
[function.PyListBuilder_ExtendArraySteal]
2962+
added = '3.16'

0 commit comments

Comments
 (0)