Skip to content

Commit 0bedc28

Browse files
authored
gh-99300: Use Py_NewRef() in Modules/itertoolsmodule.c (#99439)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Modules/itertoolsmodule.c.
1 parent abd004e commit 0bedc28

File tree

1 file changed

+31
-62
lines changed

1 file changed

+31
-62
lines changed

Modules/itertoolsmodule.c

+31-62
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc)
401401
gbo->tgtkey = NULL;
402402
gbo->currkey = NULL;
403403
gbo->currvalue = NULL;
404-
gbo->keyfunc = keyfunc;
405-
Py_INCREF(keyfunc);
404+
gbo->keyfunc = Py_NewRef(keyfunc);
406405
gbo->it = PyObject_GetIter(it);
407406
if (gbo->it == NULL) {
408407
Py_DECREF(gbo);
@@ -444,8 +443,7 @@ groupby_step(groupbyobject *gbo)
444443
return -1;
445444

446445
if (gbo->keyfunc == Py_None) {
447-
newkey = newvalue;
448-
Py_INCREF(newvalue);
446+
newkey = Py_NewRef(newvalue);
449447
} else {
450448
newkey = PyObject_CallOneArg(gbo->keyfunc, newvalue);
451449
if (newkey == NULL) {
@@ -625,10 +623,8 @@ _grouper_create(groupbyobject *parent, PyObject *tgtkey)
625623
igo = PyObject_GC_New(_grouperobject, &_grouper_type);
626624
if (igo == NULL)
627625
return NULL;
628-
igo->parent = (PyObject *)parent;
629-
Py_INCREF(parent);
630-
igo->tgtkey = tgtkey;
631-
Py_INCREF(tgtkey);
626+
igo->parent = Py_NewRef(parent);
627+
igo->tgtkey = Py_NewRef(tgtkey);
632628
parent->currgrouper = igo; /* borrowed reference */
633629

634630
PyObject_GC_Track(igo);
@@ -779,8 +775,7 @@ teedataobject_newinternal(PyObject *it)
779775
tdo->running = 0;
780776
tdo->numread = 0;
781777
tdo->nextlink = NULL;
782-
Py_INCREF(it);
783-
tdo->it = it;
778+
tdo->it = Py_NewRef(it);
784779
PyObject_GC_Track(tdo);
785780
return (PyObject *)tdo;
786781
}
@@ -790,8 +785,7 @@ teedataobject_jumplink(teedataobject *tdo)
790785
{
791786
if (tdo->nextlink == NULL)
792787
tdo->nextlink = teedataobject_newinternal(tdo->it);
793-
Py_XINCREF(tdo->nextlink);
794-
return tdo->nextlink;
788+
return Py_XNewRef(tdo->nextlink);
795789
}
796790

797791
static PyObject *
@@ -818,8 +812,7 @@ teedataobject_getitem(teedataobject *tdo, int i)
818812
tdo->numread++;
819813
tdo->values[i] = value;
820814
}
821-
Py_INCREF(value);
822-
return value;
815+
return Py_NewRef(value);
823816
}
824817

825818
static int
@@ -927,8 +920,7 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
927920
if (!Py_IS_TYPE(next, &teedataobject_type))
928921
goto err;
929922
assert(tdo->nextlink == NULL);
930-
Py_INCREF(next);
931-
tdo->nextlink = next;
923+
tdo->nextlink = Py_NewRef(next);
932924
}
933925
} else {
934926
if (next != Py_None)
@@ -1026,8 +1018,7 @@ tee_copy(teeobject *to, PyObject *Py_UNUSED(ignored))
10261018
newto = PyObject_GC_New(teeobject, &tee_type);
10271019
if (newto == NULL)
10281020
return NULL;
1029-
Py_INCREF(to->dataobj);
1030-
newto->dataobj = to->dataobj;
1021+
newto->dataobj = (teedataobject*)Py_NewRef(to->dataobj);
10311022
newto->index = to->index;
10321023
newto->weakreflist = NULL;
10331024
PyObject_GC_Track(newto);
@@ -1343,8 +1334,7 @@ cycle_next(cycleobject *lz)
13431334
lz->index++;
13441335
if (lz->index >= PyList_GET_SIZE(lz->saved))
13451336
lz->index = 0;
1346-
Py_INCREF(item);
1347-
return item;
1337+
return Py_NewRef(item);
13481338
}
13491339

