@@ -164,6 +164,9 @@ Tuple Objects
164164 .. versionchanged :: next
165165 The function now fails if the tuple hash value was already computed.
166166
167+ .. soft-deprecated :: next
168+ Use the :ref: `PyTupleWriter API <pytuplewriter >` instead.
169+
167170
168171.. _struct-sequence-objects :
169172
@@ -293,3 +296,101 @@ type.
293296
294297 .. versionchanged :: 3.13
295298 Now implemented as an alias to :c:func: `PyStructSequence_SetItem `.
299+
300+
301+ .. _pytuplewriter :
302+
303+ PyTupleWriter
304+ -------------
305+
306+ The ``PyTupleWriter `` API can be used to create a Python :class: `tuple ` object
307+ when the final size is not known in advance.
308+
309+ .. versionadded :: next
310+
311+ .. c :type :: PyTupleWriter
312+
313+ A tuple writer instance.
314+
315+ The instance must be destroyed by :c:func: `PyTupleWriter_Finish ` on
316+ success, or :c:func: `PyTupleWriter_Discard ` on error.
317+
318+ The API is **not thread safe **. To share a writer with multiple threads, a
319+ critical section or a lock is needed.
320+
321+ .. c :function :: PyTupleWriter* PyTupleWriter_Create (Py_ssize_t size)
322+
323+ Create a tuple writer instance.
324+
325+ *size* must be greater than or equal to ``0``.
326+
327+ If *size* is greater than ``0``, allocate an internal buffer of *size* items
328+ and all items must be set with :c:func:`PyTupleWriter_SetItem` or
329+ :c:func:`PyTupleWriter_SetItems`.
330+
331+ Set an exception and return ``NULL`` on error.
332+
333+ .. c:function:: PyObject* PyTupleWriter_Finish(PyTupleWriter *writer)
334+
335+ Return the final Python :class: `str ` object and destroy the writer instance.
336+
337+ Set an exception and return ``NULL `` on error.
338+
339+ The writer instance is invalid after this call.
340+
341+ .. c :function :: PyObject* PyTupleWriter_FinishWithSize (PyTupleWriter *writer, Py_ssize_t final_size)
342+
343+ Similar to :c:func: `PyTupleWriter_FinishWithSize `, but first truncates
344+ the tuple to *final_size * items.
345+
346+ On success, return a new tuple object.
347+ On error, set an exception and return ``NULL ``.
348+
349+ .. c :function :: void PyTupleWriter_Discard (PyTupleWriter *writer)
350+
351+ Discard the internal Tuple buffer and destroy the writer instance.
352+
353+ If *writer * is ``NULL ``, no operation is performed.
354+
355+ The writer instance is invalid after this call.
356+
357+ .. c :function :: void PyTupleWriter_Discard (PyTupleWriter *writer)
358+
359+ Discard the internal tuple buffer and destroy the writer instance.
360+
361+ If *writer * is ``NULL ``, no operation is performed.
362+
363+ The writer instance is invalid after this call.
364+
365+ .. c :function :: Py_ssize_t PyTupleWriter_GetSize (PyTupleWriter *writer)
366+
367+ Get the writer size.
368+
369+ The function cannot fail.
370+
371+ .. c :function :: int PyTupleWriter_SetItem (PyTupleWriter *writer, Py_ssize_t index, PyObject *item)
372+
373+ Set an item of a tuple writer start at position *index *.
374+ Copy *item * reference.
375+
376+ An item must be set exactly once.
377+
378+ On success, return ``0 ``.
379+ On error, set an exception and return ``-1 ``.
380+
381+ .. c :function :: int PyTupleWriter_SetItems (PyTupleWriter *writer, Py_ssize_t index, PyObject *const *items, Py_ssize_t nitem)
382+
383+ Set *nitem * items of a tuple writer start at position *index *.
384+ Copy *items * references.
385+
386+ Items must be set exactly once.
387+
388+ On success, return ``0 ``.
389+ On error, set an exception and return ``-1 ``.
390+
391+ .. c :function :: int PyTupleWriter_Resize (PyTupleWriter **writer, Py_ssize_t new_size)
392+
393+ Resize the tuple writer to *new_size * items.
394+
395+ On success, return ``0 ``.
396+ On error, set an exception and return ``-1 ``.
0 commit comments