blob: 5c1a209b0e99086e0d55478a17ac6571bcf4c0be [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
Serhiy Storchaka8c579b12020-02-12 12:18:59 +020018Test the idiom for temporary variable assignment in comprehensions.
19
20 >>> list((j*j for i in range(4) for j in [i+1]))
21 [1, 4, 9, 16]
22 >>> list((j*k for i in range(4) for j in [i+1] for k in [j+1]))
23 [2, 6, 12, 20]
24 >>> list((j*k for i in range(4) for j, k in [(i+1, i+2)]))
25 [2, 6, 12, 20]
26
27Not assignment
28
29 >>> list((i*i for i in [*range(4)]))
30 [0, 1, 4, 9]
31 >>> list((i*i for i in (*range(4),)))
32 [0, 1, 4, 9]
33
Raymond Hettinger354433a2004-05-19 08:20:33 +000034Make sure the induction variable is not exposed
35
36 >>> i = 20
37 >>> sum(i*i for i in range(100))
38 328350
39 >>> i
40 20
41
42Test first class
43
44 >>> g = (i*i for i in range(4))
45 >>> type(g)
Benjamin Petersonab078e92016-07-13 21:13:29 -070046 <class 'generator'>
Raymond Hettinger354433a2004-05-19 08:20:33 +000047 >>> list(g)
48 [0, 1, 4, 9]
49
50Test direct calls to next()
51
52 >>> g = (i*i for i in range(3))
Georg Brandla18af4e2007-04-21 15:47:16 +000053 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000054 0
Georg Brandla18af4e2007-04-21 15:47:16 +000055 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000056 1
Georg Brandla18af4e2007-04-21 15:47:16 +000057 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000058 4
Georg Brandla18af4e2007-04-21 15:47:16 +000059 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000060 Traceback (most recent call last):
61 File "<pyshell#21>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +000062 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000063 StopIteration
64
65Does it stay stopped?
66
Georg Brandla18af4e2007-04-21 15:47:16 +000067 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000068 Traceback (most recent call last):
69 File "<pyshell#21>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +000070 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +000071 StopIteration
72 >>> list(g)
73 []
74
75Test running gen when defining function is out of scope
76
77 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000078 ... return (i*i for i in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +000079 >>> list(f(10))
80 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
81
82 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000083 ... return ((i,j) for i in range(3) for j in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +000084 >>> list(f(4))
85 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
86 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000087 ... 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 +000088 >>> list(f(4))
89 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
90 >>> list(f(2))
91 [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
92
Raymond Hettinger3258e722004-08-16 01:35:28 +000093Verify that parenthesis are required in a statement
Raymond Hettinger84f107d2004-08-16 01:45:34 +000094
95 >>> def f(n):
Guido van Rossum805365e2007-05-07 22:24:25 +000096 ... return i*i for i in range(n)
Raymond Hettinger84f107d2004-08-16 01:45:34 +000097 Traceback (most recent call last):
98 ...
99 SyntaxError: invalid syntax
Raymond Hettinger354433a2004-05-19 08:20:33 +0000100
Neal Norwitz37c08442005-10-21 06:24:02 +0000101Verify that parenthesis are required when used as a keyword argument value
102
Guido van Rossum805365e2007-05-07 22:24:25 +0000103 >>> dict(a = i for i in range(10))
Neal Norwitz37c08442005-10-21 06:24:02 +0000104 Traceback (most recent call last):
105 ...
106 SyntaxError: invalid syntax
107
108Verify that parenthesis are required when used as a keyword argument value
109
Guido van Rossum805365e2007-05-07 22:24:25 +0000110 >>> dict(a = (i for i in range(10))) #doctest: +ELLIPSIS
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000111 {'a': <generator object <genexpr> at ...>}
Neal Norwitz37c08442005-10-21 06:24:02 +0000112
Raymond Hettinger354433a2004-05-19 08:20:33 +0000113Verify early binding for the outermost for-expression
114
115 >>> x=10
116 >>> g = (i*i for i in range(x))
117 >>> x = 5
118 >>> list(g)
119 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
120
Raymond Hettinger83ee7952004-05-20 23:04:13 +0000121Verify that the outermost for-expression makes an immediate check
122for iterability
123
124 >>> (i for i in 6)
125 Traceback (most recent call last):
126 File "<pyshell#4>", line 1, in -toplevel-
127 (i for i in 6)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128 TypeError: 'int' object is not iterable
Raymond Hettinger83ee7952004-05-20 23:04:13 +0000129
Raymond Hettinger354433a2004-05-19 08:20:33 +0000130Verify late binding for the outermost if-expression
131
132 >>> include = (2,4,6,8)
133 >>> g = (i*i for i in range(10) if i in include)
134 >>> include = (1,3,5,7,9)
135 >>> list(g)
136 [1, 9, 25, 49, 81]
137
138Verify late binding for the innermost for-expression
139
140 >>> g = ((i,j) for i in range(3) for j in range(x))
141 >>> x = 4
142 >>> list(g)
143 [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
144
145Verify re-use of tuples (a side benefit of using genexps over listcomps)
146
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000147 >>> tupleids = list(map(id, ((i,i) for i in range(10))))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 >>> int(max(tupleids) - min(tupleids))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000149 0
150
Raymond Hettinger7b46f6b2004-09-30 22:29:03 +0000151Verify that syntax error's are raised for genexps used as lvalues
152
153 >>> (y for y in (1,2)) = 10
154 Traceback (most recent call last):
155 ...
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200156 SyntaxError: cannot assign to generator expression
Raymond Hettinger7b46f6b2004-09-30 22:29:03 +0000157
158 >>> (y for y in (1,2)) += 10
159 Traceback (most recent call last):
160 ...
Pablo Galindo16ab0702020-05-15 02:04:52 +0100161 SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Raymond Hettinger7b46f6b2004-09-30 22:29:03 +0000162
Raymond Hettinger354433a2004-05-19 08:20:33 +0000163
Raymond Hettinger354433a2004-05-19 08:20:33 +0000164########### Tests borrowed from or inspired by test_generators.py ############
165
166Make a generator that acts like range()
167
Guido van Rossum805365e2007-05-07 22:24:25 +0000168 >>> yrange = lambda n: (i for i in range(n))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000169 >>> list(yrange(10))
170 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
171
172Generators always return to the most recent caller:
173
174 >>> def creator():
175 ... r = yrange(5)
Georg Brandla18af4e2007-04-21 15:47:16 +0000176 ... print("creator", next(r))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000177 ... return r
Raymond Hettinger354433a2004-05-19 08:20:33 +0000178 >>> def caller():
179 ... r = creator()
180 ... for i in r:
Guido van Rossum7131f842007-02-09 20:13:25 +0000181 ... print("caller", i)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000182 >>> caller()
183 creator 0
184 caller 1
185 caller 2
186 caller 3
187 caller 4
188
189Generators can call other generators:
190
191 >>> def zrange(n):
192 ... for i in yrange(n):
193 ... yield i
Raymond Hettinger354433a2004-05-19 08:20:33 +0000194 >>> list(zrange(5))
195 [0, 1, 2, 3, 4]
196
197
198Verify that a gen exp cannot be resumed while it is actively running:
199
Guido van Rossum805365e2007-05-07 22:24:25 +0000200 >>> g = (next(me) for i in range(10))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000201 >>> me = g
Georg Brandla18af4e2007-04-21 15:47:16 +0000202 >>> next(me)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000203 Traceback (most recent call last):
204 File "<pyshell#30>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +0000205 next(me)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000206 File "<pyshell#28>", line 1, in <generator expression>
Guido van Rossum805365e2007-05-07 22:24:25 +0000207 g = (next(me) for i in range(10))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000208 ValueError: generator already executing
209
210Verify exception propagation
211
212 >>> g = (10 // i for i in (5, 0, 2))
Georg Brandla18af4e2007-04-21 15:47:16 +0000213 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000214 2
Georg Brandla18af4e2007-04-21 15:47:16 +0000215 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000216 Traceback (most recent call last):
217 File "<pyshell#37>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +0000218 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000219 File "<pyshell#35>", line 1, in <generator expression>
220 g = (10 // i for i in (5, 0, 2))
221 ZeroDivisionError: integer division or modulo by zero
Georg Brandla18af4e2007-04-21 15:47:16 +0000222 >>> next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000223 Traceback (most recent call last):
224 File "<pyshell#38>", line 1, in -toplevel-
Georg Brandla18af4e2007-04-21 15:47:16 +0000225 next(g)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000226 StopIteration
227
228Make sure that None is a valid return value
229
Guido van Rossum805365e2007-05-07 22:24:25 +0000230 >>> list(None for i in range(10))
Raymond Hettinger354433a2004-05-19 08:20:33 +0000231 [None, None, None, None, None, None, None, None, None, None]
232
233Check that generator attributes are present
234
235 >>> g = (i*i for i in range(3))
Georg Brandla18af4e2007-04-21 15:47:16 +0000236 >>> expected = set(['gi_frame', 'gi_running'])
Raymond Hettinger354433a2004-05-19 08:20:33 +0000237 >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
238 True
239
Serhiy Storchaka9a11f172013-01-31 16:11:04 +0200240 >>> from test.support import HAVE_DOCSTRINGS
Larry Hastings581ee362014-01-28 05:00:08 -0800241 >>> print(g.__next__.__doc__ if HAVE_DOCSTRINGS else 'Implement next(self).')
242 Implement next(self).
Raymond Hettinger354433a2004-05-19 08:20:33 +0000243 >>> import types
244 >>> isinstance(g, types.GeneratorType)
245 True
246
247Check the __iter__ slot is defined to return self
248
249 >>> iter(g) is g
250 True
251
252Verify that the running flag is set properly
253
254 >>> g = (me.gi_running for i in (0,1))
255 >>> me = g
256 >>> me.gi_running
257 0
Georg Brandla18af4e2007-04-21 15:47:16 +0000258 >>> next(me)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000259 1
260 >>> me.gi_running
261 0
262
263Verify that genexps are weakly referencable
264
265 >>> import weakref
266 >>> g = (i*i for i in range(4))
267 >>> wr = weakref.ref(g)
268 >>> wr() is g
269 True
270 >>> p = weakref.proxy(g)
271 >>> list(p)
272 [0, 1, 4, 9]
273
274
275"""
276
Brett Cannon7a540732011-02-22 03:04:06 +0000277import sys
Raymond Hettinger354433a2004-05-19 08:20:33 +0000278
Brett Cannon7a540732011-02-22 03:04:06 +0000279# Trace function can throw off the tuple reuse test.
280if hasattr(sys, 'gettrace') and sys.gettrace():
281 __test__ = {}
282else:
283 __test__ = {'doctests' : doctests}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000284
285def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000286 from test import support
Raymond Hettinger354433a2004-05-19 08:20:33 +0000287 from test import test_genexps
Benjamin Petersonab078e92016-07-13 21:13:29 -0700288 support.run_doctest(test_genexps, verbose)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000289
290 # verify reference counting
291 if verbose and hasattr(sys, "gettotalrefcount"):
292 import gc
293 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000294 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000295 support.run_doctest(test_genexps, verbose)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000296 gc.collect()
297 counts[i] = sys.gettotalrefcount()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000298 print(counts)
Raymond Hettinger354433a2004-05-19 08:20:33 +0000299
300if __name__ == "__main__":
301 test_main(verbose=True)