GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [C API] Add `PyTupleWriter` API · Issue #161694 · python/cpython

[C API] Add PyTupleWriter API #161694

New issue
Copy link
New issue
Copy link
Open
Open
[C API] Add PyTupleWriter API#161694
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@vstinner
vstinner
opened on Nov 9, 2026
Issue body actions

Currently, to build a tuple, PyTuple_New() should be used with PyTuple_SetItem() or PyTuple_SET_ITEM(). This API has multiple issues: it modifies an immutable tuple and the tuple is directly tracked by the GC. There is also _PyTuple_Resize() function to resize an immutable tuple.

I propose adding a new PyTupleWriter C API to clarify the "build" phase which ends with a Finish() call (or Discard() on error or if the builder is no longer needed).

The problem of designing such API is that it should be as efficient or even more efficient than the existing PyTuple_New() + PyTuple_SET_ITEM() pattern. I made a first attempt in 2023. Last year, I made a second attempt which didn't convince other developers. The API was based on PyTupleWriter_Create() + PyTupleWriter_Add() + PyTupleWriter_Finish(), and PyTupleWriter_Create() allocated a structure on the heap memory.

I'm back with a different design: PyTupleWriter_Create() no longer allocates a structure on the heap memory, it's just a PyTupleObject* pointer hidden as an opaque PyTupleWriter* type. So PyTupleWriter_Create() and PyTupleWriter_Finish() are fast. The API basically allocates an array of PyObject* and the caller has to fill the array. The array is not initialized for best performance. The caller is responsible to fill all items, otherwise the tuple contains uninitialized memory.

See #139888 for the rationale on adding PyTupleWriter API.

Linked PRs

  • gh-161694: [C API] Add PyTupleWriter API #161639
  • gh-161694: Check for integer overflow in PyObject_GC_Resize() #161908
  • gh-161694: Check for integer overflow in _PyObject_VAR_SIZE() #161911

Activity

  1. vstinner
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-C-API
    on Nov 9, 2026
  2. vstinner commented on Nov 9, 2026

    @vstinner
    vstinner
    on Nov 9, 2026
    MemberAuthor
    More actions

    See also #161635 "Add PyList_AsTupleAndClear() function".

  3. encukou commented on Nov 10, 2026

    @encukou
    encukou
    on Nov 10, 2026
    Member
    More actions

    How common is this?
    Tuples are usually a small (and fixed size, or a fixed upper bound on the size); for that you should arrange them on the stack and call PyTuple_FromArray. (Adding a PyTuple_FromArray_Steal might help.)

    For tuples that aren't small, does overhead of e.g. converting from a list (or listwriter) really matter?

  4. vstinner
    added 5 commits that reference this issue on Nov 12, 2026

    pythongh-161694: Check for integer overflow in PyObject_GC_Resize()

    97510e4

    pythongh-161694: Check for integer overflow in PyObject_GC_Resize()

    8477da1

    pythongh-161694: Check for integer overflow in PyObject_GC_Resize()

    e494234

    pythongh-161694: Check for integer overflow in _PyObject_VAR_SIZE()

    3ac5d7b

    pythongh-161694: Check for integer overflow in _PyObject_VAR_SIZE()

    1cd073f
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.