From 8cd1c7681d86fd3d32096f2d927d3f5014d58c36 Mon Sep 17 00:00:00 2001
From: Mark Dickinson <mdickinson@enthought.com>
Date: Sun, 4 Nov 2012 11:46:17 +0000
Subject: Issue #16402: In range slicing, fix shadowing of exceptions from
 __index__ method.

---
 Lib/test/test_range.py |  9 +++++++++
 Misc/NEWS              |  3 +++
 Objects/rangeobject.c  | 10 +++++-----
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
index ede0791..4dab448 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -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)
diff --git a/Misc/NEWS b/Misc/NEWS
index 565ebcd..8d6035e 100644
--- 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.
 
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index b67b969..cebccff 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -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;
 }
-- 
cgit v0.12

