Skip to content

Commit dbf8613

Browse files
authored
gh-99300: Use Py_NewRef() in Objects/listobject.c (#99336)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Objects/listobject.c.
1 parent 694cdb2 commit dbf8613

File tree

1 file changed

+20
-40
lines changed

1 file changed

+20
-40
lines changed

Diff for: Objects/listobject.c

+20-40
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
299299
items = self->ob_item;
300300
for (i = n; --i >= where; )
301301
items[i+1] = items[i];
302-
Py_INCREF(v);
303-
items[where] = v;
302+
items[where] = Py_NewRef(v);
304303
return 0;
305304
}
306305

@@ -332,8 +331,7 @@ int
332331
PyList_Append(PyObject *op, PyObject *newitem)
333332
{
334333
if (PyList_Check(op) && (newitem != NULL)) {
335-
Py_INCREF(newitem);
336-
return _PyList_AppendTakeRef((PyListObject *)op, newitem);
334+
return _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
337335
}
338336
PyErr_BadInternalCall();
339337
return -1;
@@ -461,8 +459,7 @@ list_item(PyListObject *a, Py_ssize_t i)
461459
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
462460
return NULL;
463461
}
464-
Py_INCREF(a->ob_item[i]);
465-
return a->ob_item[i];
462+
return Py_NewRef(a->ob_item[i]);
466463
}
467464

468465
static PyObject *
@@ -483,8 +480,7 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
483480
dest = np->ob_item;
484481
for (i = 0; i < len; i++) {
485482
PyObject *v = src[i];
486-
Py_INCREF(v);
487-
dest[i] = v;
483+
dest[i] = Py_NewRef(v);
488484
}
489485
Py_SET_SIZE(np, len);
490486
return (PyObject *)np;
@@ -539,15 +535,13 @@ list_concat(PyListObject *a, PyObject *bb)
539535
dest = np->ob_item;
540536
for (i = 0; i < Py_SIZE(a); i++) {
541537
PyObject *v = src[i];
542-
Py_INCREF(v);
543-
dest[i] = v;
538+
dest[i] = Py_NewRef(v);
544539
}
545540
src = b->ob_item;
546541
dest = np->ob_item + Py_SIZE(a);
547542
for (i = 0; i < Py_SIZE(b); i++) {
548543
PyObject *v = src[i];
549-
Py_INCREF(v);
550-
dest[i] = v;
544+
dest[i] = Py_NewRef(v);
551545
}
552546
Py_SET_SIZE(np, size);
553547
return (PyObject *)np;
@@ -716,8 +710,7 @@ list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
716710
}
717711
for (k = 0; k < n; k++, ilow++) {
718712
PyObject *w = vitem[k];
719-
Py_XINCREF(w);
720-
item[ilow] = w;
713+
item[ilow] = Py_XNewRef(w);
721714
}
722715
for (k = norig - 1; k >= 0; --k)
723716
Py_XDECREF(recycle[k]);
@@ -745,14 +738,12 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
745738
{
746739
Py_ssize_t input_size = PyList_GET_SIZE(self);
747740
if (input_size == 0 || n == 1) {
748-
Py_INCREF(self);
749-
return (PyObject *)self;
741+
return Py_NewRef(self);
750742
}
751743

752744
if (n < 1) {
753745
(void)_list_clear(self);
754-
Py_INCREF(self);
755-
return (PyObject *)self;
746+
return Py_NewRef(self);
756747
}
757748

758749
if (input_size > PY_SSIZE_T_MAX / n) {
@@ -770,8 +761,7 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
770761
_Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
771762
sizeof(PyObject *)*input_size);
772763

773-
Py_INCREF(self);
774-
return (PyObject *)self;
764+
return Py_NewRef(self);
775765
}
776766

777767
static int
@@ -784,8 +774,7 @@ list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
784774
}
785775
if (v == NULL)
786776
return list_ass_slice(a, i, i+1, v);
787-
Py_INCREF(v);
788-
Py_SETREF(a->ob_item[i], v);
777+
Py_SETREF(a->ob_item[i], Py_NewRef(v));
789778
return 0;
790779
}
791780

