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_FromPair((PyObject *)Py_TYPE(op), op);
}
else if (PyFloat_CheckExact(op)) {
double d = PyFloat_AS_DOUBLE(op);
if (d == 0.0 && copysign(1.0, d) < 0.0)
key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
else
- key = PyTuple_Pack(2, Py_TYPE(op), op);
+ key = _PyTuple_FromPair((PyObject *)Py_TYPE(op), op);
}
else if (PyComplex_CheckExact(op)) {
Py_complex z;
key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
}
else {
- key = PyTuple_Pack(2, Py_TYPE(op), op);
+ key = _PyTuple_FromPair((PyObject *)Py_TYPE(op), op);
}
}
else if (PyTuple_CheckExact(op)) {
PyTuple_SET_ITEM(tuple, i, item_key);
}
- key = PyTuple_Pack(2, tuple, op);
+ key = _PyTuple_FromPair(tuple, op);
Py_DECREF(tuple);
}
else if (PyFrozenSet_CheckExact(op)) {
if (set == NULL)
return NULL;
- key = PyTuple_Pack(2, set, op);
+ key = _PyTuple_FromPair(set, op);
Py_DECREF(set);
return key;
}
goto slice_exit;
}
- key = PyTuple_Pack(2, slice_key, op);
+ key = _PyTuple_FromPair(slice_key, op);
Py_DECREF(slice_key);
slice_exit:
Py_XDECREF(start_key);
if (obj_id == NULL)
return NULL;
- key = PyTuple_Pack(2, obj_id, op);
+ key = _PyTuple_FromPair(obj_id, op);
Py_DECREF(obj_id);
}
return key;
}
if (itertype == &PyDictIterItem_Type ||
itertype == &PyDictRevIterItem_Type) {
- di->di_result = PyTuple_Pack(2, Py_None, Py_None);
+ di->di_result = _PyTuple_FromPairSteal(Py_None, Py_None);
if (di->di_result == NULL) {
Py_DECREF(di);
return NULL;
_PyTuple_Recycle(result);
}
else {
- result = PyTuple_New(2);
- if (result == NULL) {
- Py_DECREF(key);
- Py_DECREF(value);
- return NULL;
- }
- PyTuple_SET_ITEM(result, 0, key);
- PyTuple_SET_ITEM(result, 1, value);
+ result = _PyTuple_FromPairSteal(key, value);
}
return result;
}
_PyTuple_Recycle(result);
}
else {
- result = PyTuple_New(2);
- if (result == NULL) {
- return NULL;
- }
- PyTuple_SET_ITEM(result, 0, Py_NewRef(key));
- PyTuple_SET_ITEM(result, 1, Py_NewRef(value));
+ result = _PyTuple_FromPair(key, value);
}
return result;
}
else {
Py_INCREF(val1);
to_delete = PyObject_RichCompareBool(val1, val2, Py_EQ);
+ Py_CLEAR(val1);
if (to_delete < 0) {
goto error;
}
}
if (to_delete) {
+ Py_CLEAR(val2);
if (_PyDict_DelItem_KnownHash(temp_dict, key, hash) < 0) {
goto error;
}
+ Py_CLEAR(key);
}
else {
- PyObject *pair = PyTuple_Pack(2, key, val2);
+ PyObject *pair = _PyTuple_FromPairSteal(key, val2);
+ key = val2 = NULL;
if (pair == NULL) {
goto error;
}
}
Py_DECREF(pair);
}
- Py_DECREF(key);
- Py_XDECREF(val1);
- Py_DECREF(val2);
}
- key = val1 = val2 = NULL;
PyObject *remaining_pairs = PyObject_CallMethodNoArgs(
temp_dict, &_Py_ID(items));
Py_DECREF(en);
return NULL;
}
- en->en_result = PyTuple_Pack(2, Py_None, Py_None);
+ en->en_result = _PyTuple_FromPairSteal(Py_None, Py_None);
if (en->en_result == NULL) {
Py_DECREF(en);
return NULL;
_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);
- return result;
+ return _PyTuple_FromPairSteal(next_index, next_item);
}
static PyObject *
_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);
- return result;
+ return _PyTuple_FromPairSteal(next_index, next_item);
}
static PyObject *
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_stackref.h" // PyStackRef_AsPyObjectBorrow()
#include "pycore_structseq.h" // _PyStructSequence_FiniBuiltin()
+#include "pycore_tuple.h" // _PyTuple_FromPair
#include <float.h> // DBL_MAX
#include <stdlib.h> // strtol()
if (denominator == NULL)
goto error;
}
+ Py_DECREF(py_exponent);
- result_pair = PyTuple_Pack(2, numerator, denominator);
+ return _PyTuple_FromPairSteal(numerator, denominator);
error:
Py_XDECREF(py_exponent);
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode_metadata.h" // _PyOpcode_Caches
#include "pycore_optimizer.h" // _Py_Executors_InvalidateDependency()
+#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_unicodeobject.h" // _PyUnicode_Equal()
#include "frameobject.h" // PyFrameLocalsProxyObject
PyObject *value = framelocalsproxy_getval(frame->f_frame, co, i);
if (value) {
- PyObject *pair = PyTuple_Pack(2, name, value);
+ PyObject *pair = _PyTuple_FromPairSteal(Py_NewRef(name), value);
if (pair == NULL) {
- Py_DECREF(items);
- Py_DECREF(value);
- return NULL;
- }
-
- if (PyList_Append(items, pair) < 0) {
- Py_DECREF(items);
- Py_DECREF(pair);
- Py_DECREF(value);
- return NULL;
+ goto error;
}
+ int rc = PyList_Append(items, pair);
Py_DECREF(pair);
- Py_DECREF(value);
+ if (rc < 0) {
+ goto error;
+ }
}
}
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_FromPair(key, value);
if (pair == NULL) {
- Py_DECREF(items);
- return NULL;
- }
-
- if (PyList_Append(items, pair) < 0) {
- Py_DECREF(items);
- Py_DECREF(pair);
- return NULL;
+ goto error;
}
+ int rc = PyList_Append(items, pair);
Py_DECREF(pair);
+ if (rc < 0) {
+ goto error;
+ }
}
}
return items;
+
+error:
+ Py_DECREF(items);
+ return NULL;
}
static Py_ssize_t
PyObject **dest = self->ob_item + m;
Py_ssize_t pos = 0;
Py_ssize_t i = 0;
- PyObject *key_value[2];
- while (_PyDict_Next((PyObject *)dict, &pos, &key_value[0], &key_value[1], NULL)) {
- PyObject *item = PyTuple_FromArray(key_value, 2);
+ PyObject *key, *value;
+ while (_PyDict_Next((PyObject *)dict, &pos, &key, &value, NULL)) {
+ PyObject *item = _PyTuple_FromPair(key, value);
if (item == NULL) {
Py_SET_SIZE(self, m + i);
return -1;
#include "pycore_runtime.h" // _PY_NSMALLPOSINTS
#include "pycore_stackref.h"
#include "pycore_structseq.h" // _PyStructSequence_FiniBuiltin()
+#include "pycore_tuple.h" // _PyTuple_FromPairSteal
#include "pycore_unicodeobject.h" // _PyUnicode_Equal()
#include <float.h> // DBL_MANT_DIG
long_divmod(PyObject *a, PyObject *b)
{
PyLongObject *div, *mod;
- PyObject *z;
-
CHECK_BINOP(a, 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);
- }
- return z;
+ return _PyTuple_FromPairSteal((PyObject *)div, (PyObject *)mod);
}
_PyLong_DivmodNear(PyObject *a, PyObject *b)
{
PyLongObject *quo = NULL, *rem = NULL;
- PyObject *twice_rem, *result, *temp;
+ PyObject *twice_rem, *temp;
int quo_is_odd, quo_is_neg;
Py_ssize_t cmp;
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);
- return result;
+ return _PyTuple_FromPairSteal((PyObject *)quo, (PyObject *)rem);
error:
Py_XDECREF(quo);
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=384ff1766634bec2]*/
{
- PyObject *ratio_tuple;
PyObject *numerator = long_long(self);
if (numerator == NULL) {
return NULL;
}
- ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_GetOne());
- Py_DECREF(numerator);
- return ratio_tuple;
+ return _PyTuple_FromPairSteal(numerator, _PyLong_GetOne());
}
/*[clinic input]
OrderedDict_popitem_impl(PyODictObject *self, int last)
/*[clinic end generated code: output=98e7d986690d49eb input=8aafc7433e0a40e7]*/
{
- PyObject *key, *value, *item = NULL;
+ PyObject *key, *value;
_ODictNode *node;
/* pull the item */
node = last ? _odict_LAST(self) : _odict_FIRST(self);
key = Py_NewRef(_odictnode_KEY(node));
value = _odict_popkey_hash((PyObject *)self, key, NULL, _odictnode_HASH(node));
- if (value == NULL)
+ if (value == NULL) {
+ Py_DECREF(key);
return NULL;
- item = PyTuple_Pack(2, key, value);
- Py_DECREF(key);
- Py_DECREF(value);
- return item;
+ }
+ return _PyTuple_FromPairSteal(key, value);
}
/* keys() */
if (!PyErr_Occurred())
PyErr_SetObject(PyExc_KeyError, key);
Py_DECREF(key);
- goto done;
+ goto error;
}
/* Handle the values case. */
// bpo-42536: The GC may have untracked this result tuple. Since we're
// recycling it, make sure it's tracked again:
_PyTuple_Recycle(result);
+ PyTuple_SET_ITEM(result, 0, key); /* steals reference */
+ PyTuple_SET_ITEM(result, 1, value); /* steals reference */
}
else {
- result = PyTuple_New(2);
+ result = _PyTuple_FromPairSteal(key, value);
if (result == NULL) {
- Py_DECREF(key);
- Py_DECREF(value);
- goto done;
+ goto error;
}
}
- PyTuple_SET_ITEM(result, 0, key); /* steals reference */
- PyTuple_SET_ITEM(result, 1, value); /* steals reference */
return result;
-done:
+error:
Py_CLEAR(di->di_current);
Py_CLEAR(di->di_odict);
return NULL;
return NULL;
if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
- di->di_result = PyTuple_Pack(2, Py_None, Py_None);
+ di->di_result = _PyTuple_FromPairSteal(Py_None, Py_None);
if (di->di_result == NULL) {
Py_DECREF(di);
return NULL;
#include "pycore_complexobject.h" // _PyComplex_FormatAdvancedWriter()
#include "pycore_floatobject.h" // _PyFloat_FormatAdvancedWriter()
+#include "pycore_tuple.h" // _PyTuple_FromPairSteal
/************************************************************************/
/*********** Global data structures and forward declarations *********/
is_attr_obj = PyBool_FromLong(is_attr);
if (is_attr_obj == NULL)
- goto done;
+ goto error;
/* either an integer or a string */
if (idx != -1)
else
obj = SubString_new_object(&name);
if (obj == NULL)
- goto done;
+ goto error;
/* return a tuple of values */
- result = PyTuple_Pack(2, is_attr_obj, obj);
+ return _PyTuple_FromPairSteal(is_attr_obj, obj);
- done:
+ error:
Py_XDECREF(is_attr_obj);
Py_XDECREF(obj);
return result;
first_obj in that case. */
if (!field_name_split((PyObject*)self, 0, PyUnicode_GET_LENGTH(self),
&first, &first_idx, &it->it_field, NULL))
- goto done;
+ goto error;
/* first becomes an integer, if possible; else a string */
if (first_idx != -1)
/* convert "first" into a string object */
first_obj = SubString_new_object(&first);
if (first_obj == NULL)
- goto done;
+ goto error;
/* return a tuple of values */
- result = PyTuple_Pack(2, first_obj, it);
+ return _PyTuple_FromPairSteal(first_obj, (PyObject *)it);
-done:
+error:
Py_XDECREF(it);
Py_XDECREF(first_obj);
return result;
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_symtable.h" // _Py_Mangle()
+#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_typeobject.h" // struct type_cache
#include "pycore_unicodeobject.h" // _PyUnicode_Copy
#include "pycore_unionobject.h" // _Py_union_type_or
tuple = PyTuple_Pack(3, type, new_mro, old_mro);
}
else {
- tuple = PyTuple_Pack(2, type, new_mro);
+ tuple = _PyTuple_FromPair((PyObject *)type, new_mro);
}
if (tuple != NULL) {
if (PyDict_GET_SIZE(slots) > 0) {
PyObject *state2;
- state2 = PyTuple_Pack(2, state, slots);
+ state2 = _PyTuple_FromPair(state, slots);
Py_DECREF(state);
if (state2 == NULL) {
Py_DECREF(slotnames);
#include "Python.h"
#include "pycore_interpframe.h" // _PyInterpreterFrame
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK, PyAnnotateFormat
+#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_typevarobject.h"
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
#include "pycore_unionobject.h" // _Py_union_type_or, _Py_union_from_tuple
static PyObject *
make_union(PyObject *self, PyObject *other)
{
- PyObject *args = PyTuple_Pack(2, self, other);
+ PyObject *args = _PyTuple_FromPair(self, other);
if (args == NULL) {
return NULL;
}