mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									694cdb24a6
								
							
						
					
					
						commit
						dbf8613a2e
					
				
					 1 changed files with 20 additions and 40 deletions
				
			
		| 
						 | 
					@ -299,8 +299,7 @@ ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
 | 
				
			||||||
    items = self->ob_item;
 | 
					    items = self->ob_item;
 | 
				
			||||||
    for (i = n; --i >= where; )
 | 
					    for (i = n; --i >= where; )
 | 
				
			||||||
        items[i+1] = items[i];
 | 
					        items[i+1] = items[i];
 | 
				
			||||||
    Py_INCREF(v);
 | 
					    items[where] = Py_NewRef(v);
 | 
				
			||||||
    items[where] = v;
 | 
					 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -332,8 +331,7 @@ int
 | 
				
			||||||
PyList_Append(PyObject *op, PyObject *newitem)
 | 
					PyList_Append(PyObject *op, PyObject *newitem)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (PyList_Check(op) && (newitem != NULL)) {
 | 
					    if (PyList_Check(op) && (newitem != NULL)) {
 | 
				
			||||||
        Py_INCREF(newitem);
 | 
					        return _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
 | 
				
			||||||
        return _PyList_AppendTakeRef((PyListObject *)op, newitem);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    PyErr_BadInternalCall();
 | 
					    PyErr_BadInternalCall();
 | 
				
			||||||
    return -1;
 | 
					    return -1;
 | 
				
			||||||
| 
						 | 
					@ -461,8 +459,7 @@ list_item(PyListObject *a, Py_ssize_t i)
 | 
				
			||||||
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
 | 
					        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Py_INCREF(a->ob_item[i]);
 | 
					    return Py_NewRef(a->ob_item[i]);
 | 
				
			||||||
    return a->ob_item[i];
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static PyObject *
 | 
					static PyObject *
 | 
				
			||||||
| 
						 | 
					@ -483,8 +480,7 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
 | 
				
			||||||
    dest = np->ob_item;
 | 
					    dest = np->ob_item;
 | 
				
			||||||
    for (i = 0; i < len; i++) {
 | 
					    for (i = 0; i < len; i++) {
 | 
				
			||||||
        PyObject *v = src[i];
 | 
					        PyObject *v = src[i];
 | 
				
			||||||
        Py_INCREF(v);
 | 
					        dest[i] = Py_NewRef(v);
 | 
				
			||||||
        dest[i] = v;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Py_SET_SIZE(np, len);
 | 
					    Py_SET_SIZE(np, len);
 | 
				
			||||||
    return (PyObject *)np;
 | 
					    return (PyObject *)np;
 | 
				
			||||||
| 
						 | 
					@ -539,15 +535,13 @@ list_concat(PyListObject *a, PyObject *bb)
 | 
				
			||||||
    dest = np->ob_item;
 | 
					    dest = np->ob_item;
 | 
				
			||||||
    for (i = 0; i < Py_SIZE(a); i++) {
 | 
					    for (i = 0; i < Py_SIZE(a); i++) {
 | 
				
			||||||
        PyObject *v = src[i];
 | 
					        PyObject *v = src[i];
 | 
				
			||||||
        Py_INCREF(v);
 | 
					        dest[i] = Py_NewRef(v);
 | 
				
			||||||
        dest[i] = v;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    src = b->ob_item;
 | 
					    src = b->ob_item;
 | 
				
			||||||
    dest = np->ob_item + Py_SIZE(a);
 | 
					    dest = np->ob_item + Py_SIZE(a);
 | 
				
			||||||
    for (i = 0; i < Py_SIZE(b); i++) {
 | 
					    for (i = 0; i < Py_SIZE(b); i++) {
 | 
				
			||||||
        PyObject *v = src[i];
 | 
					        PyObject *v = src[i];
 | 
				
			||||||
        Py_INCREF(v);
 | 
					        dest[i] = Py_NewRef(v);
 | 
				
			||||||
        dest[i] = v;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Py_SET_SIZE(np, size);
 | 
					    Py_SET_SIZE(np, size);
 | 
				
			||||||
    return (PyObject *)np;
 | 
					    return (PyObject *)np;
 | 
				
			||||||
| 
						 | 
					@ -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++) {
 | 
					    for (k = 0; k < n; k++, ilow++) {
 | 
				
			||||||
        PyObject *w = vitem[k];
 | 
					        PyObject *w = vitem[k];
 | 
				
			||||||
        Py_XINCREF(w);
 | 
					        item[ilow] = Py_XNewRef(w);
 | 
				
			||||||
        item[ilow] = w;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    for (k = norig - 1; k >= 0; --k)
 | 
					    for (k = norig - 1; k >= 0; --k)
 | 
				
			||||||
        Py_XDECREF(recycle[k]);
 | 
					        Py_XDECREF(recycle[k]);
 | 
				
			||||||
| 
						 | 
					@ -745,14 +738,12 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    Py_ssize_t input_size = PyList_GET_SIZE(self);
 | 
					    Py_ssize_t input_size = PyList_GET_SIZE(self);
 | 
				
			||||||
    if (input_size == 0 || n == 1) {
 | 
					    if (input_size == 0 || n == 1) {
 | 
				
			||||||
        Py_INCREF(self);
 | 
					        return Py_NewRef(self);
 | 
				
			||||||
        return (PyObject *)self;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (n < 1) {
 | 
					    if (n < 1) {
 | 
				
			||||||
        (void)_list_clear(self);
 | 
					        (void)_list_clear(self);
 | 
				
			||||||
        Py_INCREF(self);
 | 
					        return Py_NewRef(self);
 | 
				
			||||||
        return (PyObject *)self;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (input_size > PY_SSIZE_T_MAX / n) {
 | 
					    if (input_size > PY_SSIZE_T_MAX / n) {
 | 
				
			||||||
| 
						 | 
					@ -770,8 +761,7 @@ list_inplace_repeat(PyListObject *self, Py_ssize_t n)
 | 
				
			||||||
    _Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
 | 
					    _Py_memory_repeat((char *)items, sizeof(PyObject *)*output_size,
 | 
				
			||||||
                      sizeof(PyObject *)*input_size);
 | 
					                      sizeof(PyObject *)*input_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Py_INCREF(self);
 | 
					    return Py_NewRef(self);
 | 
				
			||||||
    return (PyObject *)self;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int
 | 
					static int
 | 
				
			||||||
| 
						 | 
					@ -784,8 +774,7 @@ list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (v == NULL)
 | 
					    if (v == NULL)
 | 
				
			||||||
        return list_ass_slice(a, i, i+1, v);
 | 
					        return list_ass_slice(a, i, i+1, v);
 | 
				
			||||||
    Py_INCREF(v);
 | 
					    Py_SETREF(a->ob_item[i], Py_NewRef(v));
 | 
				
			||||||
    Py_SETREF(a->ob_item[i], v);
 | 
					 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -913,8 +902,7 @@ list_extend(PyListObject *self, PyObject *iterable)
 | 
				
			||||||
        dest = self->ob_item + m;
 | 
					        dest = self->ob_item + m;
 | 
				
			||||||
        for (i = 0; i < n; i++) {
 | 
					        for (i = 0; i < n; i++) {
 | 
				
			||||||
            PyObject *o = src[i];
 | 
					            PyObject *o = src[i];
 | 
				
			||||||
            Py_INCREF(o);
 | 
					            dest[i] = Py_NewRef(o);
 | 
				
			||||||
            dest[i] = o;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Py_DECREF(iterable);
 | 
					        Py_DECREF(iterable);
 | 
				
			||||||
        Py_RETURN_NONE;
 | 
					        Py_RETURN_NONE;
 | 
				
			||||||
| 
						 | 
					@ -1002,8 +990,7 @@ list_inplace_concat(PyListObject *self, PyObject *other)
 | 
				
			||||||
    if (result == NULL)
 | 
					    if (result == NULL)
 | 
				
			||||||
        return result;
 | 
					        return result;
 | 
				
			||||||
    Py_DECREF(result);
 | 
					    Py_DECREF(result);
 | 
				
			||||||
    Py_INCREF(self);
 | 
					    return Py_NewRef(self);
 | 
				
			||||||
    return (PyObject *)self;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*[clinic input]
 | 
					/*[clinic input]
 | 
				
			||||||
| 
						 | 
					@ -2512,8 +2499,7 @@ keyfunc_fail:
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        PyMem_Free(final_ob_item);
 | 
					        PyMem_Free(final_ob_item);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Py_XINCREF(result);
 | 
					    return Py_XNewRef(result);
 | 
				
			||||||
    return result;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#undef IFLT
 | 
					#undef IFLT
 | 
				
			||||||
#undef ISLT
 | 
					#undef ISLT
 | 
				
			||||||
| 
						 | 
					@ -2901,8 +2887,7 @@ list_subscript(PyListObject* self, PyObject* item)
 | 
				
			||||||
            dest = ((PyListObject *)result)->ob_item;
 | 
					            dest = ((PyListObject *)result)->ob_item;
 | 
				
			||||||
            for (cur = start, i = 0; i < slicelength;
 | 
					            for (cur = start, i = 0; i < slicelength;
 | 
				
			||||||
                 cur += (size_t)step, i++) {
 | 
					                 cur += (size_t)step, i++) {
 | 
				
			||||||
                it = src[cur];
 | 
					                it = Py_NewRef(src[cur]);
 | 
				
			||||||
                Py_INCREF(it);
 | 
					 | 
				
			||||||
                dest[i] = it;
 | 
					                dest[i] = it;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Py_SET_SIZE(result, slicelength);
 | 
					            Py_SET_SIZE(result, slicelength);
 | 
				
			||||||
| 
						 | 
					@ -3057,8 +3042,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
 | 
				
			||||||
            for (cur = start, i = 0; i < slicelength;
 | 
					            for (cur = start, i = 0; i < slicelength;
 | 
				
			||||||
                 cur += (size_t)step, i++) {
 | 
					                 cur += (size_t)step, i++) {
 | 
				
			||||||
                garbage[i] = selfitems[cur];
 | 
					                garbage[i] = selfitems[cur];
 | 
				
			||||||
                ins = seqitems[i];
 | 
					                ins = Py_NewRef(seqitems[i]);
 | 
				
			||||||
                Py_INCREF(ins);
 | 
					 | 
				
			||||||
                selfitems[cur] = ins;
 | 
					                selfitems[cur] = ins;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3199,8 +3183,7 @@ list_iter(PyObject *seq)
 | 
				
			||||||
    if (it == NULL)
 | 
					    if (it == NULL)
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    it->it_index = 0;
 | 
					    it->it_index = 0;
 | 
				
			||||||
    Py_INCREF(seq);
 | 
					    it->it_seq = (PyListObject *)Py_NewRef(seq);
 | 
				
			||||||
    it->it_seq = (PyListObject *)seq;
 | 
					 | 
				
			||||||
    _PyObject_GC_TRACK(it);
 | 
					    _PyObject_GC_TRACK(it);
 | 
				
			||||||
    return (PyObject *)it;
 | 
					    return (PyObject *)it;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -3235,8 +3218,7 @@ listiter_next(_PyListIterObject *it)
 | 
				
			||||||
    if (it->it_index < PyList_GET_SIZE(seq)) {
 | 
					    if (it->it_index < PyList_GET_SIZE(seq)) {
 | 
				
			||||||
        item = PyList_GET_ITEM(seq, it->it_index);
 | 
					        item = PyList_GET_ITEM(seq, it->it_index);
 | 
				
			||||||
        ++it->it_index;
 | 
					        ++it->it_index;
 | 
				
			||||||
        Py_INCREF(item);
 | 
					        return Py_NewRef(item);
 | 
				
			||||||
        return item;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it->it_seq = NULL;
 | 
					    it->it_seq = NULL;
 | 
				
			||||||
| 
						 | 
					@ -3350,8 +3332,7 @@ list___reversed___impl(PyListObject *self)
 | 
				
			||||||
        return NULL;
 | 
					        return NULL;
 | 
				
			||||||
    assert(PyList_Check(self));
 | 
					    assert(PyList_Check(self));
 | 
				
			||||||
    it->it_index = PyList_GET_SIZE(self) - 1;
 | 
					    it->it_index = PyList_GET_SIZE(self) - 1;
 | 
				
			||||||
    Py_INCREF(self);
 | 
					    it->it_seq = (PyListObject*)Py_NewRef(self);
 | 
				
			||||||
    it->it_seq = self;
 | 
					 | 
				
			||||||
    PyObject_GC_Track(it);
 | 
					    PyObject_GC_Track(it);
 | 
				
			||||||
    return (PyObject *)it;
 | 
					    return (PyObject *)it;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -3389,8 +3370,7 @@ listreviter_next(listreviterobject *it)
 | 
				
			||||||
    if (index>=0 && index < PyList_GET_SIZE(seq)) {
 | 
					    if (index>=0 && index < PyList_GET_SIZE(seq)) {
 | 
				
			||||||
        item = PyList_GET_ITEM(seq, index);
 | 
					        item = PyList_GET_ITEM(seq, index);
 | 
				
			||||||
        it->it_index--;
 | 
					        it->it_index--;
 | 
				
			||||||
        Py_INCREF(item);
 | 
					        return Py_NewRef(item);
 | 
				
			||||||
        return item;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    it->it_index = -1;
 | 
					    it->it_index = -1;
 | 
				
			||||||
    it->it_seq = NULL;
 | 
					    it->it_seq = NULL;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue