Skip to content

Commit 0c10066

Browse files
GH-145247: Use _PyTuple_FromPair in Modules (part 1) (#148000)
1 parent 74a82a2 commit 0c10066

File tree

11 files changed

+62
-126
lines changed

11 files changed

+62
-126
lines changed

Modules/_asynciomodule.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
1414
#include "pycore_pystate.h" // _PyThreadState_GET()
1515
#include "pycore_runtime_init.h" // _Py_ID()
16+
#include "pycore_tuple.h" // _PyTuple_FromPair
1617

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

@@ -829,14 +830,10 @@ future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg,
829830
fut->fut_context0 = Py_NewRef(ctx);
830831
}
831832
else {
832-
PyObject *tup = PyTuple_New(2);
833+
PyObject *tup = _PyTuple_FromPair(arg, (PyObject *)ctx);
833834
if (tup == NULL) {
834835
return NULL;
835836
}
836-
Py_INCREF(arg);
837-
PyTuple_SET_ITEM(tup, 0, arg);
838-
Py_INCREF(ctx);
839-
PyTuple_SET_ITEM(tup, 1, (PyObject *)ctx);
840837

841838
if (fut->fut_callbacks != NULL) {
842839
int err = PyList_Append(fut->fut_callbacks, tup);
@@ -1503,14 +1500,12 @@ _asyncio_Future__callbacks_get_impl(FutureObj *self)
15031500

15041501
Py_ssize_t i = 0;
15051502
if (self->fut_callback0 != NULL) {
1506-
PyObject *tup0 = PyTuple_New(2);
1503+
assert(self->fut_context0 != NULL);
1504+
PyObject *tup0 = _PyTuple_FromPair(self->fut_callback0, self->fut_context0);
15071505
if (tup0 == NULL) {
15081506
Py_DECREF(callbacks);
15091507
return NULL;
15101508
}
1511-
PyTuple_SET_ITEM(tup0, 0, Py_NewRef(self->fut_callback0));
1512-
assert(self->fut_context0 != NULL);
1513-
PyTuple_SET_ITEM(tup0, 1, Py_NewRef(self->fut_context0));
15141509
PyList_SET_ITEM(callbacks, i, tup0);
15151510
i++;
15161511
}

Modules/_decimal/_decimal.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <Python.h>
3333
#include "pycore_object.h" // _PyObject_VisitType()
3434
#include "pycore_pystate.h" // _PyThreadState_GET()
35+
#include "pycore_tuple.h" // _PyTuple_FromPair
3536
#include "pycore_typeobject.h"
3637

3738
#include <mpdecimal.h>
@@ -3975,7 +3976,6 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
39753976
PyObject *numerator = NULL;
39763977
PyObject *denominator = NULL;
39773978
PyObject *exponent = NULL;
3978-
PyObject *result = NULL;
39793979
PyObject *tmp;
39803980
mpd_ssize_t exp;
39813981
PyObject *context;
@@ -4035,6 +4035,7 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
40354035

40364036
if (exp >= 0) {
40374037
Py_SETREF(numerator, state->_py_long_multiply(numerator, exponent));
4038+
Py_CLEAR(exponent);
40384039
if (numerator == NULL) {
40394040
goto error;
40404041
}
@@ -4061,15 +4062,13 @@ _decimal_Decimal_as_integer_ratio_impl(PyObject *self, PyTypeObject *cls)
40614062
goto error;
40624063
}
40634064
}
4064-
4065-
result = PyTuple_Pack(2, numerator, denominator);
4066-
4065+
return _PyTuple_FromPairSteal(numerator, denominator);
40674066

40684067
error:
40694068
Py_XDECREF(exponent);
40704069
Py_XDECREF(denominator);
40714070
Py_XDECREF(numerator);
4072-
return result;
4071+
return NULL;
40734072
}
40744073

