Skip to content
Closed
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 PyTuple_Make[Single,Pair,Triplet]
  • Loading branch information
sergey-miryanov committed Oct 13, 2025
commit d4cf7551757dafac4027ba40f9f3850d11fadcea
5 changes: 5 additions & 0 deletions Include/tupleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ 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_MakeSingle(PyObject *one);
PyAPI_FUNC(PyObject *) PyTuple_MakePair(PyObject *one, PyObject *two);
PyAPI_FUNC(PyObject *) PyTuple_MakeTriplet(PyObject *one, PyObject *two, PyObject *three);


#ifndef Py_LIMITED_API
# define Py_CPYTHON_TUPLEOBJECT_H
# include "cpython/tupleobject.h"
Expand Down
4 changes: 2 additions & 2 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ defdict_missing(PyObject *op, PyObject *key)
if (factory == NULL || factory == Py_None) {
/* XXX Call dict.__missing__(key) */
PyObject *tup;
tup = PyTuple_Pack(1, key);
tup = PyTuple_MakeSingle(key);
if (!tup) return NULL;
PyErr_SetObject(PyExc_KeyError, tup);
Py_DECREF(tup);
Expand Down Expand Up @@ -2292,7 +2292,7 @@ defdict_reduce(PyObject *op, PyObject *Py_UNUSED(dummy))
if (dd->default_factory == NULL || dd->default_factory == Py_None)
args = PyTuple_New(0);
else
args = PyTuple_Pack(1, dd->default_factory);
args = PyTuple_MakeSingle(dd->default_factory);
if (args == NULL)
return NULL;
items = PyObject_CallMethodNoArgs(op, &_Py_ID(items));
Expand Down
2 changes: 1 addition & 1 deletion Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ csv_exec(PyObject *module) {
}

/* Add the CSV exception object to the module. */
PyObject *bases = PyTuple_Pack(1, PyExc_Exception);
PyObject *bases = PyTuple_MakeSingle(PyExc_Exception);
if (bases == NULL) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3510,7 +3510,7 @@ _PyCData_set(ctypes_state *st,
only it's object list. So we create a tuple, containing
b_objects list PLUS the array itself, and return that!
*/
return PyTuple_Pack(2, keep, value);
return PyTuple_MakePair(keep, value);
}
PyErr_Format(PyExc_TypeError,
"incompatible types, %s instance instead of %s instance",
Expand Down Expand Up @@ -5330,7 +5330,7 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
len = PyLong_FromSsize_t(length);
if (len == NULL)
return NULL;
key = PyTuple_Pack(2, itemtype, len);
key = PyTuple_MakePair(itemtype, len);
Py_DECREF(len);
if (!key)
return NULL;
Expand Down
5 changes: 1 addition & 4 deletions Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
if (!layout_func) {
goto error;
}
kwnames = PyTuple_Pack(
2,
&_Py_ID(is_struct),
&_Py_ID(base));
kwnames = PyTuple_MakePair(&_Py_ID(is_struct), &_Py_ID(base));
if (!kwnames) {
goto error;
}
Expand Down
14 changes: 7 additions & 7 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ delta_divmod(PyObject *left, PyObject *right)
Py_DECREF(divmod);
return NULL;
}
result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta);
result = PyTuple_MakePair(PyTuple_GET_ITEM(divmod, 0), delta);
Py_DECREF(delta);
Py_DECREF(divmod);
return result;
Expand Down Expand Up @@ -4478,8 +4478,8 @@ timezone_getinitargs(PyObject *op, PyObject *Py_UNUSED(dummy))
{
PyDateTime_TimeZone *self = PyTimeZone_CAST(op);
if (self->name == NULL)
return PyTuple_Pack(1, self->offset);
return PyTuple_Pack(2, self->offset, self->name);
return PyTuple_MakeSingle(self->offset);
return PyTuple_MakePair(self->offset, self->name);
}

