Skip to content

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

Merged
merged 2 commits into from
Apr 5, 2018

Conversation

wm75
Copy link

@wm75 wm75 commented Apr 1, 2018

Without this patch a ZeroDivisionError was leaked for Random
subclasses overriding the random, but not the getrandbits method.

https://bugs.python.org/issue33203

Without this patch a ZeroDivisionError was leaked for Random
subclasses overriding the random, but not the getrandbits method.
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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:
Copy link
Member

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.

Copy link
Contributor

@selik selik Apr 2, 2018

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

@wm75
Copy link
Author

wm75 commented Apr 1, 2018

I'm not aware of any other place calling _randbelow(0) implicitly. test_choice is currently only defined in TestBasicOps, which seems a less ideal place to patch random?

@selik
Copy link
Contributor

selik commented Apr 2, 2018

If running code with the -W error option, when the sequence is very large this will not reach the if n == 0 condition. Perhaps it would be better to place the check for zero above the check for maxsize?

@rhettinger rhettinger self-assigned this Apr 2, 2018
@wm75
Copy link
Author

wm75 commented Apr 3, 2018

@selik: I'm not sure I understand the problem with the -W error option because, if a sequence is very large, how can n be zero then?

Copy link
Contributor

@rhettinger rhettinger left a 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.

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@selik
Copy link
Contributor

selik commented Apr 5, 2018

@wm75 Sorry, I misread the n >= maxsize condition. If n is zero, it won't be greater than maxsize, obviously.

@wm75
Copy link
Author

wm75 commented Apr 5, 2018

@rhettinger I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@rhettinger: please review the changes made to this pull request.

@rhettinger rhettinger merged commit 091e95e into python:master Apr 5, 2018
@miss-islington
Copy link
Contributor

Thanks @wm75 for the PR, and @rhettinger for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-6387 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 5, 2018
…uence (pythonGH-6338)

(cherry picked from commit 091e95e)

Co-authored-by: Wolfgang Maier <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 5, 2018
…uence (pythonGH-6338)

(cherry picked from commit 091e95e)

Co-authored-by: Wolfgang Maier <[email protected]>
@bedevere-bot
Copy link

GH-6388 is a backport of this pull request to the 3.6 branch.

rhettinger pushed a commit that referenced this pull request Apr 5, 2018
…uence (GH-6338) (GH-6387)

(cherry picked from commit 091e95e)

Co-authored-by: Wolfgang Maier <[email protected]>
rhettinger pushed a commit that referenced this pull request Apr 5, 2018
…uence (GH-6338) (GH-6388)

(cherry picked from commit 091e95e)

Co-authored-by: Wolfgang Maier <[email protected]>
@wm75 wm75 deleted the choices-fix branch April 9, 2018 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants