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

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 16:27:32 +01:00 committed by GitHub
parent 4ce2a202c7
commit c0feb99187
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 77 additions and 156 deletions

View file

@ -45,8 +45,7 @@ PyObject_Type(PyObject *o)
}
v = (PyObject *)Py_TYPE(o);
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
Py_ssize_t
@ -722,9 +721,7 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
return -1;
}
view->obj = obj;
if (obj)
Py_INCREF(obj);
view->obj = Py_XNewRef(obj);
view->buf = buf;
view->len = len;
view->readonly = readonly;
@ -776,8 +773,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
/* Fast path for common types. */
if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
if (PyUnicode_CheckExact(obj)) {
Py_INCREF(obj);
return obj;
return Py_NewRef(obj);
}
if (PyLong_CheckExact(obj)) {
return PyObject_Str(obj);
@ -1405,8 +1401,7 @@ _PyNumber_Index(PyObject *item)
}
if (PyLong_Check(item)) {
Py_INCREF(item);
return item;
return Py_NewRef(item);
}
if (!_PyIndex_Check(item)) {
PyErr_Format(PyExc_TypeError,
@ -1520,8 +1515,7 @@ PyNumber_Long(PyObject *o)
}
if (PyLong_CheckExact(o)) {
Py_INCREF(o);
return o;
return Py_NewRef(o);
}
m = Py_TYPE(o)->tp_as_number;
if (m && m->nb_int) { /* This should include subclasses of int */
@ -2045,8 +2039,7 @@ PySequence_Tuple(PyObject *v)
a tuple *subclass* instance as-is, hence the restriction
to exact tuples here. In contrast, lists always make
a copy, so there's no need for exactness below. */
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
if (PyList_CheckExact(v))
return PyList_AsTuple(v);
@ -2144,8 +2137,7 @@ PySequence_Fast(PyObject *v, const char *m)
}
if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
it = PyObject_GetIter(v);