Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 4 additions & 9 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_runtime_init.h" // _Py_ID()
#include "pycore_tuple.h" // _PyTuple_FromPair

#include <stddef.h> // offsetof()

Expand Down Expand Up @@ -829,14 +830,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_FromPair(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);
Expand Down Expand Up @@ -1503,14 +1500,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_FromPair(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++;
}
Expand Down
33 changes: 12 additions & 21 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <Python.h>
#include "pycore_object.h" // _PyObject_VisitType()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_typeobject.h"

#include <mpdecimal.h>
Expand Down Expand Up @@ -3975,7 +3976,6 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
PyObject *numerator = NULL;
PyObject *denominator = NULL;
PyObject *exponent = NULL;
PyObject *result = NULL;
PyObject *tmp;
mpd_ssize_t exp;
PyObject *context;
Expand Down Expand Up @@ -4035,6 +4035,7 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)

if (exp >= 0) {
Py_SETREF(numerator, state->_py_long_multiply(numerator, exponent));
Py_CLEAR(exponent);
if (numerator == NULL) {
goto error;
}
Expand All @@ -4061,15 +4062,13 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
goto error;
}
}

result = PyTuple_Pack(2, numerator, denominator);

return _PyTuple_FromPairSteal(numerator, denominator);

error:
Py_XDECREF(exponent);
Py_XDECREF(denominator);
Py_XDECREF(numerator);
return result;
return NULL;
}

/*[clinic input]
Expand Down Expand Up @@ -4613,7 +4612,6 @@ nm_mpd_qdivmod(PyObject *v, PyObject *w)
PyObject *q, *r;
PyObject *context;
uint32_t status = 0;
PyObject *ret;

decimal_state *state = find_state_left_or_right(v, w);
CURRENT_CONTEXT(state, context);
Expand Down Expand Up @@ -4642,10 +4640,7 @@ nm_mpd_qdivmod(PyObject *v, PyObject *w)
return NULL;
}

ret = PyTuple_Pack(2, q, r);
Py_DECREF(r);
Py_DECREF(q);
return ret;
return _PyTuple_FromPairSteal(q, r);
}

static PyObject *
Expand Down Expand Up @@ -6674,7 +6669,6 @@ _decimal_Context_divmod_impl(PyObject *context, PyObject *x, PyObject *y)
PyObject *a, *b;
PyObject *q, *r;
uint32_t status = 0;
PyObject *ret;

CONVERT_BINOP_RAISE(&a, &b, x, y, context);
decimal_state *state = get_module_state_from_ctx(context);
Expand All @@ -6701,10 +6695,7 @@ _decimal_Context_divmod_impl(PyObject *context, PyObject *x, PyObject *y)
return NULL;
}

ret = PyTuple_Pack(2, q, r);
Py_DECREF(r);
Py_DECREF(q);
return ret;
return _PyTuple_FromPairSteal(q, r);
}

/* Binary or ternary arithmetic functions */
Expand Down Expand Up @@ -7810,15 +7801,15 @@ _decimal_exec(PyObject *m)

