summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.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 /Python/bltinmodule.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 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 12ca0ba..55fd364 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -5,6 +5,7 @@
#include "pycore_ast.h" // _PyAST_Validate()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_compile.h" // _PyAST_Compile()
+#include "pycore_long.h" // _PyLong_CompactValue
#include "pycore_object.h" // _Py_AddToAllObjects()
#include "pycore_pyerrors.h" // _PyErr_NoMemory()
#include "pycore_pystate.h" // _PyThreadState_GET()
@@ -2491,7 +2492,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
*/
if (PyLong_CheckExact(result)) {
int overflow;
- long i_result = PyLong_AsLongAndOverflow(result, &overflow);
+ Py_ssize_t i_result = PyLong_AsLongAndOverflow(result, &overflow);
/* If this already overflowed, don't even enter the loop. */
if (overflow == 0) {
Py_SETREF(result, NULL);
@@ -2505,15 +2506,14 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
return PyLong_FromLong(i_result);
}
if (PyLong_CheckExact(item) || PyBool_Check(item)) {
- long b;
+ Py_ssize_t b;
overflow = 0;
/* Single digits are common, fast, and cannot overflow on unpacking. */
- switch (Py_SIZE(item)) {
- case -1: b = -(sdigit) ((PyLongObject*)item)->long_value.ob_digit[0]; break;
- // Note: the continue goes to the top of the "while" loop that iterates over the elements
- case 0: Py_DECREF(item); continue;
- case 1: b = ((PyLongObject*)item)->long_value.ob_digit[0]; break;
- default: b = PyLong_AsLongAndOverflow(item, &overflow); break;
+ if (_PyLong_IsCompact((PyLongObject *)item)) {
+ b = _PyLong_CompactValue((PyLongObject *)item);
+ }
+ else {
+ b = PyLong_AsLongAndOverflow(item, &overflow);
}
if (overflow == 0 &&
(i_result >= 0 ? (b <= LONG_MAX - i_result)