diff --git a/Include/tupleobject.h b/Include/tupleobject.h index 1f9ab54be65f87..c47966022a8721 100644 --- a/Include/tupleobject.h +++ b/Include/tupleobject.h @@ -34,6 +34,13 @@ 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_MakeSingleSteal(PyObject *one); +PyAPI_FUNC(PyObject *) PyTuple_MakePair(PyObject *one, PyObject *two); +PyAPI_FUNC(PyObject *) PyTuple_MakePairSteal(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" diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 99408e60721c60..f17cb6a563aa03 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -837,14 +837,10 @@ future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg, fut->fut_context0 = Py_NewRef(ctx); } else { - PyObject *tup = PyTuple_New(2); + PyObject *tup = PyTuple_MakePair(arg, (PyObject *)ctx); if (tup == NULL) { return NULL; } - Py_INCREF(arg); - PyTuple_SET_ITEM(tup, 0, arg); - Py_INCREF(ctx); - PyTuple_SET_ITEM(tup, 1, (PyObject *)ctx); if (fut->fut_callbacks != NULL) { int err = PyList_Append(fut->fut_callbacks, tup); @@ -1511,14 +1507,12 @@ _asyncio_Future__callbacks_get_impl(FutureObj *self) Py_ssize_t i = 0; if (self->fut_callback0 != NULL) { - PyObject *tup0 = PyTuple_New(2); + assert(self->fut_context0 != NULL); + PyObject *tup0 = PyTuple_MakePair(self->fut_callback0, self->fut_context0); if (tup0 == NULL) { Py_DECREF(callbacks); return NULL; } - PyTuple_SET_ITEM(tup0, 0, Py_NewRef(self->fut_callback0)); - assert(self->fut_context0 != NULL); - PyTuple_SET_ITEM(tup0, 1, Py_NewRef(self->fut_context0)); PyList_SET_ITEM(callbacks, i, tup0); i++; } diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 3ba48d5d9d3c64..01cb26ac4ce53f 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -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); @@ -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)); diff --git a/Modules/_csv.c b/Modules/_csv.c index 87be7a8f1fb136..50d84605d5649d 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -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; } diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 91fd23d413de21..80cb20bee35f54 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -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", @@ -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; diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index ab955a0b824a2f..d984c796754e5e 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -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; } diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 46c4f57984b0df..62eeda9191c132 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -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; @@ -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[] = { @@ -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; @@ -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; diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 04b6695f8af06a..7004914335eb58 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -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: @@ -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; @@ -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; @@ -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; } @@ -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 */ diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 9263f14b57f972..5e1f986b974961 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -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); @@ -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); @@ -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; } @@ -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; } diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c index 2aee8b07891c91..aac3b5c6679760 100644 --- a/Modules/_interpretersmodule.c +++ b/Modules/_interpretersmodule.c @@ -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; diff --git a/Modules/_json.c b/Modules/_json.c index 9a1fc3aba36116..670dd44fdbfba3 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -450,14 +450,7 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) { Py_DECREF(rval); return NULL; } - tpl = PyTuple_New(2); - if (tpl == NULL) { - Py_DECREF(pyidx); - Py_DECREF(rval); - return NULL; - } - PyTuple_SET_ITEM(tpl, 0, rval); - PyTuple_SET_ITEM(tpl, 1, pyidx); + tpl = PyTuple_MakePairSteal(rval, pyidx); return tpl; } @@ -826,7 +819,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); @@ -1491,7 +1484,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; } diff --git a/Modules/_operator.c b/Modules/_operator.c index 1cc05c39f5dbad..66610014b19305 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -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"); diff --git a/Modules/_pickle.c b/Modules/_pickle.c index bfb2830f3893d6..5da712fc80a3c4 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -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); @@ -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; } @@ -4958,20 +4958,11 @@ _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) if (contents == NULL) return NULL; - reduce_value = PyTuple_New(2); - if (reduce_value == NULL) { - Py_DECREF(contents); - return NULL; - } - dict_args = PyTuple_New(1); + dict_args = PyTuple_MakeSingleSteal(contents); if (dict_args == NULL) { - Py_DECREF(contents); - Py_DECREF(reduce_value); return NULL; } - PyTuple_SET_ITEM(dict_args, 0, contents); - PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type)); - PyTuple_SET_ITEM(reduce_value, 1, dict_args); + reduce_value = PyTuple_MakePairSteal(Py_NewRef(&PyDict_Type), dict_args); return reduce_value; } @@ -7123,7 +7114,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); @@ -7451,20 +7442,11 @@ _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) if (contents == NULL) return NULL; - reduce_value = PyTuple_New(2); - if (reduce_value == NULL) { - Py_DECREF(contents); - return NULL; - } - constructor_args = PyTuple_New(1); + constructor_args = PyTuple_MakeSingleSteal(contents); if (constructor_args == NULL) { - Py_DECREF(contents); - Py_DECREF(reduce_value); return NULL; } - PyTuple_SET_ITEM(constructor_args, 0, contents); - PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type)); - PyTuple_SET_ITEM(reduce_value, 1, constructor_args); + reduce_value = PyTuple_MakePairSteal(Py_NewRef(&PyDict_Type), constructor_args); return reduce_value; } diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index f77458d94a8573..47fc43c47a13a3 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -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; } @@ -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; } diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 4b75e455f402ff..3d4ef6b0d4f8c3 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -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; } diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c index d2e61e9d6acf24..f9de3d02b33d21 100644 --- a/Modules/_testbuffer.c +++ b/Modules/_testbuffer.c @@ -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; } diff --git a/Modules/_testcapi/datetime.c b/Modules/_testcapi/datetime.c index b800f9b8eb3473..0452164c555e3f 100644 --- a/Modules/_testcapi/datetime.c +++ b/Modules/_testcapi/datetime.c @@ -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; } @@ -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) { diff --git a/Modules/_testcapi/exceptions.c b/Modules/_testcapi/exceptions.c index c0254e044bc2d5..db1f7aced0b623 100644 --- a/Modules/_testcapi/exceptions.c +++ b/Modules/_testcapi/exceptions.c @@ -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); diff --git a/Modules/_testcapi/getargs.c b/Modules/_testcapi/getargs.c index ee04c760d27213..9785d08fa83f70 100644 --- a/Modules/_testcapi/getargs.c +++ b/Modules/_testcapi/getargs.c @@ -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; } diff --git a/Modules/_testcapi/heaptype.c b/Modules/_testcapi/heaptype.c index 69dcf072da1815..f7ae68f33aaf7f 100644 --- a/Modules/_testcapi/heaptype.c +++ b/Modules/_testcapi/heaptype.c @@ -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; } @@ -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; @@ -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; } diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 4e73be20e1b709..e461cddf0448cf 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -1840,7 +1840,7 @@ get_basic_static_type(PyObject *self, PyObject *args) PyTypeObject *cls = &BasicStaticTypes[num_basic_static_types_used++]; if (base != NULL) { - cls->tp_bases = PyTuple_Pack(1, base); + cls->tp_bases = PyTuple_MakeSingle(base); if (cls->tp_bases == NULL) { return NULL; } @@ -2112,7 +2112,7 @@ record_func(PyObject *obj, PyFrameObject *f, int what, PyObject *arg) if (line_obj == NULL) { goto error; } - tuple = PyTuple_Pack(3, what_obj, line_obj, arg); + tuple = PyTuple_MakeTriplet(what_obj, line_obj, arg); if (tuple == NULL) { goto error; } @@ -2770,7 +2770,7 @@ typedef struct { static PyObject * ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod) { - return PyTuple_Pack(2, other, mod); + return PyTuple_MakePair(other, mod); } static PyNumberMethods ipowType_as_number = { @@ -2968,7 +2968,7 @@ static PyObject * generic_alias_mro_entries(PyObject *op, PyObject *Py_UNUSED(bases)) { PyGenericAliasObject *self = (PyGenericAliasObject*)op; - return PyTuple_Pack(1, self->item); + return PyTuple_MakeSingle(self->item); } static PyMethodDef generic_alias_methods[] = { diff --git a/Modules/_testclinic.c b/Modules/_testclinic.c index 890f2201b46bc0..9ef91b40121c7f 100644 --- a/Modules/_testclinic.c +++ b/Modules/_testclinic.c @@ -1430,7 +1430,7 @@ _testclinic_TestClass_get_defining_class_arg_impl(PyObject *self, PyObject *arg) /*[clinic end generated code: output=fe7e49d96cbb7718 input=d1b83d3b853af6d9]*/ { - return PyTuple_Pack(2, cls, arg); + return PyTuple_MakePair((PyObject *)cls, arg); } /*[clinic input] @@ -1444,7 +1444,7 @@ _testclinic_TestClass_defclass_varpos_impl(PyObject *self, PyTypeObject *cls, PyObject *args) /*[clinic end generated code: output=fad33f2d3a8d778d input=332043286e393d38]*/ { - return PyTuple_Pack(2, cls, args); + return PyTuple_MakePair((PyObject *)cls, args); } /*[clinic input] diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index f0882191d3c3e8..098a2cde141aa7 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -1428,7 +1428,7 @@ Tkapp_CallProc(Tcl_Event *evPtr, int flags) Tcl_Size objc; int i; ENTER_PYTHON - if (e->self->trace && !Tkapp_Trace(e->self, PyTuple_Pack(1, e->args))) { + if (e->self->trace && !Tkapp_Trace(e->self, PyTuple_MakeSingle(e->args))) { objv = NULL; } else { diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index d97cf7af767ca3..fcfa7d3a372c8a 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1379,26 +1379,20 @@ static PyObject * array_array_buffer_info_impl(arrayobject *self) /*[clinic end generated code: output=9b2a4ec3ae7e98e7 input=63d9ad83ba60cda8]*/ { - PyObject *retval = NULL, *v; + PyObject *retval = NULL; - retval = PyTuple_New(2); - if (!retval) - return NULL; - - v = PyLong_FromVoidPtr(self->ob_item); - if (v == NULL) { - Py_DECREF(retval); + PyObject *one = PyLong_FromVoidPtr(self->ob_item); + if (one == NULL) { return NULL; } - PyTuple_SET_ITEM(retval, 0, v); - v = PyLong_FromSsize_t(Py_SIZE(self)); - if (v == NULL) { - Py_DECREF(retval); + PyObject *two = PyLong_FromSsize_t(Py_SIZE(self)); + if (two == NULL) { + Py_DECREF(one); return NULL; } - PyTuple_SET_ITEM(retval, 1, v); + retval = PyTuple_MakePairSteal(one, two); return retval; } @@ -2034,13 +2028,10 @@ make_array(PyTypeObject *arraytype, char typecode, PyObject *items) if (typecode_obj == NULL) return NULL; - new_args = PyTuple_New(2); + new_args = PyTuple_MakePairSteal(typecode_obj, Py_NewRef(items)); if (new_args == NULL) { - Py_DECREF(typecode_obj); return NULL; } - PyTuple_SET_ITEM(new_args, 0, typecode_obj); - PyTuple_SET_ITEM(new_args, 1, Py_NewRef(items)); array_obj = array_new(arraytype, new_args, NULL); Py_DECREF(new_args); diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c index 4b068967a6ca6e..4055982d8151ec 100644 --- a/Modules/atexitmodule.c +++ b/Modules/atexitmodule.c @@ -190,7 +190,7 @@ atexit_register(PyObject *module, PyObject *args, PyObject *kwargs) { func_kwargs = Py_None; } - PyObject *callback = PyTuple_Pack(3, func, func_args, func_kwargs); + PyObject *callback = PyTuple_MakeTriplet(func, func_args, func_kwargs); if (callback == NULL) { return NULL; diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 60ef6f9ff4cd98..33188640a6f67b 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -310,7 +310,7 @@ pairwise_new_impl(PyTypeObject *type, PyObject *iterable) } po->it = it; po->old = NULL; - po->result = PyTuple_Pack(2, Py_None, Py_None); + po->result = PyTuple_MakePair(Py_None, Py_None); if (po->result == NULL) { Py_DECREF(po); return NULL; @@ -389,11 +389,7 @@ pairwise_next(PyObject *op) _PyTuple_Recycle(result); } else { - result = PyTuple_New(2); - if (result != NULL) { - PyTuple_SET_ITEM(result, 0, Py_NewRef(old)); - PyTuple_SET_ITEM(result, 1, Py_NewRef(new)); - } + result = PyTuple_MakePair(old, new); } Py_XSETREF(po->old, new); @@ -563,7 +559,7 @@ groupby_next(PyObject *op) if (grouper == NULL) return NULL; - r = PyTuple_Pack(2, gbo->currkey, grouper); + r = PyTuple_MakePair(gbo->currkey, grouper); Py_DECREF(grouper); return r; } diff --git a/Modules/main.c b/Modules/main.c index 74e48c94732565..8793467910be1b 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -342,7 +342,7 @@ pymain_run_module(const wchar_t *modname, int set_argv0) Py_DECREF(runmodule); return pymain_exit_err_print(); } - runargs = PyTuple_Pack(2, module, set_argv0 ? Py_True : Py_False); + runargs = PyTuple_MakePair(module, set_argv0 ? Py_True : Py_False); if (runargs == NULL) { fprintf(stderr, "Could not create arguments for runpy._run_module_as_main\n"); diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 29b7b356648a53..7b24f954179192 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -964,18 +964,12 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait) } // The result is a two item tuple: (message, address) - self->read_from.result = PyTuple_New(2); + // first item: message + // second item: address + self->read_from.result = PyTuple_MakePairSteal(Py_NewRef(self->read_from.allocated_buffer), addr); if (self->read_from.result == NULL) { - Py_CLEAR(addr); return NULL; } - - // first item: message - PyTuple_SET_ITEM(self->read_from.result, 0, - Py_NewRef(self->read_from.allocated_buffer)); - // second item: address - PyTuple_SET_ITEM(self->read_from.result, 1, addr); - return Py_NewRef(self->read_from.result); case TYPE_READ_FROM_INTO: // unparse the address @@ -987,18 +981,12 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait) } // The result is a two item tuple: (number of bytes read, address) - self->read_from_into.result = PyTuple_New(2); + // first item: number of bytes read + // second item: address + self->read_from_into.result = PyTuple_MakePairSteal(PyLong_FromUnsignedLong((unsigned long)transferred), addr); if (self->read_from_into.result == NULL) { - Py_CLEAR(addr); return NULL; } - - // first item: number of bytes read - PyTuple_SET_ITEM(self->read_from_into.result, 0, - PyLong_FromUnsignedLong((unsigned long)transferred)); - // second item: address - PyTuple_SET_ITEM(self->read_from_into.result, 1, addr); - return Py_NewRef(self->read_from_into.result); default: return PyLong_FromUnsignedLong((unsigned long) transferred); diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 38ddc3ec4ffc3d..51d5c248f821db 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -5675,7 +5675,7 @@ os__path_splitroot_ex_impl(PyObject *module, path_t *path) goto exit; } } - result = PyTuple_Pack(3, drv, root, tail); + result = PyTuple_MakeTriplet(drv, root, tail); exit: Py_XDECREF(drv); Py_XDECREF(root); @@ -10832,7 +10832,7 @@ build_itimerspec(const struct itimerspec* curr_value) Py_DECREF(value); return NULL; } - PyObject *tuple = PyTuple_Pack(2, value, interval); + PyObject *tuple = PyTuple_MakePair(value, interval); Py_DECREF(interval); Py_DECREF(value); return tuple; diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 19fe509ec5e32a..642d64b55851a2 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -408,7 +408,7 @@ select_select_impl(PyObject *module, PyObject *rlist, PyObject *wlist, if (PyErr_Occurred()) ret = NULL; else - ret = PyTuple_Pack(3, rlist, wlist, xlist); + ret = PyTuple_MakeTriplet(rlist, wlist, xlist); Py_XDECREF(rlist); Py_XDECREF(wlist); @@ -1045,7 +1045,7 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj) Py_XDECREF(num2); goto error; } - value = PyTuple_Pack(2, num1, num2); + value = PyTuple_MakePair(num1, num2); Py_DECREF(num1); Py_DECREF(num2); if (value == NULL) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index dd4b6892977484..3b3fb2ddd37bd9 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3123,7 +3123,7 @@ sock_accept(PyObject *self, PyObject *Py_UNUSED(ignored)) if (addr == NULL) goto finally; - res = PyTuple_Pack(2, sock, addr); + res = PyTuple_MakePair(sock, addr); finally: Py_XDECREF(sock); @@ -4191,7 +4191,7 @@ sock_recvfrom(PyObject *self, PyObject *args) goto finally; } - ret = PyTuple_Pack(2, buf, addr); + ret = PyTuple_MakePair(buf, addr); Py_DECREF(buf); finally: @@ -6589,7 +6589,7 @@ socket_socketpair(PyObject *self, PyObject *args) s1 = new_sockobject(state, sv[1], family, type, proto); if (s1 == NULL) goto finally; - res = PyTuple_Pack(2, s0, s1); + res = PyTuple_MakePair((PyObject *)s0, (PyObject *)s1); finally: if (res == NULL) { diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0d264a6e346f95..9fd43481170cd4 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -2948,7 +2948,7 @@ _PyCode_ConstantKey(PyObject *op) else if (PyBool_Check(op) || PyBytes_CheckExact(op)) { /* Make booleans different from integers 0 and 1. * Avoid BytesWarning from comparing bytes with strings. */ - key = PyTuple_Pack(2, Py_TYPE(op), op); + key = PyTuple_MakePair((PyObject *)Py_TYPE(op), op); } else if (PyFloat_CheckExact(op)) { double d = PyFloat_AS_DOUBLE(op); @@ -2956,9 +2956,9 @@ _PyCode_ConstantKey(PyObject *op) * or -0.0 case from all others, just to avoid the "coercion". */ if (d == 0.0 && copysign(1.0, d) < 0.0) - key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); + key = PyTuple_MakeTriplet((PyObject *)Py_TYPE(op), op, Py_None); else - key = PyTuple_Pack(2, Py_TYPE(op), op); + key = PyTuple_MakePair((PyObject *)Py_TYPE(op), op); } else if (PyComplex_CheckExact(op)) { Py_complex z; @@ -2973,16 +2973,16 @@ _PyCode_ConstantKey(PyObject *op) /* use True, False and None singleton as tags for the real and imag * sign, to make tuples different */ if (real_negzero && imag_negzero) { - key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); + key = PyTuple_MakeTriplet((PyObject *)Py_TYPE(op), op, Py_True); } else if (imag_negzero) { - key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); + key = PyTuple_MakeTriplet((PyObject *)Py_TYPE(op), op, Py_False); } else if (real_negzero) { - key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); + key = PyTuple_MakeTriplet((PyObject *)Py_TYPE(op), op, Py_None); } else { - key = PyTuple_Pack(2, Py_TYPE(op), op); + key = PyTuple_MakePair((PyObject *)Py_TYPE(op), op); } } else if (PyTuple_CheckExact(op)) { @@ -3007,7 +3007,7 @@ _PyCode_ConstantKey(PyObject *op) PyTuple_SET_ITEM(tuple, i, item_key); } - key = PyTuple_Pack(2, tuple, op); + key = PyTuple_MakePair(tuple, op); Py_DECREF(tuple); } else if (PyFrozenSet_CheckExact(op)) { @@ -3041,7 +3041,7 @@ _PyCode_ConstantKey(PyObject *op) if (set == NULL) return NULL; - key = PyTuple_Pack(2, set, op); + key = PyTuple_MakePair(set, op); Py_DECREF(set); return key; } @@ -3072,7 +3072,7 @@ _PyCode_ConstantKey(PyObject *op) goto slice_exit; } - key = PyTuple_Pack(2, slice_key, op); + key = PyTuple_MakePair(slice_key, op); Py_DECREF(slice_key); slice_exit: Py_XDECREF(start_key); @@ -3086,7 +3086,7 @@ _PyCode_ConstantKey(PyObject *op) if (obj_id == NULL) return NULL; - key = PyTuple_Pack(2, obj_id, op); + key = PyTuple_MakePair(obj_id, op); Py_DECREF(obj_id); } return key; diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 24188ffe7132d5..582360f2bd0ef0 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -5138,7 +5138,7 @@ dictiter_new(PyDictObject *dict, PyTypeObject *itertype) } if (itertype == &PyDictIterItem_Type || itertype == &PyDictRevIterItem_Type) { - di->di_result = PyTuple_Pack(2, Py_None, Py_None); + di->di_result = PyTuple_MakePair(Py_None, Py_None); if (di->di_result == NULL) { Py_DECREF(di); return NULL; @@ -5716,11 +5716,9 @@ dictiter_iternextitem(PyObject *self) _PyTuple_Recycle(result); } else { - result = PyTuple_New(2); + result = PyTuple_MakePairSteal(key, value); if (result == NULL) return NULL; - PyTuple_SET_ITEM(result, 0, key); - PyTuple_SET_ITEM(result, 1, value); } return result; } @@ -5839,12 +5837,10 @@ dictreviter_iter_lock_held(PyDictObject *d, PyObject *self) _PyTuple_Recycle(result); } else { - result = PyTuple_New(2); + result = PyTuple_MakePair(key, value); if (result == NULL) { return NULL; } - PyTuple_SET_ITEM(result, 0, Py_NewRef(key)); - PyTuple_SET_ITEM(result, 1, Py_NewRef(value)); } return result; } @@ -6348,7 +6344,7 @@ dictitems_xor_lock_held(PyObject *d1, PyObject *d2) } } else { - PyObject *pair = PyTuple_Pack(2, key, val2); + PyObject *pair = PyTuple_MakePair(key, val2); if (pair == NULL) { goto error; } diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 814ce4f919514b..2c8b6d69d5a84d 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -78,7 +78,7 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start) Py_DECREF(en); return NULL; } - en->en_result = PyTuple_Pack(2, Py_None, Py_None); + en->en_result = PyTuple_MakePair(Py_None, Py_None); if (en->en_result == NULL) { Py_DECREF(en); return NULL; @@ -224,14 +224,7 @@ enum_next_long(enumobject *en, PyObject* next_item) _PyTuple_Recycle(result); return result; } - result = PyTuple_New(2); - if (result == NULL) { - Py_DECREF(next_index); - Py_DECREF(next_item); - return NULL; - } - PyTuple_SET_ITEM(result, 0, next_index); - PyTuple_SET_ITEM(result, 1, next_item); + result = PyTuple_MakePairSteal(next_index, next_item); return result; } @@ -274,14 +267,7 @@ enum_next(PyObject *op) _PyTuple_Recycle(result); return result; } - result = PyTuple_New(2); - if (result == NULL) { - Py_DECREF(next_index); - Py_DECREF(next_item); - return NULL; - } - PyTuple_SET_ITEM(result, 0, next_index); - PyTuple_SET_ITEM(result, 1, next_item); + result = PyTuple_MakePairSteal(next_index, next_item); return result; } diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 244d8f39e2bae5..53356047064adf 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -220,9 +220,9 @@ BaseException___reduce___impl(PyBaseExceptionObject *self) /*[clinic end generated code: output=af87c1247ef98748 input=283be5a10d9c964f]*/ { if (self->args && self->dict) - return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict); + return PyTuple_MakeTriplet((PyObject *)Py_TYPE(self), self->args, self->dict); else - return PyTuple_Pack(2, Py_TYPE(self), self->args); + return PyTuple_MakePair((PyObject *)Py_TYPE(self), self->args); } /* @@ -1001,7 +1001,7 @@ _PyExc_CreateExceptionGroup(const char *msg_str, PyObject *excs) if (!msg) { return NULL; } - PyObject *args = PyTuple_Pack(2, msg, excs); + PyObject *args = PyTuple_MakePair(msg, excs); Py_DECREF(msg); if (!args) { return NULL; @@ -1075,7 +1075,7 @@ BaseExceptionGroup_derive_impl(PyBaseExceptionGroupObject *self, PyObject *excs) /*[clinic end generated code: output=4307564218dfbf06 input=f72009d38e98cec1]*/ { - PyObject *init_args = PyTuple_Pack(2, self->msg, excs); + PyObject *init_args = PyTuple_MakePair(self->msg, excs); if (!init_args) { return NULL; } @@ -1392,8 +1392,7 @@ BaseExceptionGroup_split_impl(PyBaseExceptionGroupObject *self, return NULL; } - PyObject *result = PyTuple_Pack( - 2, + PyObject *result = PyTuple_MakePair( split_result.match ? split_result.match : Py_None, split_result.rest ? split_result.rest : Py_None); @@ -1707,8 +1706,8 @@ static PyObject* create_exception_group_class(void) { struct _Py_exc_state *state = get_exc_state(); - PyObject *bases = PyTuple_Pack( - 2, PyExc_BaseExceptionGroup, PyExc_Exception); + PyObject *bases = PyTuple_MakePair( + PyExc_BaseExceptionGroup, PyExc_Exception); if (bases == NULL) { return NULL; } @@ -1856,9 +1855,9 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) return NULL; PyBaseExceptionObject *exc = PyBaseExceptionObject_CAST(self); if (state == Py_None) - res = PyTuple_Pack(2, Py_TYPE(self), exc->args); + res = PyTuple_MakePair((PyObject *)Py_TYPE(self), exc->args); else - res = PyTuple_Pack(3, Py_TYPE(self), exc->args, state); + res = PyTuple_MakeTriplet((PyObject *)Py_TYPE(self), exc->args, state); Py_DECREF(state); return res; } @@ -2356,9 +2355,9 @@ OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) Py_INCREF(args); if (self->dict) - res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict); + res = PyTuple_MakeTriplet((PyObject *)Py_TYPE(self), args, self->dict); else - res = PyTuple_Pack(2, Py_TYPE(self), args); + res = PyTuple_MakePair((PyObject *)Py_TYPE(self), args); Py_DECREF(args); return res; } @@ -2680,7 +2679,7 @@ AttributeError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) } PyAttributeErrorObject *self = PyAttributeErrorObject_CAST(op); - PyObject *return_value = PyTuple_Pack(3, Py_TYPE(self), self->args, state); + PyObject *return_value = PyTuple_MakeTriplet((PyObject *)Py_TYPE(self), self->args, state); Py_DECREF(state); return return_value; } diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 1fefb12803ec19..efb157e1b65972 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1562,7 +1562,7 @@ float_as_integer_ratio_impl(PyObject *self) goto error; } - result_pair = PyTuple_Pack(2, numerator, denominator); + result_pair = PyTuple_MakePair(numerator, denominator); error: Py_XDECREF(py_exponent); diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 0cae3703d1d0c6..65c8c3f19d3f30 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -628,7 +628,7 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored)) PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i); if (value) { - PyObject *pair = PyTuple_Pack(2, name, value); + PyObject *pair = PyTuple_MakePair(name, value); if (pair == NULL) { Py_DECREF(items); Py_DECREF(value); @@ -653,7 +653,7 @@ framelocalsproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored)) PyObject *key = NULL; PyObject *value = NULL; while (PyDict_Next(frame->f_extra_locals, &j, &key, &value)) { - PyObject *pair = PyTuple_Pack(2, key, value); + PyObject *pair = PyTuple_MakePair(key, value); if (pair == NULL) { Py_DECREF(items); return NULL; @@ -948,7 +948,7 @@ PyTypeObject PyFrameLocalsProxy_Type = { PyObject * _PyFrameLocalsProxy_New(PyFrameObject *frame) { - PyObject* args = PyTuple_Pack(1, frame); + PyObject* args = PyTuple_MakeSingle((PyObject *)frame); if (args == NULL) { return NULL; } diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index 8b526f43f1e053..e9b2dfea130d6c 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -738,7 +738,7 @@ static PyObject * ga_mro_entries(PyObject *self, PyObject *args) { gaobject *alias = (gaobject *)self; - return PyTuple_Pack(1, alias->origin); + return PyTuple_MakeSingle(alias->origin); } static PyObject * @@ -864,7 +864,7 @@ static PyGetSetDef ga_properties[] = { static inline int setup_ga(gaobject *alias, PyObject *origin, PyObject *args) { if (!PyTuple_Check(args)) { - args = PyTuple_Pack(1, args); + args = PyTuple_MakeSingle(args); if (args == NULL) { return 0; } diff --git a/Objects/listobject.c b/Objects/listobject.c index b2903e5c93ee9f..9e85f8cc472cc3 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1384,7 +1384,7 @@ list_extend_dictitems(PyListObject *self, PyDictObject *dict) Py_ssize_t i = 0; PyObject *key, *value; while (_PyDict_Next((PyObject *)dict, &pos, &key, &value, NULL)) { - PyObject *item = PyTuple_Pack(2, key, value); + PyObject *item = PyTuple_MakePair(key, value); if (item == NULL) { Py_SET_SIZE(self, m + i); return -1; diff --git a/Objects/longobject.c b/Objects/longobject.c index 5eb4063f861f7a..fa7766c96c192d 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4885,15 +4885,7 @@ long_divmod(PyObject *a, PyObject *b) if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { return NULL; } - z = PyTuple_New(2); - if (z != NULL) { - PyTuple_SET_ITEM(z, 0, (PyObject *) div); - PyTuple_SET_ITEM(z, 1, (PyObject *) mod); - } - else { - Py_DECREF(div); - Py_DECREF(mod); - } + z = PyTuple_MakePairSteal((PyObject *) div, (PyObject *) mod); return z; } @@ -6184,13 +6176,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) goto error; } - result = PyTuple_New(2); - if (result == NULL) - goto error; - - /* PyTuple_SET_ITEM steals references */ - PyTuple_SET_ITEM(result, 0, (PyObject *)quo); - PyTuple_SET_ITEM(result, 1, (PyObject *)rem); + result = PyTuple_MakePairSteal((PyObject *)quo, (PyObject *)rem); return result; error: @@ -6373,7 +6359,7 @@ int_as_integer_ratio_impl(PyObject *self) if (numerator == NULL) { return NULL; } - ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_GetOne()); + ratio_tuple = PyTuple_MakePair(numerator, _PyLong_GetOne()); Py_DECREF(numerator); return ratio_tuple; } diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index 201cb8a7df8da1..e38a378ea7b0bb 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -217,7 +217,7 @@ namespace_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) if (!args) return NULL; - result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict); + result = PyTuple_MakeTriplet((PyObject *)Py_TYPE(ns), args, ns->ns_dict); Py_DECREF(args); return result; } diff --git a/Objects/odictobject.c b/Objects/odictobject.c index bcdf94c6c64faa..68a1bcedcbbda7 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -1157,7 +1157,7 @@ OrderedDict_popitem_impl(PyODictObject *self, int last) value = _odict_popkey_hash((PyObject *)self, key, NULL, _odictnode_HASH(node)); if (value == NULL) return NULL; - item = PyTuple_Pack(2, key, value); + item = PyTuple_MakePair(key, value); Py_DECREF(key); Py_DECREF(value); return item; @@ -1857,7 +1857,7 @@ odictiter_new(PyODictObject *od, int kind) return NULL; if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) { - di->di_result = PyTuple_Pack(2, Py_None, Py_None); + di->di_result = PyTuple_MakePair(Py_None, Py_None); if (di->di_result == NULL) { Py_DECREF(di); return NULL; diff --git a/Objects/setobject.c b/Objects/setobject.c index d8340499be5aae..e4e3046b2cbb93 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -2400,13 +2400,13 @@ set___reduce___impl(PySetObject *so) keys = PySequence_List((PyObject *)so); if (keys == NULL) goto done; - args = PyTuple_Pack(1, keys); + args = PyTuple_MakeSingle(keys); if (args == NULL) goto done; state = _PyObject_GetState((PyObject *)so); if (state == NULL) goto done; - result = PyTuple_Pack(3, Py_TYPE(so), args, state); + result = PyTuple_MakeTriplet((PyObject *)Py_TYPE(so), args, state); done: Py_XDECREF(args); Py_XDECREF(keys); diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 5186ff4f6f0cf5..b1f222733825c4 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -596,18 +596,18 @@ slice_richcompare(PyObject *v, PyObject *w, int op) } - PyObject *t1 = PyTuple_Pack(3, - ((PySliceObject *)v)->start, - ((PySliceObject *)v)->stop, - ((PySliceObject *)v)->step); + PyObject *t1 = PyTuple_MakeTriplet( + ((PySliceObject *)v)->start, + ((PySliceObject *)v)->stop, + ((PySliceObject *)v)->step); if (t1 == NULL) { return NULL; } - PyObject *t2 = PyTuple_Pack(3, - ((PySliceObject *)w)->start, - ((PySliceObject *)w)->stop, - ((PySliceObject *)w)->step); + PyObject *t2 = PyTuple_MakeTriplet( + ((PySliceObject *)w)->start, + ((PySliceObject *)w)->stop, + ((PySliceObject *)w)->step); if (t2 == NULL) { Py_DECREF(t1); return NULL; diff --git a/Objects/stringlib/unicode_format.h b/Objects/stringlib/unicode_format.h index ff32db65b11a0b..dbb6040e7ae78e 100644 --- a/Objects/stringlib/unicode_format.h +++ b/Objects/stringlib/unicode_format.h @@ -1183,7 +1183,7 @@ fieldnameiter_next(PyObject *op) goto done; /* return a tuple of values */ - result = PyTuple_Pack(2, is_attr_obj, obj); + result = PyTuple_MakePair(is_attr_obj, obj); done: Py_XDECREF(is_attr_obj); @@ -1274,7 +1274,7 @@ formatter_field_name_split(PyObject *Py_UNUSED(module), PyObject *self) goto done; /* return a tuple of values */ - result = PyTuple_Pack(2, first_obj, it); + result = PyTuple_MakePair(first_obj, (PyObject *)it); done: Py_XDECREF(it); diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 94b7ae7e642283..6507c8f4b502a1 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -1181,3 +1181,85 @@ _PyTuple_DebugMallocStats(FILE *out) _PyObject_VAR_SIZE(&PyTuple_Type, len)); } } + +PyObject * +PyTuple_MakeSingle(PyObject *one) +{ + assert (one != NULL); + + PyTupleObject *op = tuple_alloc(1); + if (op == NULL) { + return NULL; + } + op->ob_item[0] = Py_NewRef(one); + _PyObject_GC_TRACK(op); + return (PyObject *) op; +} + + +PyObject * +PyTuple_MakeSingleSteal(PyObject *one) +{ + assert (one != NULL); + + PyTupleObject *op = tuple_alloc(1); + if (op == NULL) { + Py_DECREF(one); + return NULL; + } + op->ob_item[0] = one; + _PyObject_GC_TRACK(op); + return (PyObject *) op; +} + +PyObject * +PyTuple_MakePair(PyObject *one, PyObject *two) +{ + assert (one != NULL); + assert (two != NULL); + + PyTupleObject *op = tuple_alloc(2); + if (op == NULL) { + return NULL; + } + op->ob_item[0] = Py_NewRef(one); + op->ob_item[1] = Py_NewRef(two); + _PyObject_GC_TRACK(op); + return (PyObject *) op; +} + +PyObject * +PyTuple_MakePairSteal(PyObject *one, PyObject *two) +{ + assert (one != NULL); + assert (two != NULL); + + 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; + _PyObject_GC_TRACK(op); + return (PyObject *) op; +} + +PyObject * +PyTuple_MakeTriplet(PyObject *one, PyObject *two, PyObject *three) +{ + assert (one != NULL); + assert (two != NULL); + assert (three != NULL); + + PyTupleObject *op = tuple_alloc(3); + if (op == NULL) { + return NULL; + } + op->ob_item[0] = Py_NewRef(one); + op->ob_item[1] = Py_NewRef(two); + op->ob_item[2] = Py_NewRef(three); + _PyObject_GC_TRACK(op); + return (PyObject *) op; +} diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 9398bcb29c83e4..5cbfc5b8489f25 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1794,10 +1794,10 @@ mro_hierarchy(PyTypeObject *type, PyObject *temp) PyObject *tuple; if (old_mro != NULL) { - tuple = PyTuple_Pack(3, type, new_mro, old_mro); + tuple = PyTuple_MakeTriplet((PyObject *)type, new_mro, old_mro); } else { - tuple = PyTuple_Pack(2, type, new_mro); + tuple = PyTuple_MakePair((PyObject *)type, new_mro); } if (tuple != NULL) { @@ -4834,7 +4834,7 @@ type_new_get_slots(type_new_ctx *ctx, PyObject *dict) // Make it into a tuple PyObject *new_slots; if (PyUnicode_Check(slots)) { - new_slots = PyTuple_Pack(1, slots); + new_slots = PyTuple_MakeSingle(slots); } else { new_slots = PySequence_Tuple(slots); @@ -4946,7 +4946,7 @@ type_new_get_bases(type_new_ctx *ctx, PyObject **type) if (nbases == 0) { // Adjust for empty tuple bases ctx->base = &PyBaseObject_Type; - PyObject *new_bases = PyTuple_Pack(1, ctx->base); + PyObject *new_bases = PyTuple_MakeSingle((PyObject *)ctx->base); if (new_bases == NULL) { return -1; } @@ -5115,7 +5115,7 @@ get_bases_tuple(PyObject *bases_in, PyType_Spec *spec) } } if (!bases) { - return PyTuple_Pack(1, base); + return PyTuple_MakeSingle((PyObject *)base); } if (PyTuple_Check(bases)) { return Py_NewRef(bases); @@ -5127,7 +5127,7 @@ get_bases_tuple(PyObject *bases_in, PyType_Spec *spec) return Py_NewRef(bases_in); } // Not a tuple, should be a single type - return PyTuple_Pack(1, bases_in); + return PyTuple_MakeSingle(bases_in); } static inline int @@ -7718,7 +7718,7 @@ object_getstate_default(PyObject *obj, int required) if (PyDict_GET_SIZE(slots) > 0) { PyObject *state2; - state2 = PyTuple_Pack(2, state, slots); + state2 = PyTuple_MakePair(state, slots); Py_DECREF(state); if (state2 == NULL) { Py_DECREF(slotnames); @@ -7967,7 +7967,7 @@ reduce_newobj(PyObject *obj) Py_DECREF(kwargs); return NULL; } - newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs); + newargs = PyTuple_MakeTriplet((PyObject *)Py_TYPE(obj), args, kwargs); Py_DECREF(args); Py_DECREF(kwargs); if (newargs == NULL) { @@ -8796,7 +8796,7 @@ type_ready_set_bases(PyTypeObject *type, int initial) bases = PyTuple_New(0); } else { - bases = PyTuple_Pack(1, base); + bases = PyTuple_MakeSingle((PyObject *)base); } if (bases == NULL) { return -1; diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 522e9fd9c955af..10df6da04c1533 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -372,7 +372,7 @@ type_check(PyObject *arg, const char *msg) static PyObject * make_union(PyObject *self, PyObject *other) { - PyObject *args = PyTuple_Pack(2, self, other); + PyObject *args = PyTuple_MakePair(self, other); if (args == NULL) { return NULL; } @@ -800,7 +800,7 @@ typevar_typing_prepare_subst_impl(typevarobject *self, PyObject *alias, return NULL; } if (dflt != &_Py_NoDefaultStruct) { - PyObject *new_args = PyTuple_Pack(1, dflt); + PyObject *new_args = PyTuple_MakeSingle(dflt); Py_DECREF(dflt); if (new_args == NULL) { Py_DECREF(params); @@ -1536,7 +1536,7 @@ unpack_iter(PyObject *self) if (unpacked == NULL) { return NULL; } - PyObject *tuple = PyTuple_Pack(1, unpacked); + PyObject *tuple = PyTuple_MakeSingle(unpacked); if (tuple == NULL) { Py_DECREF(unpacked); return NULL; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d4549b70d4dabc..45f90877243e09 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -12989,7 +12989,7 @@ PyUnicode_Partition(PyObject *str_obj, PyObject *sep_obj) len2 = PyUnicode_GET_LENGTH(sep_obj); if (kind1 < kind2 || len1 < len2) { PyObject *empty = unicode_get_empty(); // Borrowed reference - return PyTuple_Pack(3, str_obj, empty, empty); + return PyTuple_MakeTriplet(str_obj, empty, empty); } buf1 = PyUnicode_DATA(str_obj); buf2 = PyUnicode_DATA(sep_obj); @@ -13041,7 +13041,7 @@ PyUnicode_RPartition(PyObject *str_obj, PyObject *sep_obj) len2 = PyUnicode_GET_LENGTH(sep_obj); if (kind1 < kind2 || len1 < len2) { PyObject *empty = unicode_get_empty(); // Borrowed reference - return PyTuple_Pack(3, empty, empty, str_obj); + return PyTuple_MakeTriplet(empty, empty, str_obj); } buf1 = PyUnicode_DATA(str_obj); buf2 = PyUnicode_DATA(sep_obj); diff --git a/Parser/pegen_errors.c b/Parser/pegen_errors.c index 0639a4e42436be..c103968a009e53 100644 --- a/Parser/pegen_errors.c +++ b/Parser/pegen_errors.c @@ -41,7 +41,7 @@ _PyPegen_raise_tokenizer_init_error(PyObject *filename) goto error; } - tuple = PyTuple_Pack(2, errstr, tmp); + tuple = PyTuple_MakePair(errstr, tmp); Py_DECREF(tmp); if (!value) { goto error; @@ -393,7 +393,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype, if (!tmp) { goto error; } - value = PyTuple_Pack(2, errstr, tmp); + value = PyTuple_MakePair(errstr, tmp); Py_DECREF(tmp); if (!value) { goto error; diff --git a/Python/Python-tokenize.c b/Python/Python-tokenize.c index 152d61c686722e..32efbd3d4592ff 100644 --- a/Python/Python-tokenize.c +++ b/Python/Python-tokenize.c @@ -164,7 +164,7 @@ _tokenizer_error(tokenizeriterobject *it) goto exit; } - value = PyTuple_Pack(2, errstr, tmp); + value = PyTuple_MakePair(errstr, tmp); if (!value) { result = -1; goto exit; diff --git a/Python/_warnings.c b/Python/_warnings.c index 243a5e88e9dbbc..0e2fdb981fe648 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -653,9 +653,9 @@ update_registry(PyInterpreterState *interp, PyObject *registry, PyObject *text, int rc; if (add_zero) - altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero()); + altkey = PyTuple_MakeTriplet(text, category, _PyLong_GetZero()); else - altkey = PyTuple_Pack(2, text, category); + altkey = PyTuple_MakePair(text, category); rc = already_warned(interp, registry, altkey, 1); Py_XDECREF(altkey); @@ -845,7 +845,7 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message, } /* Create key. */ - key = PyTuple_Pack(3, text, category, lineno_obj); + key = PyTuple_MakeTriplet(text, category, lineno_obj); if (key == NULL) goto cleanup; diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 64249177eec5f2..c178401e76e340 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -3256,9 +3256,9 @@ zip_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) zipobject *lz = _zipobject_CAST(self); /* Just recreate the zip with the internal iterator tuple */ if (lz->strict) { - return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True); + return PyTuple_MakeTriplet((PyObject *)Py_TYPE(lz), lz->ittuple, Py_True); } - return PyTuple_Pack(2, Py_TYPE(lz), lz->ittuple); + return PyTuple_MakePair((PyObject *)Py_TYPE(lz), lz->ittuple); } static PyObject * diff --git a/Python/ceval.c b/Python/ceval.c index f48f412fab8335..b171378c36d550 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2200,7 +2200,7 @@ _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *frame, PyObject* exc_value, } else { /* naked exception - wrap it */ - PyObject *excs = PyTuple_Pack(1, exc_value); + PyObject *excs = PyTuple_MakeSingle(exc_value); if (excs == NULL) { return -1; } diff --git a/Python/codegen.c b/Python/codegen.c index c4109fcaa48dbe..ba5d75b8a820f6 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -1206,7 +1206,7 @@ codegen_type_param_bound_or_default(compiler *c, expr_ty e, identifier name, void *key, bool allow_starred) { - PyObject *defaults = PyTuple_Pack(1, _PyLong_GetOne()); + PyObject *defaults = PyTuple_MakeSingle(_PyLong_GetOne()); ADDOP_LOAD_CONST_NEW(c, LOC(e), defaults); RETURN_IF_ERROR(codegen_setup_annotations_scope(c, LOC(e), key, name)); if (allow_starred && e->kind == Starred_kind) { @@ -1711,7 +1711,7 @@ codegen_typealias_body(compiler *c, stmt_ty s) { location loc = LOC(s); PyObject *name = s->v.TypeAlias.name->v.Name.id; - PyObject *defaults = PyTuple_Pack(1, _PyLong_GetOne()); + PyObject *defaults = PyTuple_MakeSingle(_PyLong_GetOne()); ADDOP_LOAD_CONST_NEW(c, loc, defaults); RETURN_IF_ERROR( codegen_setup_annotations_scope(c, LOC(s), s, name)); diff --git a/Python/compile.c b/Python/compile.c index c04391e682f9ac..f22a6662bd5725 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1657,7 +1657,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags, return NULL; } /* Allocate a copy of the instruction sequence on the heap */ - res = PyTuple_Pack(2, _PyCompile_InstrSequence(c), metadata); + res = PyTuple_MakePair((PyObject *)_PyCompile_InstrSequence(c), metadata); finally: Py_XDECREF(metadata); diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 16a23f0351cd26..6995af6458df4a 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -1085,7 +1085,7 @@ _convert_exc_to_TracebackException(PyObject *exc, PyObject **p_tbexc) return -1; } - args = PyTuple_Pack(1, exc); + args = PyTuple_MakeSingle(exc); if (args == NULL) { goto error; } diff --git a/Python/errors.c b/Python/errors.c index 2688396004e98b..bc2a8e08da3f70 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1312,7 +1312,7 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict) if (PyTuple_Check(base)) { bases = Py_NewRef(base); } else { - bases = PyTuple_Pack(1, base); + bases = PyTuple_MakeSingle(base); if (bases == NULL) goto failure; } diff --git a/Python/hamt.c b/Python/hamt.c index e372b1a1b4c18b..5f6bc0b9c4bf84 100644 --- a/Python/hamt.c +++ b/Python/hamt.c @@ -2525,7 +2525,7 @@ PyTypeObject _PyHamtItems_Type = { static PyObject * hamt_iter_yield_items(PyObject *key, PyObject *val) { - return PyTuple_Pack(2, key, val); + return PyTuple_MakePair(key, val); } PyObject * diff --git a/Python/import.c b/Python/import.c index d01c4d478283ff..bf07bffac2164c 100644 --- a/Python/import.c +++ b/Python/import.c @@ -4447,9 +4447,9 @@ _imp_find_frozen_impl(PyObject *module, PyObject *name, int withdata) } } - PyObject *result = PyTuple_Pack(3, data ? data : Py_None, - info.is_package ? Py_True : Py_False, - origname ? origname : Py_None); + PyObject *result = PyTuple_MakeTriplet(data ? data : Py_None, + info.is_package ? Py_True : Py_False, + origname ? origname : Py_None); Py_XDECREF(origname); Py_XDECREF(data); return result; diff --git a/Python/legacy_tracing.c b/Python/legacy_tracing.c index 594d5c5ead5021..8bcca7d9c7ab10 100644 --- a/Python/legacy_tracing.c +++ b/Python/legacy_tracing.c @@ -218,7 +218,7 @@ sys_trace_exception_func( if (tb == NULL) { tb = Py_NewRef(Py_None); } - PyObject *tuple = PyTuple_Pack(3, type, exc, tb); + PyObject *tuple = PyTuple_MakeTriplet(type, exc, tb); Py_DECREF(tb); if (tuple == NULL) { return NULL; diff --git a/Python/marshal.c b/Python/marshal.c index 4f444d4671cbff..e52e2d7b6c3577 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -612,7 +612,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p) Py_DECREF(value); break; } - PyObject *pair = PyTuple_Pack(2, dump, value); + PyObject *pair = PyTuple_MakePair(dump, value); Py_DECREF(dump); Py_DECREF(value); if (pair == NULL) { diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index b813166f167d70..c0a3a48d1a4a60 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1566,7 +1566,7 @@ finalize_remove_modules(PyObject *modules, int verbose) if (weaklist != NULL) { \ PyObject *wr = PyWeakref_NewRef(mod, NULL); \ if (wr) { \ - PyObject *tup = PyTuple_Pack(2, name, wr); \ + PyObject *tup = PyTuple_MakePair(name, wr); \ if (!tup || PyList_Append(weaklist, tup) < 0) { \ PyErr_FormatUnraisable("Exception ignored while removing modules"); \ } \ diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 59baca26793f6c..5f01342ee97f12 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -277,7 +277,7 @@ sys_audit_tstate(PyThreadState *ts, const char *event, if (argFormat && argFormat[0]) { eventArgs = Py_VaBuildValue(argFormat, vargs); if (eventArgs && !PyTuple_Check(eventArgs)) { - PyObject *argTuple = PyTuple_Pack(1, eventArgs); + PyObject *argTuple = PyTuple_MakeSingle(eventArgs); Py_SETREF(eventArgs, argTuple); } }