| 1 | n/a | """ Test Iterator Length Transparency |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Some functions or methods which accept general iterable arguments have |
|---|
| 4 | n/a | optional, more efficient code paths if they know how many items to expect. |
|---|
| 5 | n/a | For instance, map(func, iterable), will pre-allocate the exact amount of |
|---|
| 6 | n/a | space required whenever the iterable can report its length. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | The desired invariant is: len(it)==len(list(it)). |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | A complication is that an iterable and iterator can be the same object. To |
|---|
| 11 | n/a | maintain the invariant, an iterator needs to dynamically update its length. |
|---|
| 12 | n/a | For instance, an iterable such as range(10) always reports its length as ten, |
|---|
| 13 | n/a | but it=iter(range(10)) starts at ten, and then goes to nine after next(it). |
|---|
| 14 | n/a | Having this capability means that map() can ignore the distinction between |
|---|
| 15 | n/a | map(func, iterable) and map(func, iter(iterable)). |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | When the iterable is immutable, the implementation can straight-forwardly |
|---|
| 18 | n/a | report the original length minus the cumulative number of calls to next(). |
|---|
| 19 | n/a | This is the case for tuples, range objects, and itertools.repeat(). |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | Some containers become temporarily immutable during iteration. This includes |
|---|
| 22 | n/a | dicts, sets, and collections.deque. Their implementation is equally simple |
|---|
| 23 | n/a | though they need to permanently set their length to zero whenever there is |
|---|
| 24 | n/a | an attempt to iterate after a length mutation. |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | The situation slightly more involved whenever an object allows length mutation |
|---|
| 27 | n/a | during iteration. Lists and sequence iterators are dynamically updatable. |
|---|
| 28 | n/a | So, if a list is extended during iteration, the iterator will continue through |
|---|
| 29 | n/a | the new items. If it shrinks to a point before the most recent iteration, |
|---|
| 30 | n/a | then no further items are available and the length is reported at zero. |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | Reversed objects can also be wrapped around mutable objects; however, any |
|---|
| 33 | n/a | appends after the current position are ignored. Any other approach leads |
|---|
| 34 | n/a | to confusion and possibly returning the same item more than once. |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | The iterators not listed above, such as enumerate and the other itertools, |
|---|
| 37 | n/a | are not length transparent because they have no way to distinguish between |
|---|
| 38 | n/a | iterables that report static length and iterators whose length changes with |
|---|
| 39 | n/a | each call (i.e. the difference between enumerate('abc') and |
|---|
| 40 | n/a | enumerate(iter('abc')). |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | """ |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | import unittest |
|---|
| 45 | n/a | from itertools import repeat |
|---|
| 46 | n/a | from collections import deque |
|---|
| 47 | n/a | from operator import length_hint |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | n = 10 |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | class TestInvariantWithoutMutations: |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def test_invariant(self): |
|---|
| 55 | n/a | it = self.it |
|---|
| 56 | n/a | for i in reversed(range(1, n+1)): |
|---|
| 57 | n/a | self.assertEqual(length_hint(it), i) |
|---|
| 58 | n/a | next(it) |
|---|
| 59 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 60 | n/a | self.assertRaises(StopIteration, next, it) |
|---|
| 61 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | class TestTemporarilyImmutable(TestInvariantWithoutMutations): |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def test_immutable_during_iteration(self): |
|---|
| 66 | n/a | # objects such as deques, sets, and dictionaries enforce |
|---|
| 67 | n/a | # length immutability during iteration |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | it = self.it |
|---|
| 70 | n/a | self.assertEqual(length_hint(it), n) |
|---|
| 71 | n/a | next(it) |
|---|
| 72 | n/a | self.assertEqual(length_hint(it), n-1) |
|---|
| 73 | n/a | self.mutate() |
|---|
| 74 | n/a | self.assertRaises(RuntimeError, next, it) |
|---|
| 75 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | ## ------- Concrete Type Tests ------- |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | class TestRepeat(TestInvariantWithoutMutations, unittest.TestCase): |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def setUp(self): |
|---|
| 82 | n/a | self.it = repeat(None, n) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | class TestXrange(TestInvariantWithoutMutations, unittest.TestCase): |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def setUp(self): |
|---|
| 87 | n/a | self.it = iter(range(n)) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | class TestXrangeCustomReversed(TestInvariantWithoutMutations, unittest.TestCase): |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | def setUp(self): |
|---|
| 92 | n/a | self.it = reversed(range(n)) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | class TestTuple(TestInvariantWithoutMutations, unittest.TestCase): |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | def setUp(self): |
|---|
| 97 | n/a | self.it = iter(tuple(range(n))) |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | ## ------- Types that should not be mutated during iteration ------- |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | class TestDeque(TestTemporarilyImmutable, unittest.TestCase): |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | def setUp(self): |
|---|
| 104 | n/a | d = deque(range(n)) |
|---|
| 105 | n/a | self.it = iter(d) |
|---|
| 106 | n/a | self.mutate = d.pop |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | class TestDequeReversed(TestTemporarilyImmutable, unittest.TestCase): |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def setUp(self): |
|---|
| 111 | n/a | d = deque(range(n)) |
|---|
| 112 | n/a | self.it = reversed(d) |
|---|
| 113 | n/a | self.mutate = d.pop |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | class TestDictKeys(TestTemporarilyImmutable, unittest.TestCase): |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | def setUp(self): |
|---|
| 118 | n/a | d = dict.fromkeys(range(n)) |
|---|
| 119 | n/a | self.it = iter(d) |
|---|
| 120 | n/a | self.mutate = d.popitem |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | class TestDictItems(TestTemporarilyImmutable, unittest.TestCase): |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | def setUp(self): |
|---|
| 125 | n/a | d = dict.fromkeys(range(n)) |
|---|
| 126 | n/a | self.it = iter(d.items()) |
|---|
| 127 | n/a | self.mutate = d.popitem |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | class TestDictValues(TestTemporarilyImmutable, unittest.TestCase): |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def setUp(self): |
|---|
| 132 | n/a | d = dict.fromkeys(range(n)) |
|---|
| 133 | n/a | self.it = iter(d.values()) |
|---|
| 134 | n/a | self.mutate = d.popitem |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | class TestSet(TestTemporarilyImmutable, unittest.TestCase): |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | def setUp(self): |
|---|
| 139 | n/a | d = set(range(n)) |
|---|
| 140 | n/a | self.it = iter(d) |
|---|
| 141 | n/a | self.mutate = d.pop |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | ## ------- Types that can mutate during iteration ------- |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | class TestList(TestInvariantWithoutMutations, unittest.TestCase): |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def setUp(self): |
|---|
| 148 | n/a | self.it = iter(range(n)) |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | def test_mutation(self): |
|---|
| 151 | n/a | d = list(range(n)) |
|---|
| 152 | n/a | it = iter(d) |
|---|
| 153 | n/a | next(it) |
|---|
| 154 | n/a | next(it) |
|---|
| 155 | n/a | self.assertEqual(length_hint(it), n - 2) |
|---|
| 156 | n/a | d.append(n) |
|---|
| 157 | n/a | self.assertEqual(length_hint(it), n - 1) # grow with append |
|---|
| 158 | n/a | d[1:] = [] |
|---|
| 159 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 160 | n/a | self.assertEqual(list(it), []) |
|---|
| 161 | n/a | d.extend(range(20)) |
|---|
| 162 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | class TestListReversed(TestInvariantWithoutMutations, unittest.TestCase): |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def setUp(self): |
|---|
| 168 | n/a | self.it = reversed(range(n)) |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | def test_mutation(self): |
|---|
| 171 | n/a | d = list(range(n)) |
|---|
| 172 | n/a | it = reversed(d) |
|---|
| 173 | n/a | next(it) |
|---|
| 174 | n/a | next(it) |
|---|
| 175 | n/a | self.assertEqual(length_hint(it), n - 2) |
|---|
| 176 | n/a | d.append(n) |
|---|
| 177 | n/a | self.assertEqual(length_hint(it), n - 2) # ignore append |
|---|
| 178 | n/a | d[1:] = [] |
|---|
| 179 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 180 | n/a | self.assertEqual(list(it), []) # confirm invariant |
|---|
| 181 | n/a | d.extend(range(20)) |
|---|
| 182 | n/a | self.assertEqual(length_hint(it), 0) |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | ## -- Check to make sure exceptions are not suppressed by __length_hint__() |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | class BadLen(object): |
|---|
| 188 | n/a | def __iter__(self): |
|---|
| 189 | n/a | return iter(range(10)) |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def __len__(self): |
|---|
| 192 | n/a | raise RuntimeError('hello') |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | class BadLengthHint(object): |
|---|
| 196 | n/a | def __iter__(self): |
|---|
| 197 | n/a | return iter(range(10)) |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | def __length_hint__(self): |
|---|
| 200 | n/a | raise RuntimeError('hello') |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | class NoneLengthHint(object): |
|---|
| 204 | n/a | def __iter__(self): |
|---|
| 205 | n/a | return iter(range(10)) |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def __length_hint__(self): |
|---|
| 208 | n/a | return NotImplemented |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | class TestLengthHintExceptions(unittest.TestCase): |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | def test_issue1242657(self): |
|---|
| 214 | n/a | self.assertRaises(RuntimeError, list, BadLen()) |
|---|
| 215 | n/a | self.assertRaises(RuntimeError, list, BadLengthHint()) |
|---|
| 216 | n/a | self.assertRaises(RuntimeError, [].extend, BadLen()) |
|---|
| 217 | n/a | self.assertRaises(RuntimeError, [].extend, BadLengthHint()) |
|---|
| 218 | n/a | b = bytearray(range(10)) |
|---|
| 219 | n/a | self.assertRaises(RuntimeError, b.extend, BadLen()) |
|---|
| 220 | n/a | self.assertRaises(RuntimeError, b.extend, BadLengthHint()) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | def test_invalid_hint(self): |
|---|
| 223 | n/a | # Make sure an invalid result doesn't muck-up the works |
|---|
| 224 | n/a | self.assertEqual(list(NoneLengthHint()), list(range(10))) |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | if __name__ == "__main__": |
|---|
| 228 | n/a | unittest.main() |
|---|