blob: 413043c5a366f4f249d074d8cc836de7ef84d7a4 [file] [log] [blame]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001doctests = """
2
3Test simple loop with conditional
4
5 >>> sum(i*i for i in range(100) if i&1 == 1)
6 166650
7
8Test 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
13Test 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
18Make 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
26Test first class
27
28 >>> g = (i*i for i in range(4))
29 >>> type(g)
Martin v. Löwis250ad612008-04-07 05:43:42 +000030 <class 'generator'>
Raymond Hettinger354433a2004-05-19 08:20:33 +000031 >>> list(g)
32 [0, 1, 4, 9]
33
34Test direct calls to next()
35
36 >>> g = (i*i for i in range(3))
Georg Brandla18af4e2007-04-21 15:47:16 +000037 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000038 0
Georg Brandla18af4e2007-04-21 15:47:16 +000039 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000040 1
Georg Brandla18af4e2007-04-21 15:47:16 +000041 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000042 4
Georg Brandla18af4e2007-04-21 15:47:16 +000043 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000044 Traceback (most recent call last):
45 File "<pyshell#21>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +000046 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000047 StopIteration
48
49Does it stay stopped?
50
Georg Brandla18af4e2007-04-21 15:47:16 +000051 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000052 Traceback (most recent call last):
53 File "<pyshell#21>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +000054 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000055 StopIteration
56 >>> list(g)
57 []
58
59Test running gen when defining function is out of scope
60
61 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000062 ... return (i*i for i in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +000063 >>> list(f(10))
64 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
65
66 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000067 ... return ((i,j) for i in range(3) for j in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +000068 >>> 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):
Guido van Rossum805365e2007-05-07 22:24:25 +000071 ... return ((i,j) for i in range(3) for j in range(4) if j in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +000072 >>> 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 Hettinger3258e722004-08-16 01:35:28 +000077Verify that parenthesis are required in a statement
Raymond Hettinger84f107d2004-08-16 01:45:34 +000078
79 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000080 ... return i*i for i in range(n)
Raymond Hettinger84f107d2004-08-16 01:45:34 +000081 Traceback (most recent call last):
82 ...
83 SyntaxError: invalid syntax
Raymond Hettinger354433a2004-05-19 08:20:33 +000084
Neal Norwitz37c08442005-10-21 06:24:02 +000085Verify that parenthesis are required when used as a keyword argument value
86
Guido van Rossum805365e2007-05-07 22:24:25 +000087 >>> dict(a = i for i in range(10))
Neal Norwitz37c08442005-10-21 06:24:02 +000088 Traceback (most recent call last):
89 ...
90 SyntaxError: invalid syntax
91
92Verify that parenthesis are required when used as a keyword argument value
93
Guido van Rossum805365e2007-05-07 22:24:25 +000094 >>> dict(a = (i for i in range(10))) #doctest: +ELLIPSIS
Alexandre Vassalottibee32532008-05-16 18:15:12 +000095 {'a': <generator object <genexpr> at ...>}
Neal Norwitz37c08442005-10-21 06:24:02 +000096
Raymond Hettinger354433a2004-05-19 08:20:33 +000097Verify early binding for the outermost for-expression
98
99 >>> x=10
100 >>> g = (i*i for i in range(x))
101 >>> x = 5
102 >>> list(g)
103 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
104
Raymond Hettinger83ee7952004-05-20 23:04:13 +0000105Verify that the outermost for-expression makes an immediate check
106for iterability
107
108 >>> (i for i in 6)
109 Traceback (most recent call last):
110 File "<pyshell#4>", line 1, in -toplevel-
111 (i for i in 6)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000112 TypeError: 'int' object is not iterable
Raymond Hettinger83ee7952004-05-20 23:04:13 +0000113
Raymond Hettinger354433a2004-05-19 08:20:33 +0000114Verify late binding for the outermost if-expression
115
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)
119 >>> list(g)
120 [1, 9, 25, 49, 81]
121
122Verify late binding for the innermost for-expression
123
124 >>> g = ((i,j) for i in range(3) for j in range(x))
125 >>> x = 4
126 >>> list(g)
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)]
128
129Verify re-use of tuples (a side benefit of using genexps over listcomps)
130
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000131 >>> tupleids = list(map(id, ((i,i) for i in range(10))))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 >>> int(max(tupleids) - min(tupleids))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000133 0
134
Raymond Hettinger7b46f6b2004-09-30 22:29:03 +0000135Verify that syntax error's are raised for genexps used as lvalues
136
137 >>> (y for y in (1,2)) = 10
138 Traceback (most recent call last):
139 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000140 SyntaxError: can't assign to generator expression
Raymond Hettinger7b46f6b2004-09-30 22:29:03 +0000141
142 >>> (y for y in (1,2)) += 10
143 Traceback (most recent call last):
144 ...
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000145 SyntaxError: can't assign to generator expression
Raymond Hettinger7b46f6b2004-09-30 22:29:03 +0000146
Raymond Hettinger354433a2004-05-19 08:20:33 +0000147
Raymond Hettinger354433a2004-05-19 08:20:33 +0000148########### Tests borrowed from or inspired by test_generators.py ############
149
150Make a generator that acts like range()
151
Guido van Rossum805365e2007-05-07 22:24:25 +0000152 >>> yrange = lambda n: (i for i in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000153 >>> list(yrange(10))
154 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
155
156Generators always return to the most recent caller:
157
158 >>> def creator():
159 ... r = yrange(5)
Georg Brandla18af4e2007-04-21 15:47:16 +0000160 ... print("creator", next(r))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000161 ... return r
Raymond Hettinger354433a2004-05-19 08:20:33 +0000162 >>> def caller():
163 ... r = creator()
164 ... for i in r:
Guido van Rossum7131f842007-02-09 20:13:25 +0000165 ... print("caller", i)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000166 >>> caller()
167 creator 0
168 caller 1
169 caller 2
170 caller 3
171 caller 4
172
173Generators can call other generators:
174
175 >>> def zrange(n):
176 ... for i in yrange(n):
177 ... yield i
Raymond Hettinger354433a2004-05-19 08:20:33 +0000178 >>> list(zrange(5))
179 [0, 1, 2, 3, 4]
180
181
182Verify that a gen exp cannot be resumed while it is actively running:
183
Guido van Rossum805365e2007-05-07 22:24:25 +0000184 >>> g = (next(me) for i in range(10))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000185 >>> me = g
Georg Brandla18af4e2007-04-21 15:47:16 +0000186 >>> next(me)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000187 Traceback (most recent call last):
188 File "<pyshell#30>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +0000189 next(me)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000190 File "<pyshell#28>", line 1, in <generator expression>
Guido van Rossum805365e2007-05-07 22:24:25 +0000191 g = (next(me) for i in range(10))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000192 ValueError: generator already executing
193
194Verify exception propagation
195
196 >>> g = (10 // i for i in (5, 0, 2))
Georg Brandla18af4e2007-04-21 15:47:16 +0000197 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000198 2
Georg Brandla18af4e2007-04-21 15:47:16 +0000199 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000200 Traceback (most recent call last):
201 File "<pyshell#37>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +0000202 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000203 File "<pyshell#35>", line 1, in <generator expression>
204 g = (10 // i for i in (5, 0, 2))
205 ZeroDivisionError: integer division or modulo by zero
Georg Brandla18af4e2007-04-21 15:47:16 +0000206 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000207 Traceback (most recent call last):
208 File "<pyshell#38>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +0000209 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000210 StopIteration
211
212Make sure that None is a valid return value
213
Guido van Rossum805365e2007-05-07 22:24:25 +0000214 >>> list(None for i in range(10))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000215 [None, None, None, None, None, None, None, None, None, None]
216
217Check that generator attributes are present
218
219 >>> g = (i*i for i in range(3))
Georg Brandla18af4e2007-04-21 15:47:16 +0000220 >>> expected = set(['gi_frame', 'gi_running'])
Raymond Hettinger354433a2004-05-19 08:20:33 +0000221 >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
222 True
223
Georg Brandla18af4e2007-04-21 15:47:16 +0000224 >>> print(g.__next__.__doc__)
225 x.__next__() <==> next(x)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000226 >>> import types
227 >>> isinstance(g, types.GeneratorType)
228 True
229
230Check the __iter__ slot is defined to return self
231
232 >>> iter(g) is g
233 True
234
235Verify that the running flag is set properly
236
237 >>> g = (me.gi_running for i in (0,1))
238 >>> me = g
239 >>> me.gi_running
240 0
Georg Brandla18af4e2007-04-21 15:47:16 +0000241 >>> next(me)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000242 1
243 >>> me.gi_running
244 0
245
246Verify that genexps are weakly referencable
247
248 >>> import weakref
249 >>> g = (i*i for i in range(4))
250 >>> wr = weakref.ref(g)
251 >>> wr() is g
252 True
253 >>> p = weakref.proxy(g)
254 >>> list(p)
255 [0, 1, 4, 9]
256
257
258"""
259
Brett Cannon7a540732011-02-22 03:04:06 +0000260import sys
Raymond Hettinger354433a2004-05-19 08:20:33 +0000261
Brett Cannon7a540732011-02-22 03:04:06 +0000262# Trace function can throw off the tuple reuse test.
263if hasattr(sys, 'gettrace') and sys.gettrace():
264 __test__ = {}
265else:
266 __test__ = {'doctests' : doctests}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000267
268def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000269 from test import support
Raymond Hettinger354433a2004-05-19 08:20:33 +0000270 from test import test_genexps
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000271 support.run_doctest(test_genexps, verbose)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000272
273 # verify reference counting
274 if verbose and hasattr(sys, "gettotalrefcount"):
275 import gc
276 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000277 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000278 support.run_doctest(test_genexps, verbose)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000279 gc.collect()
280 counts[i] = sys.gettotalrefcount()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000281 print(counts)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000282
283if __name__ == "__main__":
284 test_main(verbose=True)