switch (cm->flag) {
case MPD_Float_operation:
base = PyTuple_Pack(2, state->DecimalException, PyExc_TypeError);
base = _PyTuple_FromPair(state->DecimalException, PyExc_TypeError);
break;
case MPD_Division_by_zero:
base = PyTuple_Pack(2, state->DecimalException,
PyExc_ZeroDivisionError);
base = _PyTuple_FromPair(state->DecimalException,
PyExc_ZeroDivisionError);
break;
case MPD_Overflow:
base = PyTuple_Pack(2, state->signal_map[INEXACT].ex,
state->signal_map[ROUNDED].ex);
base = _PyTuple_FromPair(state->signal_map[INEXACT].ex,
state->signal_map[ROUNDED].ex);
break;
case MPD_Underflow:
base = PyTuple_Pack(3, state->signal_map[INEXACT].ex,
Expand Down Expand Up @@ -7857,7 +7848,7 @@ _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_FromPair(state->signal_map[0].ex, PyExc_ZeroDivisionError);
}
else {
base = PyTuple_Pack(1, state->signal_map[0].ex);
Expand Down
9 changes: 5 additions & 4 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Python.h"
#include "pycore_dict.h" // _PyDict_CopyAsDict()
#include "pycore_pyhash.h" // _Py_HashSecret
#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

#include <stddef.h> // offsetof()
Expand Down Expand Up @@ -2594,7 +2595,7 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
return NULL;
}

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

Expand Down Expand Up @@ -2712,7 +2713,7 @@ treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
{
if (action != NULL) {
PyObject *res;
PyObject *event = PyTuple_Pack(2, action, node);
PyObject *event = _PyTuple_FromPair(action, node);
if (event == NULL)
return -1;
res = PyObject_CallOneArg(self->events_append, event);
Expand Down Expand Up @@ -2933,7 +2934,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_FromPair(target, text);
if (!pi) {
return NULL;
}
Expand All @@ -2957,7 +2958,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_FromPair(prefix, uri);
if (!parcel) {
return NULL;
}
Expand Down
17 changes: 4 additions & 13 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "pycore_global_strings.h" // _Py_ID()
#include "pycore_pyerrors.h" // _PyErr_FormatNote
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_unicodeobject.h" // _PyUnicode_CheckConsistency()

#include <stdbool.h> // bool
Expand Down Expand Up @@ -446,7 +447,6 @@ raise_stop_iteration(Py_ssize_t idx)
static PyObject *
_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
/* return (rval, idx) tuple, stealing reference to rval */
PyObject *tpl;
PyObject *pyidx;
/*
steal a reference to rval, returns (rval, idx)
Expand All @@ -459,15 +459,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);
return tpl;
return _PyTuple_FromPairSteal(rval, pyidx);
}

static PyObject *
Expand Down Expand Up @@ -810,11 +802,10 @@ _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_FromPairSteal(key, val);
key = val = NULL;
if (item == NULL)
goto bail;
Py_CLEAR(key);
Py_CLEAR(val);
if (PyList_Append(rval, item) == -1) {
Py_DECREF(item);
goto bail;
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_FromPair((PyObject *)Py_TYPE(ig), ig->item);
}

PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
Expand Down
32 changes: 9 additions & 23 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_symtable.h" // _Py_Mangle()
#include "pycore_sysmodule.h" // _PySys_GetSizeOf()
#include "pycore_tuple.h" // _PyTuple_FromPair
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()

#include <stdlib.h> // strtol()
Expand Down Expand Up @@ -3767,7 +3768,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_FromPair(*module_name, *global_name);
if (key == NULL)
return -1;
item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Expand Down Expand Up @@ -3873,7 +3874,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_FromPair(module_name, global_name);
if (extension_key == NULL) {
goto error;
}
Expand Down Expand Up @@ -5133,26 +5134,19 @@ static PyObject *
_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
{
PyObject *reduce_value, *dict_args;
PyObject *dict_args;
PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(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);
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);
return reduce_value;

return _PyTuple_FromPairSteal(Py_NewRef(&PyDict_Type), dict_args);
}

static PyMethodDef picklerproxy_methods[] = {
Expand Down Expand Up @@ -7310,7 +7304,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_FromPair(module_name, global_name);
if (key == NULL)
return NULL;
item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Expand Down Expand Up @@ -7640,27 +7634,19 @@ static PyObject *
_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
{
PyObject *reduce_value;
PyObject *constructor_args;
PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(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);
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);
return reduce_value;

return _PyTuple_FromPairSteal(Py_NewRef(&PyDict_Type), constructor_args);
}

static PyMethodDef unpicklerproxy_methods[] = {
Expand Down
30 changes: 10 additions & 20 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const char copyright[] =
#include "pycore_dict.h" // _PyDict_Next()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
#include "pycore_unicodeobject.h" // _PyUnicode_Copy
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()

Expand Down Expand Up @@ -2572,28 +2573,17 @@ _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group)
LOCAL(PyObject*)
_pair(Py_ssize_t i1, Py_ssize_t i2)
{
PyObject* pair;
PyObject* item;

pair = PyTuple_New(2);
if (!pair)
PyObject* item1 = PyLong_FromSsize_t(i1);
if (!item1) {
return NULL;
}
PyObject* item2 = PyLong_FromSsize_t(i2);
if(!item2) {
Py_DECREF(item1);
return NULL;
}

item = PyLong_FromSsize_t(i1);
if (!item)
goto error;
PyTuple_SET_ITEM(pair, 0, item);

item = PyLong_FromSsize_t(i2);
if (!item)
goto error;
PyTuple_SET_ITEM(pair, 1, item);

return pair;

error:
Py_DECREF(pair);
return NULL;
return _PyTuple_FromPairSteal(item1, item2);
}

/*[clinic input]
Expand Down
Loading
Loading