@@ -913,8 +902,7 @@ list_extend(PyListObject *self, PyObject *iterable)
913902
dest = self->ob_item + m;
914903
for (i = 0; i < n; i++) {
915904
PyObject *o = src[i];
916-
Py_INCREF(o);
917-
dest[i] = o;
905+
dest[i] = Py_NewRef(o);
918906
}
919907
Py_DECREF(iterable);
920908
Py_RETURN_NONE;
@@ -1002,8 +990,7 @@ list_inplace_concat(PyListObject *self, PyObject *other)
1002990
if (result == NULL)
1003991
return result;
1004992
Py_DECREF(result);
1005-
Py_INCREF(self);
1006-
return (PyObject *)self;
993+
return Py_NewRef(self);
1007994
}
1008995

1009996
/*[clinic input]
@@ -2512,8 +2499,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
25122499
}
25132500
PyMem_Free(final_ob_item);
25142501
}
2515-
Py_XINCREF(result);
2516-
return result;
2502+
return Py_XNewRef(result);
25172503
}
25182504
#undef IFLT
25192505
#undef ISLT
@@ -2901,8 +2887,7 @@ list_subscript(PyListObject* self, PyObject* item)
29012887
dest = ((PyListObject *)result)->ob_item;
29022888
for (cur = start, i = 0; i < slicelength;
29032889
cur += (size_t)step, i++) {
2904-
it = src[cur];
2905-
Py_INCREF(it);
2890+
it = Py_NewRef(src[cur]);
29062891
dest[i] = it;
29072892
}
29082893
Py_SET_SIZE(result, slicelength);
@@ -3057,8 +3042,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
30573042
for (cur = start, i = 0; i < slicelength;
30583043
cur += (size_t)step, i++) {
30593044
garbage[i] = selfitems[cur];
3060-
ins = seqitems[i];
3061-
Py_INCREF(ins);
3045+
ins = Py_NewRef(seqitems[i]);
30623046
selfitems[cur] = ins;
30633047
}
30643048

@@ -3199,8 +3183,7 @@ list_iter(PyObject *seq)
31993183
if (it == NULL)
32003184
return NULL;
32013185
it->it_index = 0;
3202-
Py_INCREF(seq);
3203-
it->it_seq = (PyListObject *)seq;
3186+
it->it_seq = (PyListObject *)Py_NewRef(seq);
32043187
_PyObject_GC_TRACK(it);
32053188
return (PyObject *)it;
32063189
}
@@ -3235,8 +3218,7 @@ listiter_next(_PyListIterObject *it)
32353218
if (it->it_index < PyList_GET_SIZE(seq)) {
32363219
item = PyList_GET_ITEM(seq, it->it_index);
32373220
++it->it_index;
3238-
Py_INCREF(item);
3239-
return item;
3221+
return Py_NewRef(item);
32403222
}
32413223

32423224
it->it_seq = NULL;
@@ -3350,8 +3332,7 @@ list___reversed___impl(PyListObject *self)
33503332
return NULL;
33513333
assert(PyList_Check(self));
33523334
it->it_index = PyList_GET_SIZE(self) - 1;
3353-
Py_INCREF(self);
3354-
it->it_seq = self;
3335+
it->it_seq = (PyListObject*)Py_NewRef(self);
33553336
PyObject_GC_Track(it);
33563337
return (PyObject *)it;
33573338
}
@@ -3389,8 +3370,7 @@ listreviter_next(listreviterobject *it)
33893370
if (index>=0 && index < PyList_GET_SIZE(seq)) {
33903371
item = PyList_GET_ITEM(seq, index);
33913372
it->it_index--;
3392-
Py_INCREF(item);
3393-
return item;
3373+
return Py_NewRef(item);
33943374
}
33953375
it->it_index = -1;
33963376
it->it_seq = NULL;

0 commit comments

Comments
 (0)