Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1 | doctests = """ |
| 2 | |
| 3 | Test simple loop with conditional |
| 4 | |
| 5 | >>> sum(i*i for i in range(100) if i&1 == 1) |
| 6 | 166650 |
| 7 | |
| 8 | Test simple nesting |
| 9 | |
| 10 | >>> list((i,j) for i in range(3) for j in range(4) ) |
| 11 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 12 | |
| 13 | Test nesting with the inner expression dependent on the outer |
| 14 | |
| 15 | >>> list((i,j) for i in range(4) for j in range(i) ) |
| 16 | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |
| 17 | |
| 18 | Make sure the induction variable is not exposed |
| 19 | |
| 20 | >>> i = 20 |
| 21 | >>> sum(i*i for i in range(100)) |
| 22 | 328350 |
| 23 | >>> i |
| 24 | 20 |
| 25 | |
| 26 | Test first class |
| 27 | |
| 28 | >>> g = (i*i for i in range(4)) |
| 29 | >>> type(g) |
| 30 | <type 'generator'> |
| 31 | >>> list(g) |
| 32 | [0, 1, 4, 9] |
| 33 | |
| 34 | Test direct calls to next() |
| 35 | |
| 36 | >>> g = (i*i for i in range(3)) |
| 37 | >>> g.next() |
| 38 | 0 |
| 39 | >>> g.next() |
| 40 | 1 |
| 41 | >>> g.next() |
| 42 | 4 |
| 43 | >>> g.next() |
| 44 | Traceback (most recent call last): |
| 45 | File "<pyshell#21>", line 1, in -toplevel- |
| 46 | g.next() |
| 47 | StopIteration |
| 48 | |
| 49 | Does it stay stopped? |
| 50 | |
| 51 | >>> g.next() |
| 52 | Traceback (most recent call last): |
| 53 | File "<pyshell#21>", line 1, in -toplevel- |
| 54 | g.next() |
| 55 | StopIteration |
| 56 | >>> list(g) |
| 57 | [] |
| 58 | |
| 59 | Test running gen when defining function is out of scope |
| 60 | |
| 61 | >>> def f(n): |
| 62 | ... return (i*i for i in xrange(n)) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 63 | >>> list(f(10)) |
| 64 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 65 | |
| 66 | >>> def f(n): |
| 67 | ... return ((i,j) for i in xrange(3) for j in xrange(n)) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 68 | >>> list(f(4)) |
| 69 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 70 | >>> def f(n): |
| 71 | ... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n)) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 72 | >>> list(f(4)) |
| 73 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 74 | >>> list(f(2)) |
| 75 | [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] |
| 76 | |
Raymond Hettinger | 3258e72 | 2004-08-16 01:35:28 +0000 | [diff] [blame] | 77 | Verify that parenthesis are required in a statement |
Raymond Hettinger | 84f107d | 2004-08-16 01:45:34 +0000 | [diff] [blame] | 78 | |
| 79 | >>> def f(n): |
| 80 | ... return i*i for i in xrange(n) |
| 81 | Traceback (most recent call last): |
| 82 | ... |
| 83 | SyntaxError: invalid syntax |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 84 | |
| 85 | Verify early binding for the outermost for-expression |
| 86 | |
| 87 | >>> x=10 |
| 88 | >>> g = (i*i for i in range(x)) |
| 89 | >>> x = 5 |
| 90 | >>> list(g) |
| 91 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 92 | |
Raymond Hettinger | 83ee795 | 2004-05-20 23:04:13 +0000 | [diff] [blame] | 93 | Verify that the outermost for-expression makes an immediate check |
| 94 | for iterability |
| 95 | |
| 96 | >>> (i for i in 6) |
| 97 | Traceback (most recent call last): |
| 98 | File "<pyshell#4>", line 1, in -toplevel- |
| 99 | (i for i in 6) |
| 100 | TypeError: iteration over non-sequence |
| 101 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 102 | Verify late binding for the outermost if-expression |
| 103 | |
| 104 | >>> include = (2,4,6,8) |
| 105 | >>> g = (i*i for i in range(10) if i in include) |
| 106 | >>> include = (1,3,5,7,9) |
| 107 | >>> list(g) |
| 108 | [1, 9, 25, 49, 81] |
| 109 | |
| 110 | Verify late binding for the innermost for-expression |
| 111 | |
| 112 | >>> g = ((i,j) for i in range(3) for j in range(x)) |
| 113 | >>> x = 4 |
| 114 | >>> list(g) |
| 115 | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
| 116 | |
| 117 | Verify re-use of tuples (a side benefit of using genexps over listcomps) |
| 118 | |
| 119 | >>> tupleids = map(id, ((i,i) for i in xrange(10))) |
| 120 | >>> max(tupleids) - min(tupleids) |
| 121 | 0 |
| 122 | |
| 123 | |
| 124 | |
| 125 | ########### Tests borrowed from or inspired by test_generators.py ############ |
| 126 | |
| 127 | Make a generator that acts like range() |
| 128 | |
| 129 | >>> yrange = lambda n: (i for i in xrange(n)) |
| 130 | >>> list(yrange(10)) |
| 131 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 132 | |
| 133 | Generators always return to the most recent caller: |
| 134 | |
| 135 | >>> def creator(): |
| 136 | ... r = yrange(5) |
| 137 | ... print "creator", r.next() |
| 138 | ... return r |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 139 | >>> def caller(): |
| 140 | ... r = creator() |
| 141 | ... for i in r: |
| 142 | ... print "caller", i |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 143 | >>> caller() |
| 144 | creator 0 |
| 145 | caller 1 |
| 146 | caller 2 |
| 147 | caller 3 |
| 148 | caller 4 |
| 149 | |
| 150 | Generators can call other generators: |
| 151 | |
| 152 | >>> def zrange(n): |
| 153 | ... for i in yrange(n): |
| 154 | ... yield i |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 155 | >>> list(zrange(5)) |
| 156 | [0, 1, 2, 3, 4] |
| 157 | |
| 158 | |
| 159 | Verify that a gen exp cannot be resumed while it is actively running: |
| 160 | |
| 161 | >>> g = (me.next() for i in xrange(10)) |
| 162 | >>> me = g |
| 163 | >>> me.next() |
| 164 | Traceback (most recent call last): |
| 165 | File "<pyshell#30>", line 1, in -toplevel- |
| 166 | me.next() |
| 167 | File "<pyshell#28>", line 1, in <generator expression> |
| 168 | g = (me.next() for i in xrange(10)) |
| 169 | ValueError: generator already executing |
| 170 | |
| 171 | Verify exception propagation |
| 172 | |
| 173 | >>> g = (10 // i for i in (5, 0, 2)) |
| 174 | >>> g.next() |
| 175 | 2 |
| 176 | >>> g.next() |
| 177 | Traceback (most recent call last): |
| 178 | File "<pyshell#37>", line 1, in -toplevel- |
| 179 | g.next() |
| 180 | File "<pyshell#35>", line 1, in <generator expression> |
| 181 | g = (10 // i for i in (5, 0, 2)) |
| 182 | ZeroDivisionError: integer division or modulo by zero |
| 183 | >>> g.next() |
| 184 | Traceback (most recent call last): |
| 185 | File "<pyshell#38>", line 1, in -toplevel- |
| 186 | g.next() |
| 187 | StopIteration |
| 188 | |
| 189 | Make sure that None is a valid return value |
| 190 | |
| 191 | >>> list(None for i in xrange(10)) |
| 192 | [None, None, None, None, None, None, None, None, None, None] |
| 193 | |
| 194 | Check that generator attributes are present |
| 195 | |
| 196 | >>> g = (i*i for i in range(3)) |
| 197 | >>> expected = set(['gi_frame', 'gi_running', 'next']) |
| 198 | >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected |
| 199 | True |
| 200 | |
| 201 | >>> print g.next.__doc__ |
| 202 | x.next() -> the next value, or raise StopIteration |
| 203 | >>> import types |
| 204 | >>> isinstance(g, types.GeneratorType) |
| 205 | True |
| 206 | |
| 207 | Check the __iter__ slot is defined to return self |
| 208 | |
| 209 | >>> iter(g) is g |
| 210 | True |
| 211 | |
| 212 | Verify that the running flag is set properly |
| 213 | |
| 214 | >>> g = (me.gi_running for i in (0,1)) |
| 215 | >>> me = g |
| 216 | >>> me.gi_running |
| 217 | 0 |
| 218 | >>> me.next() |
| 219 | 1 |
| 220 | >>> me.gi_running |
| 221 | 0 |
| 222 | |
| 223 | Verify that genexps are weakly referencable |
| 224 | |
| 225 | >>> import weakref |
| 226 | >>> g = (i*i for i in range(4)) |
| 227 | >>> wr = weakref.ref(g) |
| 228 | >>> wr() is g |
| 229 | True |
| 230 | >>> p = weakref.proxy(g) |
| 231 | >>> list(p) |
| 232 | [0, 1, 4, 9] |
| 233 | |
| 234 | |
| 235 | """ |
| 236 | |
| 237 | |
| 238 | __test__ = {'doctests' : doctests} |
| 239 | |
| 240 | def test_main(verbose=None): |
| 241 | import sys |
| 242 | from test import test_support |
| 243 | from test import test_genexps |
| 244 | test_support.run_doctest(test_genexps, verbose) |
| 245 | |
| 246 | # verify reference counting |
| 247 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 248 | import gc |
| 249 | counts = [None] * 5 |
| 250 | for i in xrange(len(counts)): |
| 251 | test_support.run_doctest(test_genexps, verbose) |
| 252 | gc.collect() |
| 253 | counts[i] = sys.gettotalrefcount() |
| 254 | print counts |
| 255 | |
| 256 | if __name__ == "__main__": |
| 257 | test_main(verbose=True) |