gh-99537: Use Py_SETREF() function in longobject C code (#99655)

Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);"
in longobject.c and _testcapi/long.c.
This commit is contained in:
Victor Stinner 2022-11-22 13:04:19 +01:00 committed by GitHub
parent 1acdfec359
commit 20d9749a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 57 deletions

View File

@ -121,8 +121,7 @@ test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
} }
temp = PyNumber_Add(num, one); temp = PyNumber_Add(num, one);
Py_DECREF(one); Py_DECREF(one);
Py_DECREF(num); Py_SETREF(num, temp);
num = temp;
if (num == NULL) if (num == NULL)
return NULL; return NULL;
overflow = 0; overflow = 0;
@ -165,8 +164,7 @@ test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
} }
temp = PyNumber_Subtract(num, one); temp = PyNumber_Subtract(num, one);
Py_DECREF(one); Py_DECREF(one);
Py_DECREF(num); Py_SETREF(num, temp);
num = temp;
if (num == NULL) if (num == NULL)
return NULL; return NULL;
overflow = 0; overflow = 0;
@ -285,8 +283,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
} }
temp = PyNumber_Add(num, one); temp = PyNumber_Add(num, one);
Py_DECREF(one); Py_DECREF(one);
Py_DECREF(num); Py_SETREF(num, temp);
num = temp;
if (num == NULL) if (num == NULL)
return NULL; return NULL;
overflow = 0; overflow = 0;
@ -329,8 +326,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
} }
temp = PyNumber_Subtract(num, one); temp = PyNumber_Subtract(num, one);
Py_DECREF(one); Py_DECREF(one);
Py_DECREF(num); Py_SETREF(num, temp);
num = temp;
if (num == NULL) if (num == NULL)
return NULL; return NULL;
overflow = 0; overflow = 0;

View File

