From 3a408f3dd948073183dd04c530ea7318f349d7dd Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sun, 10 Oct 2004 03:39:59 +0000 Subject: [PATCH] Fixed our lack of raising IndexError on random.choice applied to sequences. --- src/fix.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fix.py b/src/fix.py index bc44244f4..87b679bdf 100644 --- a/src/fix.py +++ b/src/fix.py @@ -53,11 +53,14 @@ def choice(iterable): return _choice(iterable) else: n = 1 - ret = None + m = new.module('') # Guaranteed unique value. + ret = m for x in iterable: if random.random() < 1/n: ret = x n += 1 + if ret is m: + raise IndexError return ret random.choice = choice