Skip to content

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

Closed

Description

@gpshead

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

Activity

  1. 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[/+] on Jun 10, 2025
  2. added a commit that references this issue on Jun 10, 2025
  3. gpshead commented on Jun 10, 2025

    @gpshead
    MemberAuthor

    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+.

  4. self-assigned this
    on Jun 10, 2025
  5. added a commit that references this issue on Jun 10, 2025
  6. serhiy-storchaka commented on Jun 10, 2025

    @serhiy-storchaka
    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.

  7. added a commit that references this issue on Jun 10, 2025
  8. added a commit that references this issue on Jun 10, 2025
  9. added a commit that references this issue on Jun 11, 2025
  10. added a commit that references this issue on Jun 11, 2025
  11. gpshead commented on Jun 11, 2025

    @gpshead
    MemberAuthor

    thanks! ❤

  12. added a commit that references this issue on Jun 11, 2025
  13. added a commit that references this issue on Jun 11, 2025
  14. added a commit that references this issue on Jun 11, 2025
  15. added a commit that references this issue on Jun 19, 2025
  16. added a commit that references this issue on Jun 29, 2025
  17. added a commit that references this issue on Jul 12, 2025
  18. added a commit that references this issue on Aug 4, 2025
  19. added a commit that references this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.13only security fixesrelease-blockertype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions