Skip to content

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

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
Show file tree
Hide file tree
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
51 changes: 20 additions & 31 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ get_small_int(sdigit ival)
{
assert(IS_SMALL_INT(ival));
PyObject *v = (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + ival];
Py_INCREF(v);
return v;
return Py_NewRef(v);
}

static PyLongObject *
Expand Down Expand Up @@ -1785,8 +1784,7 @@ pylong_int_to_decimal_string(PyObject *aa,
goto success;
}
else {
*p_output = (PyObject *)s;
Py_INCREF(s);
*p_output = Py_NewRef(s);
goto success;
}

Expand Down Expand Up @@ -2911,8 +2909,7 @@ long_divrem(PyLongObject *a, PyLongObject *b,
return -1;
}
PyObject *zero = _PyLong_GetZero();
Py_INCREF(zero);
*pdiv = (PyLongObject*)zero;
*pdiv = (PyLongObject*)Py_NewRef(zero);
return 0;
}
if (size_b == 1) {
Expand Down Expand Up @@ -3747,10 +3744,8 @@ k_mul(PyLongObject *a, PyLongObject *b)
assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */

if (a == b) {
bh = ah;
bl = al;
Py_INCREF(bh);
Py_INCREF(bl);
bh = (PyLongObject*)Py_NewRef(ah);
bl = (PyLongObject*)Py_NewRef(al);
}
else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;

Expand Down Expand Up @@ -3822,8 +3817,7 @@ k_mul(PyLongObject *a, PyLongObject *b)
ah = al = NULL;

if (a == b) {
t2 = t1;
Py_INCREF(t2);
t2 = (PyLongObject*)Py_NewRef(t1);
}
else if ((t2 = x_add(bh, bl)) == NULL) {
Py_DECREF(t1);
Expand Down Expand Up @@ -4067,12 +4061,10 @@ pylong_int_divmod(PyLongObject *v, PyLongObject *w,
return -1;
}
if (pdiv != NULL) {
Py_INCREF(q);
*pdiv = (PyLongObject *)q;
*pdiv = (PyLongObject *)Py_NewRef(q);
}
if (pmod != NULL) {
Py_INCREF(r);
*pmod = (PyLongObject *)r;
*pmod = (PyLongObject *)Py_NewRef(r);
}
Py_DECREF(result);
return 0;
Expand Down Expand Up @@ -4638,11 +4630,10 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)

/* a, b, c = v, w, x */
CHECK_BINOP(v, w);
a = (PyLongObject*)v; Py_INCREF(a);
b = (PyLongObject*)w; Py_INCREF(b);
a = (PyLongObject*)Py_NewRef(v);
b = (PyLongObject*)Py_NewRef(w);
if (PyLong_Check(x)) {
c = (PyLongObject *)x;
Py_INCREF(x);
c = (PyLongObject *)Py_NewRef(x);
}
else if (x == Py_None)
c = NULL;
Expand Down Expand Up @@ -4824,8 +4815,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
/* Left-to-right k-ary sliding window exponentiation
* (Handbook of Applied Cryptography (HAC) Algorithm 14.85)
*/
Py_INCREF(a);
table[0] = a;
table[0] = (PyLongObject*)Py_NewRef(a);
num_table_entries = 1;
MULT(a, a, a2);
/* table[i] == a**(2*i + 1) % c */
Expand Down Expand Up @@ -5362,11 +5352,12 @@ long_or(PyObject *a, PyObject *b)
static PyObject *
long_long(PyObject *v)
{
if (PyLong_CheckExact(v))
Py_INCREF(v);
else
v = _PyLong_Copy((PyLongObject *)v);
return v;
if (PyLong_CheckExact(v)) {
return Py_NewRef(v);
}
else {
return _PyLong_Copy((PyLongObject *)v);
}
}

PyObject *
Expand Down Expand Up @@ -5473,8 +5464,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
Py_SET_SIZE(c, size_a);
}
else if (Py_REFCNT(a) == 1) {
Py_INCREF(a);
c = a;
c = (PyLongObject*)Py_NewRef(a);
}
else {
alloc_a = size_a;
Expand All @@ -5487,8 +5477,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
Py_SET_SIZE(d, size_a);
}
else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
Py_INCREF(b);
d = b;
d = (PyLongObject*)Py_NewRef(b);
Py_SET_SIZE(d, size_a);
}
else {
Expand Down
51 changes: 17 additions & 34 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
if (!stop) {
return NULL;
}
start = _PyLong_GetZero();
Py_INCREF(start);
step = _PyLong_GetOne();
Py_INCREF(step);
start = Py_NewRef(_PyLong_GetZero());
step = Py_NewRef(_PyLong_GetOne());
break;
case 0:
PyErr_SetString(PyExc_TypeError,
Expand Down Expand Up @@ -216,8 +214,7 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
if (cmp_result < 0)
return NULL;
result = zero;
Py_INCREF(result);
return result;
return Py_NewRef(result);
}

if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
Expand Down Expand Up @@ -297,8 +294,7 @@ compute_range_item(rangeobject *r, PyObject *arg)
return NULL;
}
} else {
i = arg;
Py_INCREF(i);
i = Py_NewRef(arg);
}

