[Python-checkins] r64845 - in python/trunk: Lib/bisect.py Lib/test/test_bisect.py Misc/NEWS Modules/_bisectmodule.c

raymond.hettinger python-checkins at python.org
Thu Jul 10 16:03:19 CEST 2008


Author: raymond.hettinger
Date: Thu Jul 10 16:03:19 2008
New Revision: 64845

Log:
Issue 3301:  Bisect functions behaved badly when lo was negative.

Modified:
   python/trunk/Lib/bisect.py
   python/trunk/Lib/test/test_bisect.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_bisectmodule.c

Modified: python/trunk/Lib/bisect.py
==============================================================================
--- python/trunk/Lib/bisect.py	(original)
+++ python/trunk/Lib/bisect.py	Thu Jul 10 16:03:19 2008
@@ -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:

Modified: python/trunk/Lib/test/test_bisect.py
==============================================================================
--- python/trunk/Lib/test/test_bisect.py	(original)
+++ python/trunk/Lib/test/test_bisect.py	Thu Jul 10 16:03:19 2008
@@ -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):

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Thu Jul 10 16:03:19 2008
@@ -10,6 +10,7 @@
 Core and Builtins
 -----------------
 
+
 - Issue #2517: Allow unicode messages in Exceptions again by correctly
   bypassing the instance dictionary when looking up __unicode__ on
   new-style classes.
@@ -40,6 +41,8 @@
 Library
 -------
 
+- Issue #3301: Bisect module modules behaved badly when lo was negative.
+
 - Issue #839496: SimpleHTTPServer used to open text files in text mode. This is
   both unnecessary (HTTP allows text content to be sent in several forms) and
   wrong because the actual transmitted size could differ with the

Modified: python/trunk/Modules/_bisectmodule.c
==============================================================================
--- python/trunk/Modules/_bisectmodule.c	(original)
+++ python/trunk/Modules/_bisectmodule.c	Thu Jul 10 16:03:19 2008
@@ -11,6 +11,10 @@
 	PyObject *litem;
 	Py_ssize_t mid, res;
 
+	if (lo < 0) {
+		PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
+		return -1;
+	}
 	if (hi == -1) {
 		hi = PySequence_Size(list);
 		if (hi < 0)
@@ -108,6 +112,10 @@
 	PyObject *litem;
 	int mid, res;
 
+	if (lo < 0) {
+		PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
+		return -1;
+	}
 	if (hi == -1) {
 		hi = PySequence_Size(list);
 		if (hi < 0)


More information about the Python-checkins mailing list