Skip to content

regression in 3.13.4 Random.getrandbits when an int like object is passed #135326

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

Open
gpshead opened this issue Jun 10, 2025 · 2 comments
Open
Assignees
Labels
3.13 bugs and security fixes release-blocker type-bug An unexpected behavior, bug, or error

Comments

@gpshead
Copy link
Member

gpshead commented Jun 10, 2025

Bug report

Bug description:

via #133489 (comment) @tornaria reports:

There is an (unintended?) api break. In python 3.13.3, getrandbits() accepts an "integer-like" object as in:

Python 3.13.3 (main, Apr 11 2025, 13:41:26) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import random ; r = random.Random()
>>> from numpy import int64
>>> r.getrandbits(int64(20))
582096

but in python 3.13.4, getrandbits() fails as in:

Python 3.13.4 (main, Jun  8 2025, 19:43:01) [GCC 14.2.1 20250405] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import random ; r = random.Random()
>>> from numpy import int64
>>> r.getrandbits(int64(20))
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    r.getrandbits(int64(20))
    ~~~~~~~~~~~~~^^^^^^^^^^^
TypeError: an integer is required

This causes trouble for sagemath, where integers are by default a special type ZZ.

CPython versions tested on:

3.13

Operating systems tested on:

No response

Linked PRs

@gpshead gpshead added the type-bug An unexpected behavior, bug, or error label Jun 10, 2025
@gpshead gpshead changed the title regression in 3.13.4 Random.getrandbits when an int subclass is passed regression in 3.13.4 Random.getrandbits when an int like object is passed Jun 10, 2025
gpshead added a commit to gpshead/cpython that referenced this issue Jun 10, 2025
…ions on getrandbits() and randbytes() (pythonGH-133658) (pythonGH-134965)"

This reverts commit 3e1b8d6.

This caused a regression in Python 3.13.4, getrandbit() no longer accepted an integer-like object via `__index__`.
@gpshead gpshead added 3.13 bugs and security fixes release-blocker labels Jun 10, 2025
@gpshead
Copy link
Member Author

gpshead commented Jun 10, 2025

I went for a revert of the change. Easiest. We can live without that old bug being fixed in 3.13. The fix works in 3.14+.

@serhiy-storchaka
Copy link
Member

#135332 restores support without rolling back all changes.

Note that SystemRandom.getrandbits() does not support __index__, and never did. On other hand, SystemRandom.randbytes() supports __index__, but Random.randbytes() does not. Some other methods may support __index__, but currently there are no tests for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes release-blocker type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

2 participants