gh-99300: Use Py_NewRef() in Objects/ directory (#99351)

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
This commit is contained in:
Victor Stinner 2022-11-10 23:40:31 +01:00 committed by GitHub
parent 584e55bd34
commit 1960eb005e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 188 deletions

View file

@ -61,8 +61,7 @@ tuple_alloc(Py_ssize_t size)
static inline PyObject *
tuple_get_empty(void)
{
Py_INCREF(&_Py_SINGLETON(tuple_empty));
return (PyObject *)&_Py_SINGLETON(tuple_empty);
return Py_NewRef(&_Py_SINGLETON(tuple_empty));
}
PyObject *
@ -171,8 +170,7 @@ PyTuple_Pack(Py_ssize_t n, ...)
items = result->ob_item;
for (i = 0; i < n; i++) {
o = va_arg(vargs, PyObject *);
Py_INCREF(o);
items[i] = o;
items[i] = Py_NewRef(o);
}
va_end(vargs);
_PyObject_GC_TRACK(result);
@ -367,8 +365,7 @@ tupleitem(PyTupleObject *a, Py_ssize_t i)
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
return NULL;
}
Py_INCREF(a->ob_item[i]);
return a->ob_item[i];
return Py_NewRef(a->ob_item[i]);
}
PyObject *
@ -385,8 +382,7 @@ _PyTuple_FromArray(PyObject *const *src, Py_ssize_t n)
PyObject **dst = tuple->ob_item;
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = src[i];
Py_INCREF(item);
dst[i] = item;
dst[i] = Py_NewRef(item);
}
_PyObject_GC_TRACK(tuple);
return (PyObject *)tuple;
@ -425,8 +421,7 @@ tupleslice(PyTupleObject *a, Py_ssize_t ilow,
if (ihigh < ilow)
ihigh = ilow;
if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
Py_INCREF(a);
return (PyObject *)a;
return Py_NewRef(a);
}
return _PyTuple_FromArray(a->ob_item + ilow, ihigh - ilow);
}
@ -449,8 +444,7 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
PyObject **src, **dest;
PyTupleObject *np;
if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
Py_INCREF(bb);
return bb;
return Py_NewRef(bb);
}
if (!PyTuple_Check(bb)) {
PyErr_Format(PyExc_TypeError,
@ -461,8 +455,7 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
PyTupleObject *b = (PyTupleObject *)bb;
if (Py_SIZE(b) == 0 && PyTuple_CheckExact(a)) {
Py_INCREF(a);
return (PyObject *)a;
return Py_NewRef(a);
}
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
size = Py_SIZE(a) + Py_SIZE(b);
@ -478,15 +471,13 @@ tupleconcat(PyTupleObject *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);
}
_PyObject_GC_TRACK(np);
return (PyObject *)np;
@ -500,8 +491,7 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
if (PyTuple_CheckExact(a)) {
/* Since tuples are immutable, we can return a shared
copy in this case */
Py_INCREF(a);
return (PyObject *)a;
return Py_NewRef(a);
}
}
if (input_size == 0 || n <= 0) {
@ -747,8 +737,7 @@ tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
}
for (i = 0; i < n; i++) {
item = PyTuple_GET_ITEM(tmp, i);
Py_INCREF(item);
PyTuple_SET_ITEM(newobj, i, item);
PyTuple_SET_ITEM(newobj, i, Py_NewRef(item));
}
Py_DECREF(tmp);
@ -799,8 +788,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
else if (start == 0 && step == 1 &&
slicelength == PyTuple_GET_SIZE(self) &&
PyTuple_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
else {
PyTupleObject* result = tuple_alloc(slicelength);
@ -810,8 +798,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
dest = result->ob_item;
for (cur = start, i = 0; i < slicelength;
cur += step, i++) {
it = src[cur];
Py_INCREF(it);
it = Py_NewRef(src[cur]);
dest[i] = it;
}
@ -1044,8 +1031,7 @@ tupleiter_next(tupleiterobject *it)
if (it->it_index < PyTuple_GET_SIZE(seq)) {
item = PyTuple_GET_ITEM(seq, it->it_index);
++it->it_index;
Py_INCREF(item);
return item;
return Py_NewRef(item);
}
it->it_seq = NULL;
@ -1146,8 +1132,7 @@ tuple_iter(PyObject *seq)
if (it == NULL)
return NULL;
it->it_index = 0;
Py_INCREF(seq);
it->it_seq = (PyTupleObject *)seq;
it->it_seq = (PyTupleObject *)Py_NewRef(seq);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}