40754074
/*[clinic input]
@@ -4613,7 +4612,6 @@ nm_mpd_qdivmod(PyObject *v, PyObject *w)
46134612
PyObject *q, *r;
46144613
PyObject *context;
46154614
uint32_t status = 0;
4616-
PyObject *ret;
46174615

46184616
decimal_state *state = find_state_left_or_right(v, w);
46194617
CURRENT_CONTEXT(state, context);
@@ -4642,10 +4640,7 @@ nm_mpd_qdivmod(PyObject *v, PyObject *w)
46424640
return NULL;
46434641
}
46444642

4645-
ret = PyTuple_Pack(2, q, r);
4646-
Py_DECREF(r);
4647-
Py_DECREF(q);
4648-
return ret;
4643+
return _PyTuple_FromPairSteal(q, r);
46494644
}
46504645

46514646
static PyObject *
@@ -6674,7 +6669,6 @@ _decimal_Context_divmod_impl(PyObject *context, PyObject *x, PyObject *y)
66746669
PyObject *a, *b;
66756670
PyObject *q, *r;
66766671
uint32_t status = 0;
6677-
PyObject *ret;
66786672

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

6704-
ret = PyTuple_Pack(2, q, r);
6705-
Py_DECREF(r);
6706-
Py_DECREF(q);
6707-
return ret;
6698+
return _PyTuple_FromPairSteal(q, r);
67086699
}
67096700

67106701
/* Binary or ternary arithmetic functions */
@@ -7810,15 +7801,15 @@ _decimal_exec(PyObject *m)
78107801

