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.
This commit is contained in:
Victor Stinner 2022-11-13 16:04:22 +01:00 committed by GitHub
parent abd004e355
commit 0bedc28d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -401,8 +401,7 @@ itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc)
gbo->tgtkey = NULL; gbo->tgtkey = NULL;
gbo->currkey = NULL; gbo->currkey = NULL;
gbo->currvalue = NULL; gbo->currvalue = NULL;
gbo->keyfunc = keyfunc; gbo->keyfunc = Py_NewRef(keyfunc);
Py_INCREF(keyfunc);
gbo->it = PyObject_GetIter(it); gbo->it = PyObject_GetIter(it);
if (gbo->it == NULL) { if (gbo->it == NULL) {
Py_DECREF(gbo); Py_DECREF(gbo);
@ -444,8 +443,7 @@ groupby_step(groupbyobject *gbo)
return -1; return -1;
if (gbo->keyfunc == Py_None) { if (gbo->keyfunc == Py_None) {
newkey = newvalue; newkey = Py_NewRef(newvalue);
Py_INCREF(newvalue);
} else { } else {
newkey = PyObject_CallOneArg(gbo->keyfunc, newvalue); newkey = PyObject_CallOneArg(gbo->keyfunc, newvalue);
if (newkey == NULL) { if (newkey == NULL) {
@ -625,10 +623,8 @@ _grouper_create(groupbyobject *parent, PyObject *tgtkey)
igo = PyObject_GC_New(_grouperobject, &_grouper_type); igo = PyObject_GC_New(_grouperobject, &_grouper_type);
if (igo == NULL) if (igo == NULL)
return NULL; return NULL;
igo->parent = (PyObject *)parent; igo->parent = Py_NewRef(parent);
Py_INCREF(parent); igo->tgtkey = Py_NewRef(tgtkey);
igo->tgtkey = tgtkey;
Py_INCREF(tgtkey);
parent->currgrouper = igo; /* borrowed reference */ parent->currgrouper = igo; /* borrowed reference */
PyObject_GC_Track(igo); PyObject_GC_Track(igo);
@ -779,8 +775,7 @@ teedataobject_newinternal(PyObject *it)
tdo->running = 0; tdo->running = 0;
tdo->numread = 0; tdo->numread = 0;
tdo->nextlink = NULL; tdo->nextlink = NULL;
Py_INCREF(it); tdo->it = Py_NewRef(it);
tdo->it = it;
PyObject_GC_Track(tdo); PyObject_GC_Track(tdo);
return (PyObject *)tdo; return (PyObject *)tdo;
} }
@ -790,8 +785,7 @@ teedataobject_jumplink(teedataobject *tdo)
{ {
if (tdo->nextlink == NULL) if (tdo->nextlink == NULL)
tdo->nextlink = teedataobject_newinternal(tdo->it); tdo->nextlink = teedataobject_newinternal(tdo->it);
Py_XINCREF(tdo->nextlink); return Py_XNewRef(tdo->nextlink);
return tdo->nextlink;
} }
static PyObject * static PyObject *
@ -818,8 +812,7 @@ teedataobject_getitem(teedataobject *tdo, int i)
tdo->numread++; tdo->numread++;
tdo->values[i] = value; tdo->values[i] = value;
} }
Py_INCREF(value); return Py_NewRef(value);
return value;
} }
static int static int
@ -927,8 +920,7 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
if (!Py_IS_TYPE(next, &teedataobject_type)) if (!Py_IS_TYPE(next, &teedataobject_type))
goto err; goto err;
assert(tdo->nextlink == NULL); assert(tdo->nextlink == NULL);
Py_INCREF(next); tdo->nextlink = Py_NewRef(next);
tdo->nextlink = next;
} }
} else { } else {
if (next != Py_None) if (next != Py_None)
@ -1026,8 +1018,7 @@ tee_copy(teeobject *to, PyObject *Py_UNUSED(ignored))
newto = PyObject_GC_New(teeobject, &tee_type); newto = PyObject_GC_New(teeobject, &tee_type);
if (newto == NULL) if (newto == NULL)
return NULL; return NULL;
Py_INCREF(to->dataobj); newto->dataobj = (teedataobject*)Py_NewRef(to->dataobj);
newto->dataobj = to->dataobj;
newto->index = to->index; newto->index = to->index;
newto->weakreflist = NULL; newto->weakreflist = NULL;
PyObject_GC_Track(newto); PyObject_GC_Track(newto);
@ -1343,8 +1334,7 @@ cycle_next(cycleobject *lz)
lz->index++; lz->index++;
if (lz->index >= PyList_GET_SIZE(lz->saved)) if (lz->index >= PyList_GET_SIZE(lz->saved))
lz->index = 0; lz->index = 0;
Py_INCREF(item); return Py_NewRef(item);
return item;
} }
static PyObject * static PyObject *
@ -1480,8 +1470,7 @@ itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
Py_DECREF(it); Py_DECREF(it);
return NULL; return NULL;
} }
Py_INCREF(func); lz->func = Py_NewRef(func);
lz->func = func;
lz->it = it; lz->it = it;
lz->start = 0; lz->start = 0;
@ -1643,8 +1632,7 @@ itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
Py_DECREF(it); Py_DECREF(it);
return NULL; return NULL;
} }
Py_INCREF(func); lz->func = Py_NewRef(func);
lz->func = func;
lz->it = it; lz->it = it;
lz->stop = 0; lz->stop = 0;
@ -1940,8 +1928,7 @@ islice_reduce(isliceobject *lz, PyObject *Py_UNUSED(ignored))
return Py_BuildValue("O(Nn)n", Py_TYPE(lz), empty_it, 0, 0); return Py_BuildValue("O(Nn)n", Py_TYPE(lz), empty_it, 0, 0);
} }
if (lz->stop == -1) { if (lz->stop == -1) {
stop = Py_None; stop = Py_NewRef(Py_None);
Py_INCREF(stop);
} else { } else {
stop = PyLong_FromSsize_t(lz->stop); stop = PyLong_FromSsize_t(lz->stop);
if (stop == NULL) if (stop == NULL)
@ -2062,8 +2049,7 @@ itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
Py_DECREF(it); Py_DECREF(it);
return NULL; return NULL;
} }
Py_INCREF(func); lz->func = Py_NewRef(func);
lz->func = func;
lz->it = it; lz->it = it;
return (PyObject *)lz; return (PyObject *)lz;
@ -2595,8 +2581,7 @@ product_next(productobject *lz)
goto empty; goto empty;
} }
Py_INCREF(result); return Py_NewRef(result);
return result;
empty: empty:
lz->stopped = 1; lz->stopped = 1;
@ -2926,8 +2911,7 @@ combinations_next(combinationsobject *co)
} }
} }
Py_INCREF(result); return Py_NewRef(result);
return result;
empty: empty:
co->stopped = 1; co->stopped = 1;
@ -3257,8 +3241,7 @@ cwr_next(cwrobject *co)
} }
} }
Py_INCREF(result); return Py_NewRef(result);
return result;
empty: empty:
co->stopped = 1; co->stopped = 1;
@ -3615,8 +3598,7 @@ permutations_next(permutationsobject *po)
if (i < 0) if (i < 0)
goto empty; goto empty;
} }
Py_INCREF(result); return Py_NewRef(result);
return result;
empty: empty:
po->stopped = 1; po->stopped = 1;
@ -3819,13 +3801,11 @@ itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
} }
if (binop != Py_None) { if (binop != Py_None) {
Py_XINCREF(binop); lz->binop = Py_XNewRef(binop);
lz->binop = binop;
} }
lz->total = NULL; lz->total = NULL;
lz->it = it; lz->it = it;
Py_XINCREF(initial); lz->initial = Py_XNewRef(initial);
lz->initial = initial;
return (PyObject *)lz; return (PyObject *)lz;
} }
@ -3857,18 +3837,15 @@ accumulate_next(accumulateobject *lz)
if (lz->initial != Py_None) { if (lz->initial != Py_None) {
lz->total = lz->initial; lz->total = lz->initial;
Py_INCREF(Py_None); lz->initial = Py_NewRef(Py_None);
lz->initial = Py_None; return Py_NewRef(lz->total);
Py_INCREF(lz->total);
return lz->total;
} }
val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it); val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it);
if (val == NULL) if (val == NULL)
return NULL; return NULL;
if (lz->total == NULL) { if (lz->total == NULL) {
Py_INCREF(val); lz->total = Py_NewRef(val);
lz->total = val;
return lz->total; return lz->total;
} }
@ -4186,8 +4163,7 @@ itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
Py_DECREF(it); Py_DECREF(it);
return NULL; return NULL;
} }
Py_INCREF(func); lz->func = Py_NewRef(func);
lz->func = func;
lz->it = it; lz->it = it;
return (PyObject *)lz; return (PyObject *)lz;
@ -4581,8 +4557,7 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ro = (repeatobject *)type->tp_alloc(type, 0); ro = (repeatobject *)type->tp_alloc(type, 0);
if (ro == NULL) if (ro == NULL)
return NULL; return NULL;
Py_INCREF(element); ro->element = Py_NewRef(element);
ro->element = element;
ro->cnt = cnt; ro->cnt = cnt;
return (PyObject *)ro; return (PyObject *)ro;
} }
@ -4609,8 +4584,7 @@ repeat_next(repeatobject *ro)
return NULL; return NULL;
if (ro->cnt > 0) if (ro->cnt > 0)
ro->cnt--; ro->cnt--;
Py_INCREF(ro->element); return Py_NewRef(ro->element);
return ro->element;
} }
static PyObject * static PyObject *
@ -4782,8 +4756,7 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
lz->tuplesize = tuplesize; lz->tuplesize = tuplesize;
lz->numactive = tuplesize; lz->numactive = tuplesize;
lz->result = result; lz->result = result;
Py_INCREF(fillvalue); lz->fillvalue = Py_NewRef(fillvalue);
lz->fillvalue = fillvalue;
return (PyObject *)lz; return (PyObject *)lz;
} }
@ -4825,8 +4798,7 @@ zip_longest_next(ziplongestobject *lz)
for (i=0 ; i < tuplesize ; i++) { for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i); it = PyTuple_GET_ITEM(lz->ittuple, i);
if (it == NULL) { if (it == NULL) {
Py_INCREF(lz->fillvalue); item = Py_NewRef(lz->fillvalue);
item = lz->fillvalue;
} else { } else {
item = PyIter_Next(it); item = PyIter_Next(it);
if (item == NULL) { if (item == NULL) {
@ -4836,8 +4808,7 @@ zip_longest_next(ziplongestobject *lz)
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
} else { } else {
Py_INCREF(lz->fillvalue); item = Py_NewRef(lz->fillvalue);
item = lz->fillvalue;
PyTuple_SET_ITEM(lz->ittuple, i, NULL); PyTuple_SET_ITEM(lz->ittuple, i, NULL);
Py_DECREF(it); Py_DECREF(it);
} }
@ -4859,8 +4830,7 @@ zip_longest_next(ziplongestobject *lz)
for (i=0 ; i < tuplesize ; i++) { for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i); it = PyTuple_GET_ITEM(lz->ittuple, i);
if (it == NULL) { if (it == NULL) {
Py_INCREF(lz->fillvalue); item = Py_NewRef(lz->fillvalue);
item = lz->fillvalue;
} else { } else {
item = PyIter_Next(it); item = PyIter_Next(it);
if (item == NULL) { if (item == NULL) {
@ -4870,8 +4840,7 @@ zip_longest_next(ziplongestobject *lz)
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
} else { } else {
Py_INCREF(lz->fillvalue); item = Py_NewRef(lz->fillvalue);
item = lz->fillvalue;
PyTuple_SET_ITEM(lz->ittuple, i, NULL); PyTuple_SET_ITEM(lz->ittuple, i, NULL);
Py_DECREF(it); Py_DECREF(it);
} }