Skip to content

Commit

Permalink
pythongh-99537: Use Py_SETREF() function in longobject C code (python…
Browse files Browse the repository at this point in the history
…#99655)

Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);"
in longobject.c and _testcapi/long.c.
  • Loading branch information
vstinner committed Nov 22, 2022
1 parent 1acdfec commit 20d9749
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 57 deletions.
12 changes: 4 additions & 8 deletions Modules/_testcapi/long.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
73 changes: 24 additions & 49 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -5781,17 +5765,15 @@ _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 */
if (quo_is_neg)
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;
}
Expand Down Expand Up @@ -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;

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

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

0 comments on commit 20d9749

Please sign in to comment.