Skip to content

[C API] Add PyList_AsTupleAndClear() function #161635

Open

Description

@vstinner

Building a tuple when the size if known is advance is easy: allocate a new tuple with PyTuple_New(size) and then fill items using PyTuple_SetItem() or PyTuple_SET_ITEM().

Building a tuple when the final size is not known in advance is more complicated. It's possible to call _PyTuple_Resize() multiple time to resize the tuple. But it's more efficient to build a list, since the list is overallocated and so mutliple "resize" operations are more efficient.

The problem is that PyList_AsTuple() has to call Py_NewRef() on each time to create the tuple, and then call Py_DECREF() on each item when destroying the temporary list. I propose adding a new PyList_AsTupleAndClear() function to transfer items from the list to the tuple and so avoid the useless INCREF/DECREF dance.

PyList_AsTupleAndClear() should only be called when a temporary list is created in the same function and the list cannot be exposed to Python.

In fact, Python already has an internal _PyList_AsTupleAndClear() function, since it's the most efficient way to build a tuple from a list. I just propose to make the function public.

Linked PRs

Activity

  1. added 2 commits that reference this issue on Nov 8, 2026
  2. vstinner commented on Nov 8, 2026

    @vstinner
    MemberAuthor

    See also PyDict_AsFrozenDictAndClear() function which was added recently: #153410.

    C API Working decision to add PyDict_AsFrozenDictAndClear(): capi-workgroup/decisions#109.

  3. vstinner commented on Nov 8, 2026

    @vstinner
    MemberAuthor
  4. encukou commented on Nov 9, 2026

    @encukou
    Member

    The intended usage for PyDict_AsFrozenDictAndClear is a “builder pattern”, but using a dict instead of a dedicated “DictWriter” type. This is a

    Unlike dicts, there's a proposal to add a ListWriter API. If we add that, building a tuple then means filling a listwriter, finishing it (paying for GC registration), then calling PyList_AsTupleAndClear and discarding the list.
    IMO, if we add PyListWriter, then it would be useful to add PyListWriter_FinishTuple to convert that directly to tuple without finalizing the intermediate list. And with that, PyList_AsTupleAndClear wouldn't be that useful (since as with PyDict_AsFrozenDictAndClear, it's mean to be used on a list you just created).

    Maybe we should rename PyListWriter to PySequenceWriter before adding it, with PySequenceWriter_FinishList and PySequenceWriter_Finish tuple.

  5. vstinner commented on Nov 9, 2026

    @vstinner
    MemberAuthor

    See also #161694 "Add PyTupleWriter API".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions