Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

## Delta Between Two Patch Sets: Lib/test/test_slice.py

Issue 14794: slice.indices raises OverflowError
Left Patch Set: Created 7 years, 7 months ago
Right Patch Set: Created 7 years, 7 months ago
 Left: Base Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Right: Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Objects/sliceobject.c » ('j') | Objects/sliceobject.c » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Hide Comments ('s')
LEFTRIGHT
1 # tests for slice objects; in particular the indices method. 1 # tests for slice objects; in particular the indices method.
2 2
3 import unittest 3 import unittest
4 from test import support 4 from test import support
5 from pickle import loads, dumps 5 from pickle import loads, dumps
6 6
7 import itertools 7 import itertools
8 import operator 8 import operator
9 import sys 9 import sys
10 10
11 11
12 def evaluate_slice_index(arg):
13 """
14 Helper function to convert a slice argument to an integer, and raise
15 TypeError with a suitable message on failure.
16
17 """
18 if hasattr(arg, '__index__'):
19 return operator.index(arg)
20 else:
21 raise TypeError(
22 "slice indices must be integers or "
23 "None or have an __index__ method")
24
12 def slice_indices(slice, length): 25 def slice_indices(slice, length):
13 """ 26 """
14 Reference implementation for the slice.indices method. 27 Reference implementation for the slice.indices method.
15 28
16 """ 29 """
17 # Compute step and length as integers. 30 # Compute step and length as integers.
18 length = operator.index(length) 31 length = operator.index(length)
19 step = 1 if slice.step is None else operator.index(slice.step) 32 step = 1 if slice.step is None else evaluate_slice_index(slice.step)
20 33
21 # Raise ValueError for negative length or zero step. 34 # Raise ValueError for negative length or zero step.
22 if length < 0: 35 if length < 0:
23 raise ValueError("length should be nonnegative") 36 raise ValueError("length should not be negative")
24 if step == 0: 37 if step == 0:
25 raise ValueError("slice step cannot be zero") 38 raise ValueError("slice step cannot be zero")
26 39
27 # Get lower and upper bounds for start and stop. 40 # Find lower and upper bounds for start and stop.
28 lower = -1 if step < 0 else 0 41 lower = -1 if step < 0 else 0
29 upper = length - 1 if step < 0 else length 42 upper = length - 1 if step < 0 else length
30 43
31 # Compute start. 44 # Compute start.
32 if slice.start is None: 45 if slice.start is None:
33 start = upper if step < 0 else lower 46 start = upper if step < 0 else lower
34 else: 47 else:
35 start_as_integer = operator.index(slice.start) 48 start = evaluate_slice_index(slice.start)
36 if start_as_integer < 0: 49 start = max(start + length, lower) if start < 0 else min(start, upper)
37 start = max(start_as_integer + length, lower)
38 else:
39 start = min(start_as_integer, upper)
40 50
41 # Compute stop. 51 # Compute stop.
42 if slice.stop is None: 52 if slice.stop is None:
43 stop = lower if step < 0 else upper 53 stop = lower if step < 0 else upper
44 else: 54 else:
45 stop_as_integer = operator.index(slice.stop) 55 stop = evaluate_slice_index(slice.stop)
46 if stop_as_integer < 0: 56 stop = max(stop + length, lower) if stop < 0 else min(stop, upper)
47 stop = max(stop_as_integer + length, lower)
48 else:
49 stop = min(stop_as_integer, upper)
50 57
51 return start, stop, step 58 return start, stop, step
52 59
53 60
54 # Class providing an __index__ method. Used for testing slice.indices. 61 # Class providing an __index__ method. Used for testing slice.indices.
55 62
56 class MyIndexable(object): 63 class MyIndexable(object):
57 def __init__(self, value): 64 def __init__(self, value):
58 self.value = value 65 self.value = value
59 66
(...skipping 171 matching lines...)
231 t = loads(dumps(s, protocol)) 238 t = loads(dumps(s, protocol))
232 self.assertEqual(s, t) 239 self.assertEqual(s, t)
233 self.assertEqual(s.indices(15), t.indices(15)) 240 self.assertEqual(s.indices(15), t.indices(15))
234 self.assertNotEqual(id(s), id(t)) 241 self.assertNotEqual(id(s), id(t))
235 242
236 def test_main(): 243 def test_main():
237 support.run_unittest(SliceTest) 244 support.run_unittest(SliceTest)
238 245
239 if __name__ == "__main__": 246 if __name__ == "__main__":
240 test_main() 247 test_main()
LEFTRIGHT

This is Rietveld 894c83f36cb7+