bpo-33203: Ensure random.choice always raises IndexError on empty sequence (GH-6338) (GH-6387)

(cherry picked from commit 091e95e900)

Co-authored-by: Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
This commit is contained in:
Miss Islington (bot) 2018-04-05 09:02:12 -07:00 committed by Raymond Hettinger
parent 83f564fd24
commit baf304e82e
3 changed files with 9 additions and 1 deletions

View file

@ -242,6 +242,8 @@ class Random(_random.Random):
"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:
raise ValueError("Boundary cannot be zero")
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
r = random()