Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-123497: New limit for Python integers on 64-bit platforms #123498

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Include/cpython/longobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
absolute value of a long. For example, this returns 1 for 1 and -1, 2
for 2 and -2, and 2 for 3 and -3. It returns 0 for 0.
v must not be NULL, and must be a normalized long.
(uint64_t)-1 is returned and OverflowError set if the true result doesn't
fit in a size_t.
Always successful.
*/
PyAPI_FUNC(uint64_t) _PyLong_NumBits(PyObject *v);

Expand Down
9 changes: 4 additions & 5 deletions Include/internal/pycore_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ static inline PyObject* _PyLong_FromUnsignedChar(unsigned char i)
}

// _PyLong_Frexp returns a double x and an exponent e such that the
// true value is approximately equal to x * 2**e. e is >= 0. x is
// true value is approximately equal to x * 2**e. x is
// 0.0 if and only if the input is 0 (in which case, e and x are both
// zeroes); otherwise, 0.5 <= abs(x) < 1.0. On overflow, which is
// possible if the number of bits doesn't fit into a Py_ssize_t, sets
// OverflowError and returns -1.0 for x, 0 for e.
// zeroes); otherwise, 0.5 <= abs(x) < 1.0.
// Always successful.
//
// Export for 'math' shared extension
PyAPI_DATA(double) _PyLong_Frexp(PyLongObject *a, int64_t *e);
PyAPI_DATA(double) _PyLong_Frexp(PyLongObject *a, uint64_t *e);

extern PyObject* _PyLong_FromBytes(const char *, Py_ssize_t, int);

Expand Down
3 changes: 1 addition & 2 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2155,8 +2155,7 @@ save_long(PicklerObject *self, PyObject *obj)
return 0;
}
nbits = _PyLong_NumBits(obj);
if (nbits == (uint64_t)-1 && PyErr_Occurred())
goto error;
assert(!PyErr_Occurred());
/* How many bytes do we need? There are nbits >> 3 full
* bytes of data, and nbits & 7 leftover bits. If there
* are any leftover bits, then we clearly need another
Expand Down
3 changes: 1 addition & 2 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ random_seed(RandomObject *self, PyObject *arg)

/* Now split n into 32-bit chunks, from the right. */
bits = _PyLong_NumBits(n);
if (bits == (uint64_t)-1 && PyErr_Occurred())
goto Done;
assert(!PyErr_Occurred());

/* Figure out how many 32-bit chunks this gives us. */
keyused = bits == 0 ? 1 : (size_t)((bits - 1) / 32 + 1);
Expand Down
9 changes: 3 additions & 6 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,9 +1680,7 @@ math_isqrt(PyObject *module, PyObject *n)

/* c = (n.bit_length() - 1) // 2 */
c = _PyLong_NumBits(n);
if (c == (uint64_t)(-1)) {
goto error;
}
assert(!PyErr_Occurred());
c = (c - 1U) / 2U;

/* Fast path: if c <= 31 then n < 2**64 and we can compute directly with a
Expand Down Expand Up @@ -2185,7 +2183,7 @@ loghelper(PyObject* arg, double (*func)(double))
/* If it is int, do it ourselves. */
if (PyLong_Check(arg)) {
double x, result;
int64_t e;
uint64_t e;

/* Negative or zero inputs give a ValueError. */
if (!_PyLong_IsPositive((PyLongObject *)arg)) {
Expand All @@ -2202,8 +2200,7 @@ loghelper(PyObject* arg, double (*func)(double))
to compute the log anyway. Clear the exception and continue. */
PyErr_Clear();
x = _PyLong_Frexp((PyLongObject *)arg, &e);
if (x == -1.0 && PyErr_Occurred())
return NULL;
assert(!PyErr_Occurred());
/* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */
result = func(x) + func(2.0) * e;
}
Expand Down
6 changes: 1 addition & 5 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,7 @@ float_richcompare(PyObject *v, PyObject *w, int op)
* its magnitude must exceed the magnitude of any
* finite float.
*/
if (nbits64 == (uint64_t)-1 && PyErr_Occurred()) {
/* This Python integer is so large that uint64_t isn't
* big enough to hold the # of bits. */
PyErr_Clear();
}
assert(!PyErr_Occurred());
i = (double)vsign;
assert(wsign != 0);
j = wsign * 2.0;
Expand Down
Loading
Loading