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