Skip to content

Impose new limit for Python integers #123497

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

Closed
serhiy-storchaka opened this issue Aug 30, 2024 · 1 comment
Closed

Impose new limit for Python integers #123497

serhiy-storchaka opened this issue Aug 30, 2024 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 30, 2024

The upper limit of Python integers is determined by by the size of addressable memory. On 64-bit platforms this is about $2^{63}$ bytes or about $15\cdot 2^{62} < 2^{66}$ bits. Unfortunately, the number of bits of some integers cannot be represented as a 64-bit integer, this complicates the code.

The following PR lowers the limit to about $2^{64}$ bits or about $2^{65}/15 > 2^{61}$ bytes. It is still much larger than practically achievable (larger that the limit of addressable memory in modern processors).

First proposed in #121486, then discussed in https://discuss.python.org/t/imposing-a-hard-limit-on-python-integers/62302.

Linked PRs

@serhiy-storchaka serhiy-storchaka added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Aug 30, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 30, 2024
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 64-bit integer.

Both limits are much larger than what can be available on practice, so
there is no effect on users.

_PyLong_NumBits() and _PyLong_Frexp() are now always successful.
@picnixz picnixz added the type-feature A feature request or enhancement label Aug 30, 2024
serhiy-storchaka added a commit that referenced this issue Sep 29, 2024
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.
@serhiy-storchaka
Copy link
Member Author

Merged the signed version, as this is what @encukou preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants