summaryrefslogtreecommitdiff
path: root/Lib/random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-07 10:06:56 +0000
committerRaymond Hettinger <python@rcn.com>2010-09-07 10:06:56 +0000
commitdc4872eefe2531e6627aa09818e95372a50e3b45 (patch)
tree0b61130e5de9917744671797d0ffab1b2036898c /Lib/random.py
parentc324697bac35d7151c93f3c64009b3ec17a053e7 (diff)
downloadcpython-git-dc4872eefe2531e6627aa09818e95372a50e3b45.tar.gz
Fix corner case for Random.choice() and add tests.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py6
1 files changed, 5 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 88b8f6d701..0886562a21 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -239,7 +239,11 @@ class Random(_random.Random):
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
- return seq[self._randbelow(len(seq))] # raises IndexError if seq is empty
+ try:
+ i = self._randbelow(len(seq))
+ except ValueError:
+ raise IndexError('Cannot choose from an empty sequence')
+ return seq[i]
def shuffle(self, x, random=None, int=int):
"""x, random=random.random -> shuffle list x in place; return None.