@ -2598,8 +2598,7 @@ long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits,
memcpy(tmp->ob_digit, memcpy(tmp->ob_digit,
z->ob_digit, z->ob_digit,
sizeof(digit) * size_z); sizeof(digit) * size_z);
Py_DECREF(z); Py_SETREF(z, tmp);
z = tmp;
z->ob_digit[size_z] = (digit)c; z->ob_digit[size_z] = (digit)c;
++size_z; ++size_z;
} }
@ -4140,8 +4139,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
(Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
PyLongObject *temp; PyLongObject *temp;
temp = (PyLongObject *) long_add(mod, w); temp = (PyLongObject *) long_add(mod, w);
Py_DECREF(mod); Py_SETREF(mod, temp);
mod = temp;
if (mod == NULL) { if (mod == NULL) {
Py_DECREF(div); Py_DECREF(div);
return -1; return -1;
@ -4152,8 +4150,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
Py_DECREF(div); Py_DECREF(div);
return -1; return -1;
} }
Py_DECREF(div); Py_SETREF(div, temp);
div = temp;
} }
if (pdiv != NULL) if (pdiv != NULL)
*pdiv = div; *pdiv = div;
@ -4189,8 +4186,7 @@ l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod)
(Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
PyLongObject *temp; PyLongObject *temp;
temp = (PyLongObject *) long_add(mod, w); temp = (PyLongObject *) long_add(mod, w);
Py_DECREF(mod); Py_SETREF(mod, temp);
mod = temp;
if (mod == NULL) if (mod == NULL)
return -1; return -1;
} }
@ -4430,8 +4426,7 @@ long_true_divide(PyObject *v, PyObject *w)
else { else {
PyLongObject *div, *rem; PyLongObject *div, *rem;
div = x_divrem(x, b, &rem); div = x_divrem(x, b, &rem);
Py_DECREF(x); Py_SETREF(x, div);
x = div;
if (x == NULL) if (x == NULL)
goto error; goto error;
if (Py_SIZE(rem)) if (Py_SIZE(rem))
@ -4561,8 +4556,7 @@ long_invmod(PyLongObject *a, PyLongObject *n)
if (l_divmod(a, n, &q, &r) == -1) { if (l_divmod(a, n, &q, &r) == -1) {
goto Error; goto Error;
} }
Py_DECREF(a); Py_SETREF(a, n);
a = n;
n = r; n = r;
t = (PyLongObject *)long_mul(q, c); t = (PyLongObject *)long_mul(q, c);
Py_DECREF(q); Py_DECREF(q);
@ -4574,8 +4568,7 @@ long_invmod(PyLongObject *a, PyLongObject *n)
if (s == NULL) { if (s == NULL) {
goto Error; goto Error;
} }
Py_DECREF(b); Py_SETREF(b, c);
b = c;
c = s; c = s;
} }
/* references now owned: a, b, c, n */ /* references now owned: a, b, c, n */
@ -4670,8 +4663,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = (PyLongObject *)_PyLong_Copy(c); temp = (PyLongObject *)_PyLong_Copy(c);
if (temp == NULL) if (temp == NULL)
goto Error; goto Error;
Py_DECREF(c); Py_SETREF(c, temp);
c = temp;
temp = NULL; temp = NULL;
_PyLong_Negate(&c); _PyLong_Negate(&c);
if (c == NULL) if (c == NULL)
@ -4691,8 +4683,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = (PyLongObject *)_PyLong_Copy(b); temp = (PyLongObject *)_PyLong_Copy(b);
if (temp == NULL) if (temp == NULL)
goto Error; goto Error;
Py_DECREF(b); Py_SETREF(b, temp);
b = temp;
temp = NULL; temp = NULL;
_PyLong_Negate(&b); _PyLong_Negate(&b);
if (b == NULL) if (b == NULL)
@ -4701,8 +4692,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = long_invmod(a, c); temp = long_invmod(a, c);
if (temp == NULL) if (temp == NULL)
goto Error; goto Error;
Py_DECREF(a); Py_SETREF(a, temp);
a = temp;
temp = NULL; temp = NULL;
} }
@ -4718,8 +4708,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) { if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) {
if (l_mod(a, c, &temp) < 0) if (l_mod(a, c, &temp) < 0)
goto Error; goto Error;
Py_DECREF(a); Py_SETREF(a, temp);
a = temp;
temp = NULL; temp = NULL;
} }
} }
@ -4786,9 +4775,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
* because we're primarily trying to cut overhead for small powers. * because we're primarily trying to cut overhead for small powers.
*/ */
assert(bi); /* else there is no significant bit */ assert(bi); /* else there is no significant bit */
Py_INCREF(a); Py_SETREF(z, Py_NewRef(a));
Py_DECREF(z);
z = a;
for (bit = 2; ; bit <<= 1) { for (bit = 2; ; bit <<= 1) {
if (bit > bi) { /* found the first bit */ if (bit > bi) { /* found the first bit */
assert((bi & bit) == 0); assert((bi & bit) == 0);
@ -4874,8 +4861,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = (PyLongObject *)long_sub(z, c); temp = (PyLongObject *)long_sub(z, c);
if (temp == NULL) if (temp == NULL)
goto Error; goto Error;
Py_DECREF(z); Py_SETREF(z, temp);
z = temp;
temp = NULL; temp = NULL;
} }
goto Done; goto Done;
@ -5444,8 +5430,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
/* no progress; do a Euclidean step */ /* no progress; do a Euclidean step */
if (l_mod(a, b, &r) < 0) if (l_mod(a, b, &r) < 0)
goto error; goto error;
Py_DECREF(a); Py_SETREF(a, b);
a = b;
b = r; b = r;
alloc_a = alloc_b; alloc_a = alloc_b;
alloc_b = Py_SIZE(b); alloc_b = Py_SIZE(b);
@ -5766,8 +5751,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
goto error; goto error;
if (quo_is_neg) { if (quo_is_neg) {
temp = long_neg((PyLongObject*)twice_rem); temp = long_neg((PyLongObject*)twice_rem);
Py_DECREF(twice_rem); Py_SETREF(twice_rem, temp);
twice_rem = temp;
if (twice_rem == NULL) if (twice_rem == NULL)
goto error; goto error;
} }
@ -5781,8 +5765,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
temp = long_sub(quo, (PyLongObject *)one); temp = long_sub(quo, (PyLongObject *)one);
else else
temp = long_add(quo, (PyLongObject *)one); temp = long_add(quo, (PyLongObject *)one);
Py_DECREF(quo); Py_SETREF(quo, (PyLongObject *)temp);
quo = (PyLongObject *)temp;
if (quo == NULL) if (quo == NULL)
goto error; goto error;
/* and remainder */ /* and remainder */
@ -5790,8 +5773,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
temp = long_add(rem, (PyLongObject *)b); temp = long_add(rem, (PyLongObject *)b);
else else
temp = long_sub(rem, (PyLongObject *)b); temp = long_sub(rem, (PyLongObject *)b);
Py_DECREF(rem); Py_SETREF(rem, (PyLongObject *)temp);
rem = (PyLongObject *)temp;
if (rem == NULL) if (rem == NULL)
goto error; goto error;
} }
@ -5857,8 +5839,7 @@ int___round___impl(PyObject *self, PyObject *o_ndigits)
/* result = self - divmod_near(self, 10 ** -ndigits)[1] */ /* result = self - divmod_near(self, 10 ** -ndigits)[1] */
temp = long_neg((PyLongObject*)ndigits); temp = long_neg((PyLongObject*)ndigits);
Py_DECREF(ndigits); Py_SETREF(ndigits, temp);
ndigits = temp;
if (ndigits == NULL) if (ndigits == NULL)
return NULL; return NULL;
@ -5870,21 +5851,18 @@ int___round___impl(PyObject *self, PyObject *o_ndigits)
temp = long_pow(result, ndigits, Py_None); temp = long_pow(result, ndigits, Py_None);
Py_DECREF(ndigits); Py_DECREF(ndigits);
Py_DECREF(result); Py_SETREF(result, temp);
result = temp;
if (result == NULL) if (result == NULL)
return NULL; return NULL;
temp = _PyLong_DivmodNear(self, result); temp = _PyLong_DivmodNear(self, result);
Py_DECREF(result); Py_SETREF(result, temp);
result = temp;
if (result == NULL) if (result == NULL)
return NULL; return NULL;
temp = long_sub((PyLongObject *)self, temp = long_sub((PyLongObject *)self,
(PyLongObject *)PyTuple_GET_ITEM(result, 1)); (PyLongObject *)PyTuple_GET_ITEM(result, 1));
Py_DECREF(result); Py_SETREF(result, temp);
result = temp;
return result; return result;
} }
@ -5949,8 +5927,7 @@ int_bit_length_impl(PyObject *self)
Py_DECREF(x); Py_DECREF(x);
if (y == NULL) if (y == NULL)
goto error; goto error;
Py_DECREF(result); Py_SETREF(result, y);
result = y;
x = (PyLongObject *)PyLong_FromLong((long)msd_bits); x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
if (x == NULL) if (x == NULL)
@ -5959,8 +5936,7 @@ int_bit_length_impl(PyObject *self)
Py_DECREF(x); Py_DECREF(x);
if (y == NULL) if (y == NULL)
goto error; goto error;
Py_DECREF(result); Py_SETREF(result, y);
result = y;
return (PyObject *)result; return (PyObject *)result;
@ -6026,8 +6002,7 @@ int_bit_count_impl(PyObject *self)
if (y == NULL) { if (y == NULL) {
goto error; goto error;
} }
Py_DECREF(result); Py_SETREF(result, y);
result = y;
} }
return result; return result;