static PyMethodDef timezone_methods[] = {
Expand Down Expand Up @@ -5228,9 +5228,9 @@ time_getstate(PyDateTime_Time *self, int proto)
/* Set the first bit of the first byte */
PyBytes_AS_STRING(basestate)[0] |= (1 << 7);
if (! HASTZINFO(self) || self->tzinfo == Py_None)
result = PyTuple_Pack(1, basestate);
result = PyTuple_MakeSingle(basestate);
else
result = PyTuple_Pack(2, basestate, self->tzinfo);
result = PyTuple_MakePair(basestate, self->tzinfo);
Py_DECREF(basestate);
}
return result;
Expand Down Expand Up @@ -7165,9 +7165,9 @@ datetime_getstate(PyDateTime_DateTime *self, int proto)
/* Set the first bit of the third byte */
PyBytes_AS_STRING(basestate)[2] |= (1 << 7);
if (! HASTZINFO(self) || self->tzinfo == Py_None)
result = PyTuple_Pack(1, basestate);
result = PyTuple_MakeSingle(basestate);
else
result = PyTuple_Pack(2, basestate, self->tzinfo);
result = PyTuple_MakePair(basestate, self->tzinfo);
Py_DECREF(basestate);
}
return result;
Expand Down
28 changes: 14 additions & 14 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4058,7 +4058,7 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
}
}

result = PyTuple_Pack(2, numerator, denominator);
result = PyTuple_MakePair(numerator, denominator);


error:
Expand Down Expand Up @@ -4638,7 +4638,7 @@ nm_mpd_qdivmod(PyObject *v, PyObject *w)
return NULL;
}

ret = PyTuple_Pack(2, q, r);
ret = PyTuple_MakePair(q, r);
Py_DECREF(r);
Py_DECREF(q);
return ret;
Expand Down Expand Up @@ -6689,7 +6689,7 @@ _decimal_Context_divmod_impl(PyObject *context, PyObject *x, PyObject *y)
return NULL;
}

ret = PyTuple_Pack(2, q, r);
ret = PyTuple_MakePair(q, r);
Py_DECREF(r);
Py_DECREF(q);
return ret;
Expand Down Expand Up @@ -7694,23 +7694,23 @@ _decimal_exec(PyObject *m)

switch (cm->flag) {
case MPD_Float_operation:
base = PyTuple_Pack(2, state->DecimalException, PyExc_TypeError);
base = PyTuple_MakePair(state->DecimalException, PyExc_TypeError);
break;
case MPD_Division_by_zero:
base = PyTuple_Pack(2, state->DecimalException,
PyExc_ZeroDivisionError);
base = PyTuple_MakePair(state->DecimalException,
PyExc_ZeroDivisionError);
break;
case MPD_Overflow:
base = PyTuple_Pack(2, state->signal_map[INEXACT].ex,
state->signal_map[ROUNDED].ex);
base = PyTuple_MakePair(state->signal_map[INEXACT].ex,
state->signal_map[ROUNDED].ex);
break;
case MPD_Underflow:
base = PyTuple_Pack(3, state->signal_map[INEXACT].ex,
state->signal_map[ROUNDED].ex,
state->signal_map[SUBNORMAL].ex);
base = PyTuple_MakeTriplet(state->signal_map[INEXACT].ex,
state->signal_map[ROUNDED].ex,
state->signal_map[SUBNORMAL].ex);
break;
default:
base = PyTuple_Pack(1, state->DecimalException);
base = PyTuple_MakeSingle(state->DecimalException);
break;
}