13501340
static PyObject *
@@ -1480,8 +1470,7 @@ itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
14801470
Py_DECREF(it);
14811471
return NULL;
14821472
}
1483-
Py_INCREF(func);
1484-
lz->func = func;
1473+
lz->func = Py_NewRef(func);
14851474
lz->it = it;
14861475
lz->start = 0;
14871476

@@ -1643,8 +1632,7 @@ itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
16431632
Py_DECREF(it);
16441633
return NULL;
16451634
}
1646-
Py_INCREF(func);
1647-
lz->func = func;
1635+
lz->func = Py_NewRef(func);
16481636
lz->it = it;
16491637
lz->stop = 0;
16501638

@@ -1940,8 +1928,7 @@ islice_reduce(isliceobject *lz, PyObject *Py_UNUSED(ignored))
19401928
return Py_BuildValue("O(Nn)n", Py_TYPE(lz), empty_it, 0, 0);
19411929
}
19421930
if (lz->stop == -1) {
1943-
stop = Py_None;
1944-
Py_INCREF(stop);
1931+
stop = Py_NewRef(Py_None);
19451932
} else {
19461933
stop = PyLong_FromSsize_t(lz->stop);
19471934
if (stop == NULL)
@@ -2062,8 +2049,7 @@ itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
20622049
Py_DECREF(it);
20632050
return NULL;
20642051
}
2065-
Py_INCREF(func);
2066-
lz->func = func;
2052+
lz->func = Py_NewRef(func);
20672053
lz->it = it;
20682054

20692055
return (PyObject *)lz;
@@ -2595,8 +2581,7 @@ product_next(productobject *lz)
25952581
goto empty;
25962582
}
25972583

2598-
Py_INCREF(result);
2599-
return result;
2584+
return Py_NewRef(result);
26002585

26012586
empty:
26022587
lz->stopped = 1;
@@ -2926,8 +2911,7 @@ combinations_next(combinationsobject *co)
29262911
}
29272912
}
29282913

2929-
Py_INCREF(result);
2930-
return result;
2914+
return Py_NewRef(result);
29312915

29322916
empty:
29332917
co->stopped = 1;
@@ -3257,8 +3241,7 @@ cwr_next(cwrobject *co)
32573241
}
32583242
}
32593243

3260-
Py_INCREF(result);
3261-
return result;
3244+
return Py_NewRef(result);
32623245

32633246
empty:
32643247
co->stopped = 1;
@@ -3615,8 +3598,7 @@ permutations_next(permutationsobject *po)
36153598
if (i < 0)
36163599
goto empty;
36173600
}
3618-
Py_INCREF(result);
3619-
return result;
3601+
return Py_NewRef(result);
36203602

36213603
empty:
36223604
po->stopped = 1;
@@ -3819,13 +3801,11 @@ itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
38193801
}
38203802

38213803
if (binop != Py_None) {
3822-
Py_XINCREF(binop);
3823-
lz->binop = binop;
3804+
lz->binop = Py_XNewRef(binop);
38243805
}
38253806
lz->total = NULL;
38263807
lz->it = it;
3827-
Py_XINCREF(initial);
3828-
lz->initial = initial;
3808+
lz->initial = Py_XNewRef(initial);
38293809
return (PyObject *)lz;
38303810
}
38313811

@@ -3857,18 +3837,15 @@ accumulate_next(accumulateobject *lz)
38573837

