Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8cd1c76

Browse files
committed
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
1 parent b87f82f commit 8cd1c76

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

Lib/test/test_range.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ def __index__(self):
312312

313313
self.assertRaises(TypeError, range, IN())
314314

315+
# Test use of user-defined classes in slice indices.
316+
self.assertEqual(list(range(10)[:I(5)]), list(range(5)))
317+
318+
with self.assertRaises(RuntimeError):
319+
range(0, 10)[:IX()]
320+
321+
with self.assertRaises(TypeError):
322+
range(0, 10)[:IN()]
323+
315324
def test_count(self):
316325
self.assertEqual(range(3).count(-1), 0)
317326
self.assertEqual(range(3).count(0), 1)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #16402: When slicing a range, fix shadowing of exceptions from
14+
__index__.
15+
1316
- Issue #16336: fix input checking in the surrogatepass error handler.
1417
Patch by Serhiy Storchaka.
1518

Objects/rangeobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ compute_slice_element(PyObject *obj)
330330
if (PyIndex_Check(obj)) {
331331
result = PyNumber_Index(obj);
332332
}
333-
}
334-
if (result == NULL) {
335-
PyErr_SetString(PyExc_TypeError,
336-
"slice indices must be integers or "
337-
"None or have an __index__ method");
333+
else {
334+
PyErr_SetString(PyExc_TypeError,
335+
"slice indices must be integers or "
336+
"None or have an __index__ method");
337+
}
338338
}
339339
return result;
340340
}

0 commit comments

Comments
 (0)