Repository navigation
Add PyTuple_FromPair #145247
Description
Feature or enhancement
Proposal:
Rationale
We have had quite a few PRs lately suggesting replacing PyTuple_New (which requires manually setting the items) with PyTuple_FromArray.
Many of them have been rejected because of (maybe excessive) requirements for the authors to do benchmarking, even though the code is IMO a clear improvement. Almost all of these have been for pairs.
I propose adding PyTuple_FromPair to provide an idiomatic, fast and safe way to create 2-tuples.
We should also add a "steal" variant as well to simplify replacing PyTuple_New() ... PyTuple_SET_ITEM.
Note: I originally wanted to call this
PyTuple_MakePair, but I'm takingPyTuple_FromPairfrom capi-workgroup/decisions#84 (comment)
Here's all the relevant PRs I could easily find:
#144529
#144531
#144532
#144760
#144771
#144772
#144773
#144829
Prior abandoned issue:
#140052 which also proposes adding PyTuple_FromSingle and which links to capi-workgroup/decisions#84.
capi-workgroup/decisions#84 rejects the idea without any clear rationale.
Clarity
t = PyTuple_New(2);
// error handling here
// Care must be taken not to leak the invalid tuple
PyTuple_SET_ITEM(t, a);
PyTuple_SET_ITEM(t, b);compare with
t = PyTuple_FromPair(a, b);
// error handling herePerformance
Compared to PyTuple_New
PyTuple_FromPair can allocate directly from the free list without additional checks. It does not need to check for weird sizes, or special case size zero. It does not need to NULL out the items before they are set. It can determine whether the tuple needs to be tracked or not at construction without the overhead of tracking it, then later untracking it.
Compared to PyTuple_FromArray
The advantage is less than for PyTuple_New, but still significant, as it reduces memory traffic by not storing the items in an array, then reading them out again.
Safety
This has the same safety advantages as PyTuple_FromArray, as it never produces an incomplete tuple. Since much of the code it will replace is using PyTuple_New, this is a worthwhile improvement
Implementation
We should make this a private API to start with. Once it proves it use, then we can make it public.
PyObject *_PyTuple_MakePair(PyObject *a, PyObject *b);
PyObject *_PyTuple_MakePairSteal(PyObject *a, PyObject *b);Linked PRs
- GH-145247: Implement _PyTuple_FromPair #145325
- GH-145247: Use _PyTuple_FromPair in Parser and Python #145842
- GH-145247: Use _PyTuple_FromPair[Steal] in Objects #145884
- GH-145247: Use _PyTuple_FromPair in exceptions.c #145910
- [3.13] Fix possible memory leak in OrderedDict popitem (GH-145247) #146538
- [3.14] Fix possible memory leak in OrderedDict popitem (GH-145247) #146537
- GH-145247: Use _PyTuple_FromPair in Modules(1) #148000
- GH-145247: Use _PyTuple_FromPair in Modules(2) #148003
Activity
markshannon commentedon Feb 26, 2026
Minor correction:
Some of the above PR suggest replacing
PyTuple_Pack.In terms of clarity or safety there is no real improvement in using
PyTuple_FromPair, but there is a significant performance advantage;PyTuple_Packis quite cumbersomesergey-miryanov commentedon Feb 26, 2026
If we decide to add this, I would like to work on it, if no one minds.
markshannon commentedon Feb 26, 2026
Please, go ahead.
I think the only real debate is which, if any, of these functions should go in the public API.
If you make the functions private initially, then there should be no objection
If you do want to add
FromSingleandFromTriplet, could you do them in separate PRs later to avoid making the initial PR too big or contentious.vstinner commentedon Mar 4, 2026
The C API Working Group voted against adding a public C API to create 1-tuple or 2-tuple. So I suggest adding an internal C API instead.
sergey-miryanov commentedon Mar 5, 2026
Results from microbenchmarks, run under Ubuntu 24.04, CPU i5-11600K @ 3.90GHz.
When storing List to the tuple - List is gc-trackable, so tuple will be tracked too (microbenchmark):
When storing Long to the tuple - Long is not gc-trackable, so tuple will not be tracked (microbenchmark):
Script for run microbenchmarks
sergey-miryanov commentedon Mar 5, 2026
I have a branch where I replaced old construction of 2-tuples with new API. It touches a lot of modules. Should I create a separate PR for each module?
GH-145247: Implement _PyTuple_FromPair() (#145325)
vstinner commentedon Mar 10, 2026
I merged #145325 which implements _PyTuple_FromPair() and _PyTuple_FromPairSteal().
I suggest creating a first PR which modify a maximum of 10 files. If this change is merged, move on to the next batch.
sergey-miryanov commentedon Mar 10, 2026
Thanks!
GH-145247: Use _PyTuple_FromPair in Parser and Python (#145842)
5 remaining items
GH-145247: Use _PyTuple_FromPair[Steal] in Objects (#145884)
vstinner commentedon Mar 30, 2026
@sergey-miryanov: You can CC-me if you write more changes to use these new functions.
sergey-miryanov commentedon Mar 30, 2026
@vstinner Ok, thanks! Will prepare PRs this week.
GH-145247: Use _PyTuple_FromPair in Modules (part 1) (#148000)
GH-145247: Use _PyTuple_FromPair in Modules (part 2) (#148003)
pythonGH-145247: Use _PyTuple_FromPair[Steal] in Objects (python#145884)
pythonGH-145247: Implement _PyTuple_FromPair() (python#145325)
pythonGH-145247: Use _PyTuple_FromPair in Parser and Python (python#1…
pythonGH-145247: Use _PyTuple_FromPair in exceptions.c (pythonGH-145910)
pythonGH-145247: Use _PyTuple_FromPair[Steal] in Objects (python#145884)
pythonGH-145247: Use _PyTuple_FromPair in Modules (part 1) (python#14…
pythonGH-145247: Use _PyTuple_FromPair in Modules (part 2) (python#14…
StanFromIreland commentedon May 8, 2026
Triage: Is work still on-going or can this be closed?
sergey-miryanov commentedon May 8, 2026
I plan to review the codebase for additional use cases for
_PyTuple_FromPair, and evaluate whether adding_PyTuple_FromSingleand_PyTuple_FromTripletvariants makes sense.