38583838
if (lz->initial != Py_None) {
38593839
lz->total = lz->initial;
3860-
Py_INCREF(Py_None);
3861-
lz->initial = Py_None;
3862-
Py_INCREF(lz->total);
3863-
return lz->total;
3840+
lz->initial = Py_NewRef(Py_None);
3841+
return Py_NewRef(lz->total);
38643842
}
38653843
val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it);
38663844
if (val == NULL)
38673845
return NULL;
38683846

38693847
if (lz->total == NULL) {
3870-
Py_INCREF(val);
3871-
lz->total = val;
3848+
lz->total = Py_NewRef(val);
38723849
return lz->total;
38733850
}
38743851

@@ -4186,8 +4163,7 @@ itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
41864163
Py_DECREF(it);
41874164
return NULL;
41884165
}
4189-
Py_INCREF(func);
4190-
lz->func = func;
4166+
lz->func = Py_NewRef(func);
41914167
lz->it = it;
41924168

41934169
return (PyObject *)lz;
@@ -4581,8 +4557,7 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
45814557
ro = (repeatobject *)type->tp_alloc(type, 0);
45824558
if (ro == NULL)
45834559
return NULL;
4584-
Py_INCREF(element);
4585-
ro->element = element;
4560+
ro->element = Py_NewRef(element);
45864561
ro->cnt = cnt;
45874562
return (PyObject *)ro;
45884563
}
@@ -4609,8 +4584,7 @@ repeat_next(repeatobject *ro)
46094584
return NULL;
46104585
if (ro->cnt > 0)
46114586
ro->cnt--;
4612-
Py_INCREF(ro->element);
4613-
return ro->element;
4587+
return Py_NewRef(ro->element);
46144588
}
46154589

46164590
static PyObject *
@@ -4782,8 +4756,7 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
47824756
lz->tuplesize = tuplesize;
47834757
lz->numactive = tuplesize;
47844758
lz->result = result;
4785-
Py_INCREF(fillvalue);
4786-
lz->fillvalue = fillvalue;
4759+
lz->fillvalue = Py_NewRef(fillvalue);
47874760
return (PyObject *)lz;
47884761
}
47894762

@@ -4825,8 +4798,7 @@ zip_longest_next(ziplongestobject *lz)
48254798
for (i=0 ; i < tuplesize ; i++) {
48264799
it = PyTuple_GET_ITEM(lz->ittuple, i);
48274800
if (it == NULL) {
4828-
Py_INCREF(lz->fillvalue);
4829-
item = lz->fillvalue;
4801+
item = Py_NewRef(lz->fillvalue);
48304802
} else {
48314803
item = PyIter_Next(it);
48324804
if (item == NULL) {
@@ -4836,8 +4808,7 @@ zip_longest_next(ziplongestobject *lz)
48364808
Py_DECREF(result);
48374809
return NULL;
48384810
} else {
4839-
Py_INCREF(lz->fillvalue);
4840-
item = lz->fillvalue;
4811+
item = Py_NewRef(lz->fillvalue);
48414812
PyTuple_SET_ITEM(lz->ittuple, i, NULL);
48424813
Py_DECREF(it);
48434814
}
@@ -4859,8 +4830,7 @@ zip_longest_next(ziplongestobject *lz)
48594830
for (i=0 ; i < tuplesize ; i++) {
48604831
it = PyTuple_GET_ITEM(lz->ittuple, i);
48614832
if (it == NULL) {
4862-
Py_INCREF(lz->fillvalue);
4863-
item = lz->fillvalue;
4833+
item = Py_NewRef(lz->fillvalue);
48644834
} else {
48654835
item = PyIter_Next(it);
48664836
if (item == NULL) {
@@ -4870,8 +4840,7 @@ zip_longest_next(ziplongestobject *lz)
48704840
Py_DECREF(result);
48714841
return NULL;
48724842
} else {
4873-
Py_INCREF(lz->fillvalue);
4874-
item = lz->fillvalue;
4843+
item = Py_NewRef(lz->fillvalue);
48754844
PyTuple_SET_ITEM(lz->ittuple, i, NULL);
48764845
Py_DECREF(it);
48774846
}

0 commit comments

Comments
 (0)