3 Test simple loop with conditional
5 >>> sum(i*i for i in range(100) if i&1 == 1)
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)]
13 Test nesting with the inner expression dependent on the outer
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)]
18 Make sure the induction variable is not exposed
21 >>> sum(i*i for i in range(100))
28 >>> g = (i*i for i in range(4))
34 Test direct calls to next()
36 >>> g = (i*i for i in range(3))
44 Traceback (most recent call last):
45 File "<pyshell#21>", line 1, in -toplevel-
52 Traceback (most recent call last):
53 File "<pyshell#21>", line 1, in -toplevel-
59 Test running gen when defining function is out of scope
62 ... return (i*i for i in xrange(n))
64 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
67 ... return ((i,j) for i in xrange(3) for j in xrange(n))
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)]
71 ... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n))
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)]
75 [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
77 Verify that parenthesis are required in a statement
80 ... return i*i for i in xrange(n)
81 Traceback (most recent call last):
83 SyntaxError: invalid syntax
85 Verify that parenthesis are required when used as a keyword argument value
87 >>> dict(a = i for i in xrange(10))
88 Traceback (most recent call last):
90 SyntaxError: invalid syntax
92 Verify that parenthesis are required when used as a keyword argument value
94 >>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS
95 {'a': <generator object <genexpr> at ...>}
97 Verify early binding for the outermost for-expression
100 >>> g = (i*i for i in range(x))
103 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
105 Verify that the outermost for-expression makes an immediate check
109 Traceback (most recent call last):
110 File "<pyshell#4>", line 1, in -toplevel-
112 TypeError: 'int' object is not iterable
114 Verify late binding for the outermost if-expression
116 >>> include = (2,4,6,8)
117 >>> g = (i*i for i in range(10) if i in include)
118 >>> include = (1,3,5,7,9)
122 Verify late binding for the innermost for-expression
124 >>> g = ((i,j) for i in range(3) for j in range(x))
127 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
129 Verify re-use of tuples (a side benefit of using genexps over listcomps)
131 >>> tupleids = map(id, ((i,i) for i in xrange(10)))
132 >>> int(max(tupleids) - min(tupleids))
135 Verify that syntax error's are raised for genexps used as lvalues
137 >>> (y for y in (1,2)) = 10
138 Traceback (most recent call last):
140 File "<doctest test.test_genexps.__test__.doctests[40]>", line 1
141 SyntaxError: can't assign to generator expression
143 >>> (y for y in (1,2)) += 10
144 Traceback (most recent call last):
146 File "<doctest test.test_genexps.__test__.doctests[41]>", line 1
147 SyntaxError: can't assign to generator expression
150 ########### Tests borrowed from or inspired by test_generators.py ############
152 Make a generator that acts like range()
154 >>> yrange = lambda n: (i for i in xrange(n))
156 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
158 Generators always return to the most recent caller:
162 ... print "creator", r.next()
167 ... print "caller", i
175 Generators can call other generators:
178 ... for i in yrange(n):
184 Verify that a gen exp cannot be resumed while it is actively running:
186 >>> g = (me.next() for i in xrange(10))
189 Traceback (most recent call last):
190 File "<pyshell#30>", line 1, in -toplevel-
192 File "<pyshell#28>", line 1, in <generator expression>
193 g = (me.next() for i in xrange(10))
194 ValueError: generator already executing
196 Verify exception propagation
198 >>> g = (10 // i for i in (5, 0, 2))
202 Traceback (most recent call last):
203 File "<pyshell#37>", line 1, in -toplevel-
205 File "<pyshell#35>", line 1, in <generator expression>
206 g = (10 // i for i in (5, 0, 2))
207 ZeroDivisionError: integer division or modulo by zero
209 Traceback (most recent call last):
210 File "<pyshell#38>", line 1, in -toplevel-
214 Make sure that None is a valid return value
216 >>> list(None for i in xrange(10))
217 [None, None, None, None, None, None, None, None, None, None]
219 Check that generator attributes are present
221 >>> g = (i*i for i in range(3))
222 >>> expected = set(['gi_frame', 'gi_running', 'next'])
223 >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
226 >>> print g.next.__doc__
227 x.next() -> the next value, or raise StopIteration
229 >>> isinstance(g, types.GeneratorType)
232 Check the __iter__ slot is defined to return self
237 Verify that the running flag is set properly
239 >>> g = (me.gi_running for i in (0,1))
248 Verify that genexps are weakly referencable
251 >>> g = (i*i for i in range(4))
252 >>> wr = weakref.ref(g)
255 >>> p = weakref.proxy(g)
263 __test__
= {'doctests' : doctests
}
265 def test_main(verbose
=None):
267 from test
import test_support
268 from test
import test_genexps
269 test_support
.run_doctest(test_genexps
, verbose
)
271 # verify reference counting
272 if verbose
and hasattr(sys
, "gettotalrefcount"):
275 for i
in xrange(len(counts
)):
276 test_support
.run_doctest(test_genexps
, verbose
)
278 counts
[i
] = sys
.gettotalrefcount()
281 if __name__
== "__main__":
282 test_main(verbose
=True)