summaryrefslogtreecommitdiffstats
path: root/Modules/mathmodule.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2023-03-22 14:49:51 (GMT)
committerGitHub <noreply@github.com>2023-03-22 14:49:51 (GMT)
commit7559f5fda94ab568a1a910b17683ed81dc3426fb (patch)
treea050bbc075372c6246fe3386560596f2283ae8bb /Modules/mathmodule.c
parent713df2c53489ce8012d0ede10b70950e6b0d8372 (diff)
downloadcpython-7559f5fda94ab568a1a910b17683ed81dc3426fb.zip
cpython-7559f5fda94ab568a1a910b17683ed81dc3426fb.tar.gz
cpython-7559f5fda94ab568a1a910b17683ed81dc3426fb.tar.bz2
GH-101291: Rearrange the size bits in PyLongObject (GH-102464)
* Eliminate all remaining uses of Py_SIZE and Py_SET_SIZE on PyLongObject, adding asserts. * Change layout of size/sign bits in longobject to support future addition of immortal ints and tagged medium ints. * Add functions to hide some internals of long object, and for setting sign and digit count. * Replace uses of IS_MEDIUM_VALUE macro with _PyLong_IsCompact().
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r--Modules/mathmodule.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 473936e..eddc1a3 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -836,7 +836,7 @@ long_lcm(PyObject *a, PyObject *b)
{
PyObject *g, *m, *f, *ab;
- if (Py_SIZE(a) == 0 || Py_SIZE(b) == 0) {
+ if (_PyLong_IsZero((PyLongObject *)a) || _PyLong_IsZero((PyLongObject *)b)) {
return PyLong_FromLong(0);
}
g = _PyLong_GCD(a, b);
@@ -1726,13 +1726,13 @@ math_isqrt(PyObject *module, PyObject *n)
return NULL;
}
- if (_PyLong_Sign(n) < 0) {
+ if (_PyLong_IsNegative((PyLongObject *)n)) {
PyErr_SetString(
PyExc_ValueError,
"isqrt() argument must be nonnegative");
goto error;
}
- if (_PyLong_Sign(n) == 0) {
+ if (_PyLong_IsZero((PyLongObject *)n)) {
Py_DECREF(n);
return PyLong_FromLong(0);
}
@@ -2254,7 +2254,7 @@ loghelper(PyObject* arg, double (*func)(double))
Py_ssize_t e;
/* Negative or zero inputs give a ValueError. */
- if (Py_SIZE(arg) <= 0) {
+ if (!_PyLong_IsPositive((PyLongObject *)arg)) {
PyErr_SetString(PyExc_ValueError,
"math domain error");
return NULL;
@@ -3716,12 +3716,12 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
}
assert(PyLong_CheckExact(n) && PyLong_CheckExact(k));
- if (Py_SIZE(n) < 0) {
+ if (_PyLong_IsNegative((PyLongObject *)n)) {
PyErr_SetString(PyExc_ValueError,
"n must be a non-negative integer");
goto error;
}
- if (Py_SIZE(k) < 0) {
+ if (_PyLong_IsNegative((PyLongObject *)k)) {
PyErr_SetString(PyExc_ValueError,
"k must be a non-negative integer");
goto error;
@@ -3808,12 +3808,12 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
}
assert(PyLong_CheckExact(n) && PyLong_CheckExact(k));
- if (Py_SIZE(n) < 0) {
+ if (_PyLong_IsNegative((PyLongObject *)n)) {
PyErr_SetString(PyExc_ValueError,
"n must be a non-negative integer");
goto error;
}
- if (Py_SIZE(k) < 0) {
+ if (_PyLong_IsNegative((PyLongObject *)k)) {
PyErr_SetString(PyExc_ValueError,
"k must be a non-negative integer");
goto error;
@@ -3845,7 +3845,8 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
if (temp == NULL) {
goto error;
}
- if (Py_SIZE(temp) < 0) {
+ assert(PyLong_Check(temp));
+ if (_PyLong_IsNegative((PyLongObject *)temp)) {
Py_DECREF(temp);
result = PyLong_FromLong(0);
goto done;