Skip to content

bpo-33203: Ensure random.choice always raises IndexError on empty sequence#6338

Merged
rhettinger merged 2 commits into
python:masterfrom
wm75:choices-fix
Apr 5, 2018
Merged

bpo-33203: Ensure random.choice always raises IndexError on empty sequence#6338
rhettinger merged 2 commits into
python:masterfrom
wm75:choices-fix

Conversation

@wm75

@wm75 wm75 commented Apr 1, 2018

Copy link
Copy Markdown

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.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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)?

Comment thread Lib/random.py
"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
Copy Markdown
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.

@selik selik Apr 2, 2018

Copy link
Copy Markdown
Contributor

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

wm75 commented Apr 1, 2018

Copy link
Copy Markdown
Author

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

selik commented Apr 2, 2018

Copy link
Copy Markdown
Contributor

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

wm75 commented Apr 3, 2018

Copy link
Copy Markdown
Author

@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?

@rhettinger rhettinger left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown

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

selik commented Apr 5, 2018

Copy link
Copy Markdown
Contributor

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

@wm75

wm75 commented Apr 5, 2018

Copy link
Copy Markdown
Author

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

@bedevere-bot

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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 <wolfgang.maier@biologie.uni-freiburg.de>
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 <wolfgang.maier@biologie.uni-freiburg.de>
@bedevere-bot

Copy link
Copy Markdown

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 <wolfgang.maier@biologie.uni-freiburg.de>
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 <wolfgang.maier@biologie.uni-freiburg.de>
@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