Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add initial version of _PyTuple_FromPair
  • Loading branch information
sergey-miryanov committed Feb 27, 2026
commit f2d3ab87ac04b11ec63b8d33c7704b0497894ae9
3 changes: 3 additions & 0 deletions Include/internal/pycore_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ extern PyStatus _PyTuple_InitGlobalObjects(PyInterpreterState *);
PyAPI_FUNC(PyObject *)_PyTuple_FromStackRefStealOnSuccess(const union _PyStackRef *, Py_ssize_t);
PyAPI_FUNC(PyObject *)_PyTuple_FromArraySteal(PyObject *const *, Py_ssize_t);

PyAPI_FUNC(PyObject *)_PyTuple_FromPair(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *)_PyTuple_FromPairSteal(PyObject *, PyObject *);
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated

typedef struct {
PyObject_HEAD
Py_ssize_t it_index;
Expand Down
53 changes: 53 additions & 0 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,59 @@ PyTuple_Pack(Py_ssize_t n, ...)
return (PyObject *)result;
}

static PyTupleObject *
tuple_alloc_2()
{
Py_ssize_t size = 2;
Py_ssize_t index = size - 1;
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated
assert(index < PyTuple_MAXSAVESIZE);
PyTupleObject *result = _Py_FREELIST_POP(PyTupleObject, tuples[index]);
if (result == NULL) {
result = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
}
if (result != NULL) {
_PyTuple_RESET_HASH_CACHE(result);
}
return result;
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated
}

PyObject *
_PyTuple_FromPair(PyObject *one, PyObject *two)
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated
{
assert (one != NULL);
assert (two != NULL);
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated

PyTupleObject *op = tuple_alloc_2();
if (op == NULL) {
return NULL;
}
op->ob_item[0] = Py_NewRef(one);
op->ob_item[1] = Py_NewRef(two);
if (maybe_tracked(one) || maybe_tracked(two)) {
_PyObject_GC_TRACK(op);
}
return (PyObject *)op;
}

PyObject *
_PyTuple_FromPairSteal(PyObject *one, PyObject *two)
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated
{
assert (one != NULL);
assert (two != NULL);
Comment thread
sergey-miryanov marked this conversation as resolved.
Outdated

PyTupleObject *op = tuple_alloc_2();
if (op == NULL) {
Py_DECREF(one);
Py_DECREF(two);
return NULL;
}
op->ob_item[0] = one;
op->ob_item[1] = two;
if (maybe_tracked(one) || maybe_tracked(two)) {
_PyObject_GC_TRACK(op);
}
return (PyObject *)op;
}

/* Methods */

Expand Down