From f2431b38e716c498dd20d57fd0fc679fd84f75ac Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 24 Apr 2024 00:26:04 +0200 Subject: [PATCH] Add PyTuple_Pack2 --- Include/tupleobject.h | 1 + Modules/itertoolsmodule.c | 2 +- Objects/tupleobject.c | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Include/tupleobject.h b/Include/tupleobject.h index 1f9ab54be65f87..b32e03d36667d4 100644 --- a/Include/tupleobject.h +++ b/Include/tupleobject.h @@ -33,6 +33,7 @@ PyAPI_FUNC(PyObject *) PyTuple_GetItem(PyObject *, Py_ssize_t); PyAPI_FUNC(int) PyTuple_SetItem(PyObject *, Py_ssize_t, PyObject *); PyAPI_FUNC(PyObject *) PyTuple_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t); PyAPI_FUNC(PyObject *) PyTuple_Pack(Py_ssize_t, ...); +PyAPI_FUNC(PyObject*) PyTuple_Pack2(PyObject* a, PyObject* b); #ifndef Py_LIMITED_API # define Py_CPYTHON_TUPLEOBJECT_H diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 6ee447ef6a8cd6..daf165f20efae1 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -356,7 +356,7 @@ pairwise_next(pairwiseobject *po) return NULL; } /* Future optimization: Reuse the result tuple as we do in enumerate() */ - result = PyTuple_Pack(2, old, new); + result = PyTuple_Pack2(old, new); Py_XSETREF(po->old, new); Py_DECREF(old); return result; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 5ae1ee9a89af84..63739bf4eba4f8 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -151,6 +151,21 @@ _PyTuple_MaybeUntrack(PyObject *op) _PyObject_GC_UNTRACK(op); } +PyObject* +PyTuple_Pack2(PyObject *a, PyObject *b) +{ + assert(a!=NULL); + assert(b != NULL); + PyTupleObject* result = tuple_alloc(2); + if (result == NULL) { + return NULL; + } + result->ob_item[0] = Py_NewRef(a); + result->ob_item[1] = Py_NewRef(b); + _PyObject_GC_TRACK(result); + return (PyObject*)result; +} + PyObject * PyTuple_Pack(Py_ssize_t n, ...) {