gh-123497: New limit for Python integers on 64-bit platforms (GH-123724)

Instead of be limited just by the size of addressable memory (2**63
bytes), Python integers are now also limited by the number of bits, so
the number of bit now always fit in a 64-bit integer.

Both limits are much larger than what might be available in practice,
so it doesn't affect users.

_PyLong_NumBits() and _PyLong_Frexp() are now always successful.
This commit is contained in:
Serhiy Storchaka 2024-09-29 10:40:20 +03:00 committed by GitHub
parent e0a41a5dd1
commit d08c788822
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 108 additions and 175 deletions

View file

@ -2146,7 +2146,7 @@ save_long(PicklerObject *self, PyObject *obj)
if (self->proto >= 2) {
/* Linear-time pickling. */
uint64_t nbits;
int64_t nbits;
size_t nbytes;
unsigned char *pdata;
char header[5];
@ -2161,8 +2161,8 @@ save_long(PicklerObject *self, PyObject *obj)
return 0;
}
nbits = _PyLong_NumBits(obj);
if (nbits == (uint64_t)-1 && PyErr_Occurred())
goto error;
assert(nbits >= 0);
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