/* PyLong equivalent to:
Expand Down Expand Up @@ -522,30 +518,24 @@ range_hash(rangeobject *r)
t = PyTuple_New(3);
if (!t)
return -1;
Py_INCREF(r->length);
PyTuple_SET_ITEM(t, 0, r->length);
PyTuple_SET_ITEM(t, 0, Py_NewRef(r->length));
cmp_result = PyObject_Not(r->length);
if (cmp_result == -1)
goto end;
if (cmp_result == 1) {
Py_INCREF(Py_None);
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 1, Py_None);
PyTuple_SET_ITEM(t, 2, Py_None);
PyTuple_SET_ITEM(t, 1, Py_NewRef(Py_None));
PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
}
else {
Py_INCREF(r->start);
PyTuple_SET_ITEM(t, 1, r->start);
PyTuple_SET_ITEM(t, 1, Py_NewRef(r->start));
cmp_result = PyObject_RichCompareBool(r->length, _PyLong_GetOne(), Py_EQ);
if (cmp_result == -1)
goto end;
if (cmp_result == 1) {
Py_INCREF(Py_None);
PyTuple_SET_ITEM(t, 2, Py_None);
PyTuple_SET_ITEM(t, 2, Py_NewRef(Py_None));
}
else {
Py_INCREF(r->step);
PyTuple_SET_ITEM(t, 2, r->step);
PyTuple_SET_ITEM(t, 2, Py_NewRef(r->step));
}
}
result = PyObject_Hash(t);
Expand Down Expand Up @@ -982,8 +972,7 @@ longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
if (cmp > 0)
state = r->len;
}
Py_INCREF(state);
Py_XSETREF(r->index, state);
Py_XSETREF(r->index, Py_NewRef(state));
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -1118,14 +1107,10 @@ range_iter(PyObject *seq)
if (it == NULL)
return NULL;

it->start = r->start;
it->step = r->step;
it->len = r->length;
it->index = _PyLong_GetZero();
Py_INCREF(it->start);
Py_INCREF(it->step);
Py_INCREF(it->len);
Py_INCREF(it->index);
it->start = Py_NewRef(r->start);
it->step = Py_NewRef(r->step);
it->len = Py_NewRef(r->length);
it->index = Py_NewRef(_PyLong_GetZero());
return (PyObject *)it;
}

Expand Down Expand Up @@ -1206,8 +1191,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
it->index = it->start = it->step = NULL;

/* start + (len - 1) * step */
it->len = range->length;
Py_INCREF(it->len);
it->len = Py_NewRef(range->length);

diff = PyNumber_Subtract(it->len, _PyLong_GetOne());
if (!diff)
Expand All @@ -1228,8 +1212,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
if (!it->step)
goto create_failure;

it->index = _PyLong_GetZero();
Py_INCREF(it->index);
it->index = Py_NewRef(_PyLong_GetZero());
return (PyObject *)it;

create_failure:
Expand Down
45 changes: 15 additions & 30 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 *
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Loading