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

Skip to content

Commit c992faf

Browse files
committed
Issue #16402: Merge fix from 3.3
2 parents 729eda4 + 1321eda commit c992faf

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(range(10)[:I(5)], 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.4.0 Alpha 1?
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
@@ -331,11 +331,11 @@ compute_slice_element(PyObject *obj)
331331
if (PyIndex_Check(obj)) {
332332
result = PyNumber_Index(obj);
333333
}
334-
}
335-
if (result == NULL) {
336-
PyErr_SetString(PyExc_TypeError,
337-
"slice indices must be integers or "
338-
"None or have an __index__ method");
334+
else {
335+
PyErr_SetString(PyExc_TypeError,
336+
"slice indices must be integers or "
337+
"None or have an __index__ method");
338+
}
339339
}
340340
return result;
341341
}

0 commit comments

Comments
 (0)