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);
Py_DECREF(one);
Py_DECREF(num);
num = temp;
Py_SETREF(num, temp);
if (num == NULL)
return NULL;
overflow = 0;
@ -165,8 +164,7 @@ test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
}
temp = PyNumber_Subtract(num, one);
Py_DECREF(one);
Py_DECREF(num);
num = temp;
Py_SETREF(num, temp);
if (num == NULL)
return NULL;
overflow = 0;
@ -285,8 +283,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
}
temp = PyNumber_Add(num, one);
Py_DECREF(one);
Py_DECREF(num);
num = temp;
Py_SETREF(num, temp);
if (num == NULL)
return NULL;
overflow = 0;
@ -329,8 +326,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
}
temp = PyNumber_Subtract(num, one);
Py_DECREF(one);
Py_DECREF(num);
num = temp;
Py_SETREF(num, temp);
if (num == NULL)
return NULL;
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,
z->ob_digit,
sizeof(digit) * size_z);
Py_DECREF(z);
z = tmp;
Py_SETREF(z, tmp);
z->ob_digit[size_z] = (digit)c;
++size_z;
}
@ -4140,8 +4139,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
(Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
PyLongObject *temp;
temp = (PyLongObject *) long_add(mod, w);
Py_DECREF(mod);
mod = temp;
Py_SETREF(mod, temp);
if (mod == NULL) {
Py_DECREF(div);
return -1;
@ -4152,8 +4150,7 @@ l_divmod(PyLongObject *v, PyLongObject *w,
Py_DECREF(div);
return -1;
}
Py_DECREF(div);
div = temp;
Py_SETREF(div, temp);
}
if (pdiv != NULL)
*pdiv = div;
@ -4189,8 +4186,7 @@ l_mod(PyLongObject *v, PyLongObject *w, PyLongObject **pmod)
(Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
PyLongObject *temp;
temp = (PyLongObject *) long_add(mod, w);
Py_DECREF(mod);
mod = temp;
Py_SETREF(mod, temp);
if (mod == NULL)
return -1;
}
@ -4430,8 +4426,7 @@ long_true_divide(PyObject *v, PyObject *w)
else {
PyLongObject *div, *rem;
div = x_divrem(x, b, &rem);
Py_DECREF(x);
x = div;
Py_SETREF(x, div);
if (x == NULL)
goto error;
if (Py_SIZE(rem))
@ -4561,8 +4556,7 @@ long_invmod(PyLongObject *a, PyLongObject *n)
if (l_divmod(a, n, &q, &r) == -1) {
goto Error;
}
Py_DECREF(a);
a = n;
Py_SETREF(a, n);
n = r;
t = (PyLongObject *)long_mul(q, c);
Py_DECREF(q);
@ -4574,8 +4568,7 @@ long_invmod(PyLongObject *a, PyLongObject *n)
if (s == NULL) {
goto Error;
}
Py_DECREF(b);
b = c;
Py_SETREF(b, c);
c = s;
}
/* references now owned: a, b, c, n */
@ -4670,8 +4663,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = (PyLongObject *)_PyLong_Copy(c);
if (temp == NULL)
goto Error;
Py_DECREF(c);
c = temp;
Py_SETREF(c, temp);
temp = NULL;
_PyLong_Negate(&c);
if (c == NULL)
@ -4691,8 +4683,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = (PyLongObject *)_PyLong_Copy(b);
if (temp == NULL)
goto Error;
Py_DECREF(b);
b = temp;
Py_SETREF(b, temp);
temp = NULL;
_PyLong_Negate(&b);
if (b == NULL)
@ -4701,8 +4692,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = long_invmod(a, c);
if (temp == NULL)
goto Error;
Py_DECREF(a);
a = temp;
Py_SETREF(a, temp);
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 (l_mod(a, c, &temp) < 0)
goto Error;
Py_DECREF(a);
a = temp;
Py_SETREF(a, temp);
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.
*/
assert(bi); /* else there is no significant bit */
Py_INCREF(a);
Py_DECREF(z);
z = a;
Py_SETREF(z, Py_NewRef(a));
for (bit = 2; ; bit <<= 1) {
if (bit > bi) { /* found the first bit */
assert((bi & bit) == 0);
@ -4874,8 +4861,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
temp = (PyLongObject *)long_sub(z, c);
if (temp == NULL)
goto Error;
Py_DECREF(z);
z = temp;
Py_SETREF(z, temp);
temp = NULL;
}
goto Done;
@ -5444,8 +5430,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
/* no progress; do a Euclidean step */
if (l_mod(a, b, &r) < 0)
goto error;
Py_DECREF(a);
a = b;
Py_SETREF(a, b);
b = r;
alloc_a = alloc_b;
alloc_b = Py_SIZE(b);
@ -5766,8 +5751,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
goto error;
if (quo_is_neg) {
temp = long_neg((PyLongObject*)twice_rem);
Py_DECREF(twice_rem);
twice_rem = temp;
Py_SETREF(twice_rem, temp);
if (twice_rem == NULL)
goto error;
}
@ -5781,8 +5765,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
temp = long_sub(quo, (PyLongObject *)one);
else
temp = long_add(quo, (PyLongObject *)one);
Py_DECREF(quo);
quo = (PyLongObject *)temp;
Py_SETREF(quo, (PyLongObject *)temp);
if (quo == NULL)
goto error;
/* and remainder */
@ -5790,8 +5773,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
temp = long_add(rem, (PyLongObject *)b);
else
temp = long_sub(rem, (PyLongObject *)b);
Py_DECREF(rem);
rem = (PyLongObject *)temp;
Py_SETREF(rem, (PyLongObject *)temp);
if (rem == NULL)
goto error;
}
@ -5857,8 +5839,7 @@ int___round___impl(PyObject *self, PyObject *o_ndigits)
/* result = self - divmod_near(self, 10 ** -ndigits)[1] */
temp = long_neg((PyLongObject*)ndigits);
Py_DECREF(ndigits);
ndigits = temp;
Py_SETREF(ndigits, temp);
if (ndigits == NULL)
return NULL;
@ -5870,21 +5851,18 @@ int___round___impl(PyObject *self, PyObject *o_ndigits)
temp = long_pow(result, ndigits, Py_None);
Py_DECREF(ndigits);
Py_DECREF(result);
result = temp;
Py_SETREF(result, temp);
if (result == NULL)
return NULL;
temp = _PyLong_DivmodNear(self, result);
Py_DECREF(result);
result = temp;
Py_SETREF(result, temp);
if (result == NULL)
return NULL;
temp = long_sub((PyLongObject *)self,
(PyLongObject *)PyTuple_GET_ITEM(result, 1));
Py_DECREF(result);
result = temp;
Py_SETREF(result, temp);
return result;
}
@ -5949,8 +5927,7 @@ int_bit_length_impl(PyObject *self)
Py_DECREF(x);
if (y == NULL)
goto error;
Py_DECREF(result);
result = y;
Py_SETREF(result, y);
x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
if (x == NULL)
@ -5959,8 +5936,7 @@ int_bit_length_impl(PyObject *self)
Py_DECREF(x);
if (y == NULL)
goto error;
Py_DECREF(result);
result = y;
Py_SETREF(result, y);
return (PyObject *)result;
@ -6026,8 +6002,7 @@ int_bit_count_impl(PyObject *self)
if (y == NULL) {
goto error;
}
Py_DECREF(result);
result = y;
Py_SETREF(result, y);
}
return result;