Issue 3301: Bisect functions behaved badly when lo was negative.
diff --git a/Lib/bisect.py b/Lib/bisect.py
index e4a2133..fe84255 100644
--- a/Lib/bisect.py
+++ b/Lib/bisect.py
@@ -9,6 +9,8 @@
slice of a to be searched.
"""
+ if lo < 0:
+ raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
@@ -30,6 +32,8 @@
slice of a to be searched.
"""
+ if lo < 0:
+ raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
@@ -49,6 +53,8 @@
slice of a to be searched.
"""
+ if lo < 0:
+ raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
@@ -69,6 +75,8 @@
slice of a to be searched.
"""
+ if lo < 0:
+ raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py
index fb490b7..7776cc8 100644
--- a/Lib/test/test_bisect.py
+++ b/Lib/test/test_bisect.py
@@ -114,6 +114,14 @@
self.assertEqual(func(data, elem), expected)
self.assertEqual(func(UserList(data), elem), expected)
+ def test_negative_lo(self):
+ # Issue 3301
+ mod = self.module
+ self.assertRaises(ValueError, mod.bisect_left, [1, 2, 3], 5, -1, 3),
+ self.assertRaises(ValueError, mod.bisect_right, [1, 2, 3], 5, -1, 3),
+ self.assertRaises(ValueError, mod.insort_left, [1, 2, 3], 5, -1, 3),
+ self.assertRaises(ValueError, mod.insort_right, [1, 2, 3], 5, -1, 3),
+
def test_random(self, n=25):
from random import randrange
for i in xrange(n):