Expand Down Expand Up @@ -7741,10 +7741,10 @@ _decimal_exec(PyObject *m)
for (cm = state->cond_map+1; cm->name != NULL; cm++) {
PyObject *base;
if (cm->flag == MPD_Division_undefined) {
base = PyTuple_Pack(2, state->signal_map[0].ex, PyExc_ZeroDivisionError);
base = PyTuple_MakePair(state->signal_map[0].ex, PyExc_ZeroDivisionError);
}
else {
base = PyTuple_Pack(1, state->signal_map[0].ex);
base = PyTuple_MakeSingle(state->signal_map[0].ex);
}
if (base == NULL) {
goto error; /* GCOV_NOT_REACHED */
Expand Down
8 changes: 4 additions & 4 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
return NULL;
}

old = PyTuple_Pack(2,
old = PyTuple_MakePair(
st->comment_factory ? st->comment_factory : Py_None,
st->pi_factory ? st->pi_factory : Py_None);

Expand Down Expand Up @@ -2695,7 +2695,7 @@ treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
{
if (action != NULL) {
PyObject *res;
PyObject *event = PyTuple_Pack(2, action, node);
PyObject *event = PyTuple_MakePair(action, node);
if (event == NULL)
return -1;
res = PyObject_CallOneArg(self->events_append, event);
Expand Down Expand Up @@ -2912,7 +2912,7 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
Py_XSETREF(self->last_for_tail, Py_NewRef(pi));
}
} else {
pi = PyTuple_Pack(2, target, text);
pi = PyTuple_MakePair(target, text);
if (!pi) {
return NULL;
}
Expand All @@ -2936,7 +2936,7 @@ treebuilder_handle_start_ns(TreeBuilderObject* self, PyObject* prefix, PyObject*
PyObject* parcel;

if (self->events_append && self->start_ns_event_obj) {
parcel = PyTuple_Pack(2, prefix, uri);
parcel = PyTuple_MakePair(prefix, uri);
if (!parcel) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ get_summary(PyInterpreterState *interp)
Py_DECREF(idobj);
return NULL;
}
PyObject *res = PyTuple_Pack(2, idobj, whenceobj);
PyObject *res = PyTuple_MakePair(idobj, whenceobj);
Py_DECREF(idobj);
Py_DECREF(whenceobj);
return res;
Expand Down
4 changes: 2 additions & 2 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ss
goto bail;

if (has_pairs_hook) {
PyObject *item = PyTuple_Pack(2, key, val);
PyObject *item = PyTuple_MakePair(key, val);
if (item == NULL)
goto bail;
Py_CLEAR(key);
Expand Down Expand Up @@ -1491,7 +1491,7 @@ encoder_call(PyObject *op, PyObject *args, PyObject *kwds)
if (str == NULL) {
return NULL;
}
PyObject *result = PyTuple_Pack(1, str);
PyObject *result = PyTuple_MakeSingle(str);
Py_DECREF(str);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ itemgetter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy))
itemgetterobject *ig = itemgetterobject_CAST(op);
if (ig->nitems == 1)
return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item);
return PyTuple_Pack(2, Py_TYPE(ig), ig->item);
return PyTuple_MakePair((PyObject *)Py_TYPE(ig), ig->item);
}

PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
Expand Down
6 changes: 3 additions & 3 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3609,7 +3609,7 @@ fix_imports(PickleState *st, PyObject **module_name, PyObject **global_name)
PyObject *key;
PyObject *item;

key = PyTuple_Pack(2, *module_name, *global_name);
key = PyTuple_MakePair(*module_name, *global_name);
if (key == NULL)
return -1;
item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Expand Down Expand Up @@ -3706,7 +3706,7 @@ save_global(PickleState *st, PicklerObject *self, PyObject *obj,
char pdata[5];
Py_ssize_t n;

extension_key = PyTuple_Pack(2, module_name, global_name);
extension_key = PyTuple_MakePair(module_name, global_name);
if (extension_key == NULL) {
goto error;
}
Expand Down Expand Up @@ -7123,7 +7123,7 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyTypeObject *cls,

/* Check if the global (i.e., a function or a class) was renamed
or moved to another module. */
key = PyTuple_Pack(2, module_name, global_name);
key = PyTuple_MakePair(module_name, global_name);
if (key == NULL)
return NULL;
item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_sqlite/microprotocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pysqlite_microprotocols_add(pysqlite_state *state, PyTypeObject *type,

assert(type != NULL);
assert(proto != NULL);
key = PyTuple_Pack(2, (PyObject *)type, proto);
key = PyTuple_MakePair((PyObject *)type, proto);
if (!key) {
return -1;
}
Expand All @@ -81,7 +81,7 @@ pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
way to get a quotable object to be its instance */

/* look for an adapter in the registry */
key = PyTuple_Pack(2, (PyObject *)Py_TYPE(obj), proto);
key = PyTuple_MakePair((PyObject *)Py_TYPE(obj), proto);
if (!key) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6797,7 +6797,7 @@ do { \
}

/* ssl.CertificateError used to be a subclass of ValueError */
bases = PyTuple_Pack(2, state->PySSLErrorObject, PyExc_ValueError);
bases = PyTuple_MakePair(state->PySSLErrorObject, PyExc_ValueError);
if (bases == NULL) {
goto error;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ init_ndbuf(PyObject *items, PyObject *shape, PyObject *strides,

/* convert scalar to list */
if (ndim == 0) {
items = PyTuple_Pack(1, items);
items = PyTuple_MakeSingle(items);
if (items == NULL)
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/_testcapi/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ get_date_fromtimestamp(PyObject *self, PyObject *args)
}

// Construct the argument tuple
if ((tsargs = PyTuple_Pack(1, ts)) == NULL) {
if ((tsargs = PyTuple_MakeSingle(ts)) == NULL) {
return NULL;
}

Expand Down Expand Up @@ -383,10 +383,10 @@ get_datetime_fromtimestamp(PyObject *self, PyObject *args)

// Construct the argument tuple
if (usetz) {
tsargs = PyTuple_Pack(2, ts, tzinfo);
tsargs = PyTuple_MakePair(ts, tzinfo);
}
else {
tsargs = PyTuple_Pack(1, ts);
tsargs = PyTuple_MakeSingle(ts);
}

if (tsargs == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ _testcapi_set_exc_info_impl(PyObject *module, PyObject *new_type,
Py_INCREF(new_tb);
PyErr_SetExcInfo(new_type, new_value, new_tb);

PyObject *orig_exc = PyTuple_Pack(3,
PyObject *orig_exc = PyTuple_MakeTriplet(
type ? type : Py_None,
value ? value : Py_None,
tb ? tb : Py_None);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test_w_code_invalid(PyObject *self, PyObject *arg)
PyObject *kwargs;
PyObject *tmp;

if (!(args = PyTuple_Pack(1, Py_None))) {
if (!(args = PyTuple_MakeSingle(Py_None))) {
return NULL;
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/_testcapi/heaptype.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ test_from_spec_invalid_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(
goto finally;
}

bases = PyTuple_Pack(2, class_a, class_b);
bases = PyTuple_MakePair(class_a, class_b);
if (bases == NULL) {
goto finally;
}
Expand Down Expand Up @@ -1356,7 +1356,7 @@ _PyTestCapi_Init_Heaptype(PyObject *m) {
if (HeapCType == NULL) {
return -1;
}
PyObject *subclass_bases = PyTuple_Pack(1, HeapCType);
PyObject *subclass_bases = PyTuple_MakeSingle(HeapCType);
Py_DECREF(HeapCType);
if (subclass_bases == NULL) {
return -1;
Expand Down Expand Up @@ -1395,7 +1395,7 @@ _PyTestCapi_Init_Heaptype(PyObject *m) {
PyObject *HeapCTypeVectorcall = PyType_FromSpec(&HeapCTypeVectorcall_spec);
ADD("HeapCTypeVectorcall", HeapCTypeVectorcall);

PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass);
PyObject *subclass_with_finalizer_bases = PyTuple_MakeSingle(HeapCTypeSubclass);
if (subclass_with_finalizer_bases == NULL) {
return -1;
}
Expand Down
Loading