Skip to content

Remove unnecessary and over-restrictive type check in SystemRandom.getrandbits() #10905

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

Merged
merged 1 commit into from
Dec 4, 2018

Conversation

rhettinger
Copy link
Contributor

  • The type check is over-restrictive (i.e. doesn't allow int subclasses).
  • The type check is unnecessary:
    • We already that know k can be compared to 0
    • In the subsequent step, urandom() verifies that (k + 7) // 8 is an instance of integer, and if not will raise a TypeError for us.
  • The code is performance sensitive (called in the inner loops for shuffle(), sample(), and choices()).

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I confirm that os.urandom(float) raises a TypeError. I also checked and test_random already ensures that getrandbits(float) raises a TypeError, so this change LGTM.

nitpick: you might just try to write a shorter title for the commit when you merge your change :-)

@rhettinger rhettinger merged commit 09473ac into python:master Dec 4, 2018
@rhettinger rhettinger deleted the random_cleanup branch December 4, 2018 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants