-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-33203: Ensure random.choice always raises IndexError on empty sequence #6338
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
Conversation
Without this patch a ZeroDivisionError was leaked for Random subclasses overriding the random, but not the getrandbits method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there high-level tests for choice([])
and other implicit calls of _randbelow(0)
?
@@ -242,6 +242,8 @@ def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type, | |||
"enough bits to choose from a population range this large.\n" | |||
"To remove the range limitation, add a getrandbits() method.") | |||
return int(random() * n) | |||
if n == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 8 suggests not n
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition n == 0
mirrors the docstring, Raises ValueError if n==0."
I'm not aware of any other place calling |
If running code with the |
@selik: I'm not sure I understand the problem with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all looks fine to me. Please add a NEWS blurb.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
@wm75 Sorry, I misread the |
@rhettinger I have made the requested changes; please review again |
Thanks for making the requested changes! @rhettinger: please review the changes made to this pull request. |
Thanks @wm75 for the PR, and @rhettinger for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7. |
GH-6387 is a backport of this pull request to the 3.7 branch. |
…uence (pythonGH-6338) (cherry picked from commit 091e95e) Co-authored-by: Wolfgang Maier <[email protected]>
…uence (pythonGH-6338) (cherry picked from commit 091e95e) Co-authored-by: Wolfgang Maier <[email protected]>
GH-6388 is a backport of this pull request to the 3.6 branch. |
…uence (GH-6338) (GH-6387) (cherry picked from commit 091e95e) Co-authored-by: Wolfgang Maier <[email protected]>
…uence (GH-6338) (GH-6388) (cherry picked from commit 091e95e) Co-authored-by: Wolfgang Maier <[email protected]>
Without this patch a ZeroDivisionError was leaked for Random
subclasses overriding the random, but not the getrandbits method.
https://bugs.python.org/issue33203