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