Skip to content

gh-99300: Use Py_NewRef() in Objects/listobject.c #99336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
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
60 changes: 20 additions & 40 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
items = self->ob_item;
for (i = n; --i >= where; )
items[i+1] = items[i];
Py_INCREF(v);
items[where] = v;
items[where] = Py_NewRef(v);
return 0;
}

Expand Down Expand Up @@ -332,8 +331,7 @@ int
PyList_Append(PyObject *op, PyObject *newitem)
{
if (PyList_Check(op) && (newitem != NULL)) {
Py_INCREF(newitem);
return _PyList_AppendTakeRef((PyListObject *)op, newitem);
return _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
}
PyErr_BadInternalCall();
return -1;
Expand Down Expand Up @@ -461,8 +459,7 @@ list_item(PyListObject *a, Py_ssize_t i)
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
return NULL;
}
Py_INCREF(a->ob_item[i]);
return a->ob_item[i];
return Py_NewRef(a->ob_item[i]);
}

static PyObject *
Expand All @@ -483,8 +480,7 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
dest = np->ob_item;
for (i = 0; i < len; i++) {
PyObject *v = src[i];
Py_INCREF(v);
dest[i] = v;
dest[i] = Py_NewRef(v);
}
Py_SET_SIZE(np, len);
return (PyObject *)np;
Expand Down Expand Up @@ -539,15 +535,13 @@ list_concat(PyListObject *a, PyObject *bb)
dest = np->ob_item;
for (i = 0; i < Py_SIZE(a); i++) {
PyObject *v = src[i];
Py_INCREF(v);
dest[i] = v;
dest[i] = Py_NewRef(v);
}
src = b->ob_item;
dest = np->ob_item + Py_SIZE(a);
for (i = 0; i < Py_SIZE(b); i++) {
PyObject *v = src[i];
Py_INCREF(v);
dest[i] = v;
dest[i] = Py_NewRef(v);
}
Py_SET_SIZE(np, size);
return (PyObject *)np;
Expand Down Expand Up @@ -716,8 +710,7 @@ list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
}
for (k = 0; k < n; k++, ilow++) {
PyObject *w = vitem[k];
Py_XINCREF(w);
item[ilow] = w;
item[ilow] = Py_XNewRef(w);
}
for (k = norig - 1; k >= 0; --k)
Py_XDECREF(recycle[k]);
Expand Down Expand Up @@ -745,14 +738,12 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
{
Py_ssize_t input_size = PyList_GET_SIZE(self);
if (input_size == 0 || n == 1) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}

if (n < 1) {
(void)_list_clear(self);
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}

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

Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}

static int
Expand All @@ -784,8 +774,7 @@ list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
}
if (v == NULL)
return list_ass_slice(a, i, i+1, v);
Py_INCREF(v);
Py_SETREF(a->ob_item[i], v);
Py_SETREF(a->ob_item[i], Py_NewRef(v));
return 0;
}

Expand Down Expand Up @@ -913,8 +902,7 @@ list_extend(PyListObject *self, PyObject *iterable)
dest = self->ob_item + m;
for (i = 0; i < n; i++) {
PyObject *o = src[i];
Py_INCREF(o);
dest[i] = o;
dest[i] = Py_NewRef(o);
}
Py_DECREF(iterable);
Py_RETURN_NONE;
Expand Down Expand Up @@ -1002,8 +990,7 @@ list_inplace_concat(PyListObject *self, PyObject *other)
if (result == NULL)
return result;
Py_DECREF(result);
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}

/*[clinic input]
Expand Down Expand Up @@ -2512,8 +2499,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
}
PyMem_Free(final_ob_item);
}
Py_XINCREF(result);
return result;
return Py_XNewRef(result);
}
#undef IFLT
#undef ISLT
Expand Down Expand Up @@ -2901,8 +2887,7 @@ list_subscript(PyListObject* self, PyObject* item)
dest = ((PyListObject *)result)->ob_item;
for (cur = start, i = 0; i < slicelength;
cur += (size_t)step, i++) {
it = src[cur];
Py_INCREF(it);
it = Py_NewRef(src[cur]);
dest[i] = it;
}
Py_SET_SIZE(result, slicelength);
Expand Down Expand Up @@ -3057,8 +3042,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
for (cur = start, i = 0; i < slicelength;
cur += (size_t)step, i++) {
garbage[i] = selfitems[cur];
ins = seqitems[i];
Py_INCREF(ins);
ins = Py_NewRef(seqitems[i]);
selfitems[cur] = ins;
}

Expand Down Expand Up @@ -3199,8 +3183,7 @@ list_iter(PyObject *seq)
if (it == NULL)
return NULL;
it->it_index = 0;
Py_INCREF(seq);
it->it_seq = (PyListObject *)seq;
it->it_seq = (PyListObject *)Py_NewRef(seq);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}
Expand Down Expand Up @@ -3235,8 +3218,7 @@ listiter_next(_PyListIterObject *it)
if (it->it_index < PyList_GET_SIZE(seq)) {
item = PyList_GET_ITEM(seq, it->it_index);
++it->it_index;
Py_INCREF(item);
return item;
return Py_NewRef(item);
}

it->it_seq = NULL;
Expand Down Expand Up @@ -3350,8 +3332,7 @@ list___reversed___impl(PyListObject *self)
return NULL;
assert(PyList_Check(self));
it->it_index = PyList_GET_SIZE(self) - 1;
Py_INCREF(self);
it->it_seq = self;
it->it_seq = (PyListObject*)Py_NewRef(self);
PyObject_GC_Track(it);
return (PyObject *)it;
}
Expand Down Expand Up @@ -3389,8 +3370,7 @@ listreviter_next(listreviterobject *it)
if (index>=0 && index < PyList_GET_SIZE(seq)) {
item = PyList_GET_ITEM(seq, index);
it->it_index--;
Py_INCREF(item);
return item;
return Py_NewRef(item);
}
it->it_index = -1;
it->it_seq = NULL;
Expand Down