[Python-checkins] gh-99537: Use Py_SETREF() function in longobject C code (#99655)
vstinner
webhook-mailer at python.org
Tue Nov 22 07:04:29 EST 2022
https://github.com/python/cpython/commit/20d9749a0f9b9fa6946019f04a54b6287d16588e
commit: 20d9749a0f9b9fa6946019f04a54b6287d16588e
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-11-22T13:04:19+01:00
summary:
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.
files:
M Modules/_testcapi/long.c
M Objects/longobject.c
diff --git a/Modules/_testcapi/long.c b/Modules/_testcapi/long.c
index 70e315cdb8a3..1be8de5e5762 100644
--- a/Modules/_testcapi/long.c
+++ b/Modules/_testcapi/long.c
@@ -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;
diff --git a/Objects/longobject.c b/Objects/longobject.c
index bb9e40f862aa..f4bd981e4b98 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -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;
More information about the Python-checkins
mailing list