1 | n/a | # tests for slice objects; in particular the indices method. |
---|
2 | n/a | |
---|
3 | n/a | import itertools |
---|
4 | n/a | import operator |
---|
5 | n/a | import sys |
---|
6 | n/a | import unittest |
---|
7 | n/a | import weakref |
---|
8 | n/a | |
---|
9 | n/a | from pickle import loads, dumps |
---|
10 | n/a | from test import support |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | def evaluate_slice_index(arg): |
---|
14 | n/a | """ |
---|
15 | n/a | Helper function to convert a slice argument to an integer, and raise |
---|
16 | n/a | TypeError with a suitable message on failure. |
---|
17 | n/a | |
---|
18 | n/a | """ |
---|
19 | n/a | if hasattr(arg, '__index__'): |
---|
20 | n/a | return operator.index(arg) |
---|
21 | n/a | else: |
---|
22 | n/a | raise TypeError( |
---|
23 | n/a | "slice indices must be integers or " |
---|
24 | n/a | "None or have an __index__ method") |
---|
25 | n/a | |
---|
26 | n/a | def slice_indices(slice, length): |
---|
27 | n/a | """ |
---|
28 | n/a | Reference implementation for the slice.indices method. |
---|
29 | n/a | |
---|
30 | n/a | """ |
---|
31 | n/a | # Compute step and length as integers. |
---|
32 | n/a | length = operator.index(length) |
---|
33 | n/a | step = 1 if slice.step is None else evaluate_slice_index(slice.step) |
---|
34 | n/a | |
---|
35 | n/a | # Raise ValueError for negative length or zero step. |
---|
36 | n/a | if length < 0: |
---|
37 | n/a | raise ValueError("length should not be negative") |
---|
38 | n/a | if step == 0: |
---|
39 | n/a | raise ValueError("slice step cannot be zero") |
---|
40 | n/a | |
---|
41 | n/a | # Find lower and upper bounds for start and stop. |
---|
42 | n/a | lower = -1 if step < 0 else 0 |
---|
43 | n/a | upper = length - 1 if step < 0 else length |
---|
44 | n/a | |
---|
45 | n/a | # Compute start. |
---|
46 | n/a | if slice.start is None: |
---|
47 | n/a | start = upper if step < 0 else lower |
---|
48 | n/a | else: |
---|
49 | n/a | start = evaluate_slice_index(slice.start) |
---|
50 | n/a | start = max(start + length, lower) if start < 0 else min(start, upper) |
---|
51 | n/a | |
---|
52 | n/a | # Compute stop. |
---|
53 | n/a | if slice.stop is None: |
---|
54 | n/a | stop = lower if step < 0 else upper |
---|
55 | n/a | else: |
---|
56 | n/a | stop = evaluate_slice_index(slice.stop) |
---|
57 | n/a | stop = max(stop + length, lower) if stop < 0 else min(stop, upper) |
---|
58 | n/a | |
---|
59 | n/a | return start, stop, step |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | # Class providing an __index__ method. Used for testing slice.indices. |
---|
63 | n/a | |
---|
64 | n/a | class MyIndexable(object): |
---|
65 | n/a | def __init__(self, value): |
---|
66 | n/a | self.value = value |
---|
67 | n/a | |
---|
68 | n/a | def __index__(self): |
---|
69 | n/a | return self.value |
---|
70 | n/a | |
---|
71 | n/a | |
---|
72 | n/a | class SliceTest(unittest.TestCase): |
---|
73 | n/a | |
---|
74 | n/a | def test_constructor(self): |
---|
75 | n/a | self.assertRaises(TypeError, slice) |
---|
76 | n/a | self.assertRaises(TypeError, slice, 1, 2, 3, 4) |
---|
77 | n/a | |
---|
78 | n/a | def test_repr(self): |
---|
79 | n/a | self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)") |
---|
80 | n/a | |
---|
81 | n/a | def test_hash(self): |
---|
82 | n/a | # Verify clearing of SF bug #800796 |
---|
83 | n/a | self.assertRaises(TypeError, hash, slice(5)) |
---|
84 | n/a | with self.assertRaises(TypeError): |
---|
85 | n/a | slice(5).__hash__() |
---|
86 | n/a | |
---|
87 | n/a | def test_cmp(self): |
---|
88 | n/a | s1 = slice(1, 2, 3) |
---|
89 | n/a | s2 = slice(1, 2, 3) |
---|
90 | n/a | s3 = slice(1, 2, 4) |
---|
91 | n/a | self.assertEqual(s1, s2) |
---|
92 | n/a | self.assertNotEqual(s1, s3) |
---|
93 | n/a | self.assertNotEqual(s1, None) |
---|
94 | n/a | self.assertNotEqual(s1, (1, 2, 3)) |
---|
95 | n/a | self.assertNotEqual(s1, "") |
---|
96 | n/a | |
---|
97 | n/a | class Exc(Exception): |
---|
98 | n/a | pass |
---|
99 | n/a | |
---|
100 | n/a | class BadCmp(object): |
---|
101 | n/a | def __eq__(self, other): |
---|
102 | n/a | raise Exc |
---|
103 | n/a | |
---|
104 | n/a | s1 = slice(BadCmp()) |
---|
105 | n/a | s2 = slice(BadCmp()) |
---|
106 | n/a | self.assertEqual(s1, s1) |
---|
107 | n/a | self.assertRaises(Exc, lambda: s1 == s2) |
---|
108 | n/a | |
---|
109 | n/a | s1 = slice(1, BadCmp()) |
---|
110 | n/a | s2 = slice(1, BadCmp()) |
---|
111 | n/a | self.assertEqual(s1, s1) |
---|
112 | n/a | self.assertRaises(Exc, lambda: s1 == s2) |
---|
113 | n/a | |
---|
114 | n/a | s1 = slice(1, 2, BadCmp()) |
---|
115 | n/a | s2 = slice(1, 2, BadCmp()) |
---|
116 | n/a | self.assertEqual(s1, s1) |
---|
117 | n/a | self.assertRaises(Exc, lambda: s1 == s2) |
---|
118 | n/a | |
---|
119 | n/a | def test_members(self): |
---|
120 | n/a | s = slice(1) |
---|
121 | n/a | self.assertEqual(s.start, None) |
---|
122 | n/a | self.assertEqual(s.stop, 1) |
---|
123 | n/a | self.assertEqual(s.step, None) |
---|
124 | n/a | |
---|
125 | n/a | s = slice(1, 2) |
---|
126 | n/a | self.assertEqual(s.start, 1) |
---|
127 | n/a | self.assertEqual(s.stop, 2) |
---|
128 | n/a | self.assertEqual(s.step, None) |
---|
129 | n/a | |
---|
130 | n/a | s = slice(1, 2, 3) |
---|
131 | n/a | self.assertEqual(s.start, 1) |
---|
132 | n/a | self.assertEqual(s.stop, 2) |
---|
133 | n/a | self.assertEqual(s.step, 3) |
---|
134 | n/a | |
---|
135 | n/a | class AnyClass: |
---|
136 | n/a | pass |
---|
137 | n/a | |
---|
138 | n/a | obj = AnyClass() |
---|
139 | n/a | s = slice(obj) |
---|
140 | n/a | self.assertTrue(s.stop is obj) |
---|
141 | n/a | |
---|
142 | n/a | def check_indices(self, slice, length): |
---|
143 | n/a | try: |
---|
144 | n/a | actual = slice.indices(length) |
---|
145 | n/a | except ValueError: |
---|
146 | n/a | actual = "valueerror" |
---|
147 | n/a | try: |
---|
148 | n/a | expected = slice_indices(slice, length) |
---|
149 | n/a | except ValueError: |
---|
150 | n/a | expected = "valueerror" |
---|
151 | n/a | self.assertEqual(actual, expected) |
---|
152 | n/a | |
---|
153 | n/a | if length >= 0 and slice.step != 0: |
---|
154 | n/a | actual = range(*slice.indices(length)) |
---|
155 | n/a | expected = range(length)[slice] |
---|
156 | n/a | self.assertEqual(actual, expected) |
---|
157 | n/a | |
---|
158 | n/a | def test_indices(self): |
---|
159 | n/a | self.assertEqual(slice(None ).indices(10), (0, 10, 1)) |
---|
160 | n/a | self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2)) |
---|
161 | n/a | self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2)) |
---|
162 | n/a | self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1)) |
---|
163 | n/a | self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2)) |
---|
164 | n/a | self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2)) |
---|
165 | n/a | # issue 3004 tests |
---|
166 | n/a | self.assertEqual(slice(None, -9).indices(10), (0, 1, 1)) |
---|
167 | n/a | self.assertEqual(slice(None, -10).indices(10), (0, 0, 1)) |
---|
168 | n/a | self.assertEqual(slice(None, -11).indices(10), (0, 0, 1)) |
---|
169 | n/a | self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1)) |
---|
170 | n/a | self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1)) |
---|
171 | n/a | self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1)) |
---|
172 | n/a | self.assertEqual(slice(None, 9).indices(10), (0, 9, 1)) |
---|
173 | n/a | self.assertEqual(slice(None, 10).indices(10), (0, 10, 1)) |
---|
174 | n/a | self.assertEqual(slice(None, 11).indices(10), (0, 10, 1)) |
---|
175 | n/a | self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1)) |
---|
176 | n/a | self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1)) |
---|
177 | n/a | self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1)) |
---|
178 | n/a | |
---|
179 | n/a | self.assertEqual( |
---|
180 | n/a | slice(-100, 100 ).indices(10), |
---|
181 | n/a | slice(None).indices(10) |
---|
182 | n/a | ) |
---|
183 | n/a | self.assertEqual( |
---|
184 | n/a | slice(100, -100, -1).indices(10), |
---|
185 | n/a | slice(None, None, -1).indices(10) |
---|
186 | n/a | ) |
---|
187 | n/a | self.assertEqual(slice(-100, 100, 2).indices(10), (0, 10, 2)) |
---|
188 | n/a | |
---|
189 | n/a | self.assertEqual(list(range(10))[::sys.maxsize - 1], [0]) |
---|
190 | n/a | |
---|
191 | n/a | # Check a variety of start, stop, step and length values, including |
---|
192 | n/a | # values exceeding sys.maxsize (see issue #14794). |
---|
193 | n/a | vals = [None, -2**100, -2**30, -53, -7, -1, 0, 1, 7, 53, 2**30, 2**100] |
---|
194 | n/a | lengths = [0, 1, 7, 53, 2**30, 2**100] |
---|
195 | n/a | for slice_args in itertools.product(vals, repeat=3): |
---|
196 | n/a | s = slice(*slice_args) |
---|
197 | n/a | for length in lengths: |
---|
198 | n/a | self.check_indices(s, length) |
---|
199 | n/a | self.check_indices(slice(0, 10, 1), -3) |
---|
200 | n/a | |
---|
201 | n/a | # Negative length should raise ValueError |
---|
202 | n/a | with self.assertRaises(ValueError): |
---|
203 | n/a | slice(None).indices(-1) |
---|
204 | n/a | |
---|
205 | n/a | # Zero step should raise ValueError |
---|
206 | n/a | with self.assertRaises(ValueError): |
---|
207 | n/a | slice(0, 10, 0).indices(5) |
---|
208 | n/a | |
---|
209 | n/a | # Using a start, stop or step or length that can't be interpreted as an |
---|
210 | n/a | # integer should give a TypeError ... |
---|
211 | n/a | with self.assertRaises(TypeError): |
---|
212 | n/a | slice(0.0, 10, 1).indices(5) |
---|
213 | n/a | with self.assertRaises(TypeError): |
---|
214 | n/a | slice(0, 10.0, 1).indices(5) |
---|
215 | n/a | with self.assertRaises(TypeError): |
---|
216 | n/a | slice(0, 10, 1.0).indices(5) |
---|
217 | n/a | with self.assertRaises(TypeError): |
---|
218 | n/a | slice(0, 10, 1).indices(5.0) |
---|
219 | n/a | |
---|
220 | n/a | # ... but it should be fine to use a custom class that provides index. |
---|
221 | n/a | self.assertEqual(slice(0, 10, 1).indices(5), (0, 5, 1)) |
---|
222 | n/a | self.assertEqual(slice(MyIndexable(0), 10, 1).indices(5), (0, 5, 1)) |
---|
223 | n/a | self.assertEqual(slice(0, MyIndexable(10), 1).indices(5), (0, 5, 1)) |
---|
224 | n/a | self.assertEqual(slice(0, 10, MyIndexable(1)).indices(5), (0, 5, 1)) |
---|
225 | n/a | self.assertEqual(slice(0, 10, 1).indices(MyIndexable(5)), (0, 5, 1)) |
---|
226 | n/a | |
---|
227 | n/a | def test_setslice_without_getslice(self): |
---|
228 | n/a | tmp = [] |
---|
229 | n/a | class X(object): |
---|
230 | n/a | def __setitem__(self, i, k): |
---|
231 | n/a | tmp.append((i, k)) |
---|
232 | n/a | |
---|
233 | n/a | x = X() |
---|
234 | n/a | x[1:2] = 42 |
---|
235 | n/a | self.assertEqual(tmp, [(slice(1, 2), 42)]) |
---|
236 | n/a | |
---|
237 | n/a | def test_pickle(self): |
---|
238 | n/a | s = slice(10, 20, 3) |
---|
239 | n/a | for protocol in (0,1,2): |
---|
240 | n/a | t = loads(dumps(s, protocol)) |
---|
241 | n/a | self.assertEqual(s, t) |
---|
242 | n/a | self.assertEqual(s.indices(15), t.indices(15)) |
---|
243 | n/a | self.assertNotEqual(id(s), id(t)) |
---|
244 | n/a | |
---|
245 | n/a | def test_cycle(self): |
---|
246 | n/a | class myobj(): pass |
---|
247 | n/a | o = myobj() |
---|
248 | n/a | o.s = slice(o) |
---|
249 | n/a | w = weakref.ref(o) |
---|
250 | n/a | o = None |
---|
251 | n/a | support.gc_collect() |
---|
252 | n/a | self.assertIsNone(w()) |
---|
253 | n/a | |
---|
254 | n/a | if __name__ == "__main__": |
---|
255 | n/a | unittest.main() |
---|