From 091e95e9004b794280ab35becec2c3e30dd5e96e Mon Sep 17 00:00:00 2001 From: Wolfgang Maier Date: Thu, 5 Apr 2018 17:19:44 +0200 Subject: bpo-33203: Ensure random.choice always raises IndexError on empty sequence (GH-6338) --- Lib/random.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'Lib/random.py') diff --git a/Lib/random.py b/Lib/random.py index 91065b7e30..0bc24174e1 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -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() -- cgit v1.2.1