78117802
switch (cm->flag) {
78127803
case MPD_Float_operation:
7813-
base = PyTuple_Pack(2, state->DecimalException, PyExc_TypeError);
7804+
base = _PyTuple_FromPair(state->DecimalException, PyExc_TypeError);
78147805
break;
78157806
case MPD_Division_by_zero:
7816-
base = PyTuple_Pack(2, state->DecimalException,
7817-
PyExc_ZeroDivisionError);
7807+
base = _PyTuple_FromPair(state->DecimalException,
7808+
PyExc_ZeroDivisionError);
78187809
break;
78197810
case MPD_Overflow:
7820-
base = PyTuple_Pack(2, state->signal_map[INEXACT].ex,
7821-
state->signal_map[ROUNDED].ex);
7811+
base = _PyTuple_FromPair(state->signal_map[INEXACT].ex,
7812+
state->signal_map[ROUNDED].ex);
78227813
break;
78237814
case MPD_Underflow:
78247815
base = PyTuple_Pack(3, state->signal_map[INEXACT].ex,
@@ -7857,7 +7848,7 @@ _decimal_exec(PyObject *m)
78577848
for (cm = state->cond_map+1; cm->name != NULL; cm++) {
78587849
PyObject *base;
78597850
if (cm->flag == MPD_Division_undefined) {
7860-
base = PyTuple_Pack(2, state->signal_map[0].ex, PyExc_ZeroDivisionError);
7851+
base = _PyTuple_FromPair(state->signal_map[0].ex, PyExc_ZeroDivisionError);
78617852
}
78627853
else {
78637854
base = PyTuple_Pack(1, state->signal_map[0].ex);

Modules/_elementtree.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "Python.h"
1919
#include "pycore_dict.h" // _PyDict_CopyAsDict()
2020
#include "pycore_pyhash.h" // _Py_HashSecret
21+
#include "pycore_tuple.h" // _PyTuple_FromPair
2122
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
2223

2324
#include <stddef.h> // offsetof()
@@ -2594,7 +2595,7 @@ _elementtree__set_factories_impl(PyObject *module, PyObject *comment_factory,
25942595
return NULL;
25952596
}
25962597

2597-
old = PyTuple_Pack(2,
2598+
old = _PyTuple_FromPair(
25982599
st->comment_factory ? st->comment_factory : Py_None,
25992600
st->pi_factory ? st->pi_factory : Py_None);
26002601

@@ -2712,7 +2713,7 @@ treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
27122713
{
27132714
if (action != NULL) {
27142715
PyObject *res;
2715-
PyObject *event = PyTuple_Pack(2, action, node);
2716+
PyObject *event = _PyTuple_FromPair(action, node);
27162717
if (event == NULL)
27172718
return -1;
27182719
res = PyObject_CallOneArg(self->events_append, event);
@@ -2933,7 +2934,7 @@ treebuilder_handle_pi(TreeBuilderObject* self, PyObject* target, PyObject* text)
29332934
Py_XSETREF(self->last_for_tail, Py_NewRef(pi));
29342935
}
29352936
} else {
2936-
pi = PyTuple_Pack(2, target, text);
2937+
pi = _PyTuple_FromPair(target, text);
29372938
if (!pi) {
29382939
return NULL;
29392940
}
@@ -2957,7 +2958,7 @@ treebuilder_handle_start_ns(TreeBuilderObject* self, PyObject* prefix, PyObject*
29572958
PyObject* parcel;
29582959

29592960
if (self->events_append && self->start_ns_event_obj) {
2960-
parcel = PyTuple_Pack(2, prefix, uri);
2961+
parcel = _PyTuple_FromPair(prefix, uri);
29612962
if (!parcel) {
29622963
return NULL;
29632964
}

Modules/_json.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pycore_global_strings.h" // _Py_ID()
1515
#include "pycore_pyerrors.h" // _PyErr_FormatNote
1616
#include "pycore_runtime.h" // _PyRuntime
17+
#include "pycore_tuple.h" // _PyTuple_FromPair
1718
#include "pycore_unicodeobject.h" // _PyUnicode_CheckConsistency()
1819

1920
#include <stdbool.h> // bool
@@ -446,7 +447,6 @@ raise_stop_iteration(Py_ssize_t idx)
446447
static PyObject *
447448
_build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
448449
/* return (rval, idx) tuple, stealing reference to rval */
449-
PyObject *tpl;
450450
PyObject *pyidx;
451451
/*
452452
steal a reference to rval, returns (rval, idx)
@@ -459,15 +459,7 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
459459
Py_DECREF(rval);
460460
return NULL;
461461
}
462-
tpl = PyTuple_New(2);
463-
if (tpl == NULL) {
464-
Py_DECREF(pyidx);
465-
Py_DECREF(rval);
466-
return NULL;
467-
}
468-
PyTuple_SET_ITEM(tpl, 0, rval);
469-
PyTuple_SET_ITEM(tpl, 1, pyidx);
470-
return tpl;
462+
return _PyTuple_FromPairSteal(rval, pyidx);
471463
}
472464

473465
static PyObject *
@@ -810,11 +802,10 @@ _parse_object_unicode(PyScannerObject *s, PyObject *memo, PyObject *pystr, Py_ss
810802
goto bail;
811803

812804
if (has_pairs_hook) {
813-
PyObject *item = PyTuple_Pack(2, key, val);
805+
PyObject *item = _PyTuple_FromPairSteal(key, val);
806+
key = val = NULL;
814807
if (item == NULL)
815808
goto bail;
816-
Py_CLEAR(key);
817-
Py_CLEAR(val);
818809
if (PyList_Append(rval, item) == -1) {
819810
Py_DECREF(item);
820811
goto bail;

Modules/_operator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ itemgetter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy))
11941194
itemgetterobject *ig = itemgetterobject_CAST(op);
11951195
if (ig->nitems == 1)
11961196
return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item);
1197-
return PyTuple_Pack(2, Py_TYPE(ig), ig->item);
1197+
return _PyTuple_FromPair((PyObject *)Py_TYPE(ig), ig->item);
11981198
}
11991199

12001200
PyDoc_STRVAR(reduce_doc, "Return state information for pickling");

Modules/_pickle.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "pycore_setobject.h" // _PySet_NextEntry()
2222
#include "pycore_symtable.h" // _Py_Mangle()
2323
#include "pycore_sysmodule.h" // _PySys_GetSizeOf()
24+
#include "pycore_tuple.h" // _PyTuple_FromPair
2425
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
2526

2627
#include <stdlib.h> // strtol()
@@ -3767,7 +3768,7 @@ fix_imports(PickleState *st, PyObject **module_name, PyObject **global_name)
37673768
PyObject *key;
37683769
PyObject *item;
37693770

3770-
key = PyTuple_Pack(2, *module_name, *global_name);
3771+
key = _PyTuple_FromPair(*module_name, *global_name);
37713772
if (key == NULL)
37723773
return -1;
37733774
item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
@@ -3873,7 +3874,7 @@ save_global(PickleState *st, PicklerObject *self, PyObject *obj,
38733874
char pdata[5];
38743875
Py_ssize_t n;
38753876

3876-
extension_key = PyTuple_Pack(2, module_name, global_name);
3877+
extension_key = _PyTuple_FromPair(module_name, global_name);
38773878
if (extension_key == NULL) {
38783879
goto error;
38793880
}
@@ -5133,26 +5134,19 @@ static PyObject *
51335134
_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
51345135
/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
51355136
{
5136-
PyObject *reduce_value, *dict_args;
5137+
PyObject *dict_args;
51375138
PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
51385139
if (contents == NULL)
51395140
return NULL;
51405141

5141-
reduce_value = PyTuple_New(2);
5142-
if (reduce_value == NULL) {
5143-
Py_DECREF(contents);
5144-
return NULL;
5145-
}
51465142
dict_args = PyTuple_New(1);
51475143
if (dict_args == NULL) {
51485144
Py_DECREF(contents);
5149-
Py_DECREF(reduce_value);
51505145
return NULL;
51515146
}
51525147
PyTuple_SET_ITEM(dict_args, 0, contents);
5153-
PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
5154-
PyTuple_SET_ITEM(reduce_value, 1, dict_args);
5155-
return reduce_value;
5148+
5149+
return _PyTuple_FromPairSteal(Py_NewRef(&PyDict_Type), dict_args);
51565150
}
51575151

51585152
static PyMethodDef picklerproxy_methods[] = {
@@ -7310,7 +7304,7 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyTypeObject *cls,
73107304

73117305
/* Check if the global (i.e., a function or a class) was renamed
73127306
or moved to another module. */
7313-
key = PyTuple_Pack(2, module_name, global_name);
7307+
key = _PyTuple_FromPair(module_name, global_name);
73147308
if (key == NULL)
73157309
return NULL;
73167310
item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
@@ -7640,27 +7634,19 @@ static PyObject *
76407634
_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
76417635
/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
76427636
{
7643-
PyObject *reduce_value;
76447637
PyObject *constructor_args;
76457638
PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
76467639
if (contents == NULL)
76477640
return NULL;
76487641

7649-
reduce_value = PyTuple_New(2);
7650-
if (reduce_value == NULL) {
7651-
Py_DECREF(contents);
7652-
return NULL;
7653-
}
76547642
constructor_args = PyTuple_New(1);
76557643
if (constructor_args == NULL) {
76567644
Py_DECREF(contents);
7657-
Py_DECREF(reduce_value);
76587645
return NULL;
76597646
}
76607647
PyTuple_SET_ITEM(constructor_args, 0, contents);
7661-
PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
7662-
PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
7663-
return reduce_value;
7648+
7649+
return _PyTuple_FromPairSteal(Py_NewRef(&PyDict_Type), constructor_args);
76647650
}
76657651

76667652
static PyMethodDef unpicklerproxy_methods[] = {

Modules/_sre/sre.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const char copyright[] =
4343
#include "pycore_dict.h" // _PyDict_Next()
4444
#include "pycore_long.h" // _PyLong_GetZero()
4545
#include "pycore_moduleobject.h" // _PyModule_GetState()
46+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
4647
#include "pycore_unicodeobject.h" // _PyUnicode_Copy
4748
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
4849

@@ -2572,28 +2573,17 @@ _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group)
25722573
LOCAL(PyObject*)
25732574
_pair(Py_ssize_t i1, Py_ssize_t i2)
25742575
{
2575-
PyObject* pair;
2576-
PyObject* item;
2577-
2578-
pair = PyTuple_New(2);
2579-
if (!pair)
2576+
PyObject* item1 = PyLong_FromSsize_t(i1);
2577+
if (!item1) {
25802578
return NULL;
2579+
}
2580+
PyObject* item2 = PyLong_FromSsize_t(i2);
2581+
if(!item2) {
2582+
Py_DECREF(item1);
2583+
return NULL;
2584+
}
25812585

2582-
item = PyLong_FromSsize_t(i1);
2583-
if (!item)
2584-
goto error;
2585-
PyTuple_SET_ITEM(pair, 0, item);
2586-
2587-
item = PyLong_FromSsize_t(i2);
2588-
if (!item)
2589-
goto error;
2590-
PyTuple_SET_ITEM(pair, 1, item);
2591-
2592-
return pair;
2593-
2594-
error:
2595-
Py_DECREF(pair);
2596-
return NULL;
2586+
return _PyTuple_FromPairSteal(item1, item2);
25972587
}
25982588

25992589
/*[clinic input]

0 commit comments

Comments
 (0)