]> granicus.if.org Git - python/commitdiff
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
authorMark Dickinson <mdickinson@enthought.com>
Sun, 4 Nov 2012 11:46:17 +0000 (11:46 +0000)
committerMark Dickinson <mdickinson@enthought.com>
Sun, 4 Nov 2012 11:46:17 +0000 (11:46 +0000)
Lib/test/test_range.py
Misc/NEWS
Objects/rangeobject.c

index ede07912b0cfb304c4929a5b86f003a46e6a4a0b..4dab448d5a08ec641aa9c5e2f80c85e1d7db1305 100644 (file)
@@ -312,6 +312,15 @@ class RangeTest(unittest.TestCase):
 
         self.assertRaises(TypeError, range, IN())
 
+        # Test use of user-defined classes in slice indices.
+        self.assertEqual(list(range(10)[:I(5)]), list(range(5)))
+
+        with self.assertRaises(RuntimeError):
+            range(0, 10)[:IX()]
+
+        with self.assertRaises(TypeError):
+            range(0, 10)[:IN()]
+
     def test_count(self):
         self.assertEqual(range(3).count(-1), 0)
         self.assertEqual(range(3).count(0), 1)
index 565ebcde8f24661dd67ddf2e13125ea05cb343f0..8d6035e189514b16962dff9c4329a3b4d2ef6570 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #16402: When slicing a range, fix shadowing of exceptions from
+  __index__.
+
 - Issue #16336: fix input checking in the surrogatepass error handler.
   Patch by Serhiy Storchaka.
 
index b67b9694d58aaacea0c210c7e1237b715a84d599..cebccff9556eb4c43c471bf3a92ab8a19a146441 100644 (file)
@@ -330,11 +330,11 @@ compute_slice_element(PyObject *obj)
         if (PyIndex_Check(obj)) {
             result = PyNumber_Index(obj);
         }
-    }
-    if (result == NULL) {
-        PyErr_SetString(PyExc_TypeError,
-                        "slice indices must be integers or "
-                        "None or have an __index__ method");
+        else {
+            PyErr_SetString(PyExc_TypeError,
+                            "slice indices must be integers or "
+                            "None or have an __index__ method");
+        }
     }
     return result;
 }