annotate Lib/test/test_generators.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 3f994367a979
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84911
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
1 import gc
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
2 import sys
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
3 import unittest
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
4 import weakref
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
5
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
6 from test import support
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
7
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
8
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
9 class FinalizationTest(unittest.TestCase):
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
10
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
11 def test_frame_resurrect(self):
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
12 # A generator frame can be resurrected by a generator's finalization.
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
13 def gen():
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
14 nonlocal frame
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
15 try:
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
16 yield
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
17 finally:
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
18 frame = sys._getframe()
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
19
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
20 g = gen()
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
21 wr = weakref.ref(g)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
22 next(g)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
23 del g
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
24 support.gc_collect()
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
25 self.assertIs(wr(), None)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
26 self.assertTrue(frame)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
27 del frame
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
28 support.gc_collect()
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
29
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
30 def test_refcycle(self):
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
31 # A generator caught in a refcycle gets finalized anyway.
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
32 old_garbage = gc.garbage[:]
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
33 finalized = False
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
34 def gen():
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
35 nonlocal finalized
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
36 try:
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
37 g = yield
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
38 yield 1
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
39 finally:
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
40 finalized = True
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
41
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
42 g = gen()
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
43 next(g)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
44 g.send(g)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
45 self.assertGreater(sys.getrefcount(g), 2)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
46 self.assertFalse(finalized)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
47 del g
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
48 support.gc_collect()
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
49 self.assertTrue(finalized)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
50 self.assertEqual(gc.garbage, old_garbage)
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
51
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
52
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
53 tutorial_tests = """
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
54 Let's try a simple generator:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
55
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
56 >>> def f():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
57 ... yield 1
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
58 ... yield 2
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
59
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
60 >>> for i in f():
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
61 ... print(i)
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
62 1
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
63 2
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
64 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
65 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
66 1
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
67 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
68 2
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
69
18265
a46e35acf9c1 Repair indentation in comment.
Tim Peters <tim.peters@gmail.com>
parents: 18257
diff changeset
70 "Falling off the end" stops the generator:
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
71
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
72 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
73 Traceback (most recent call last):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
74 File "<stdin>", line 1, in ?
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
75 File "<stdin>", line 2, in g
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
76 StopIteration
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
77
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
78 "return" also stops the generator:
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
79
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
80 >>> def f():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
81 ... yield 1
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
82 ... return
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
83 ... yield 2 # never reached
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
84 ...
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
85 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
86 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
87 1
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
88 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
89 Traceback (most recent call last):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
90 File "<stdin>", line 1, in ?
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
91 File "<stdin>", line 3, in f
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
92 StopIteration
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
93 >>> next(g) # once stopped, can't be resumed
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
94 Traceback (most recent call last):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
95 File "<stdin>", line 1, in ?
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
96 StopIteration
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
97
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
98 "raise StopIteration" stops the generator too:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
99
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
100 >>> def f():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
101 ... yield 1
18525
64fa72a84347 Repair flawed example.
Tim Peters <tim.peters@gmail.com>
parents: 18524
diff changeset
102 ... raise StopIteration
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
103 ... yield 2 # never reached
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
104 ...
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
105 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
106 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
107 1
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
108 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
109 Traceback (most recent call last):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
110 File "<stdin>", line 1, in ?
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
111 StopIteration
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
112 >>> next(g)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
113 Traceback (most recent call last):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
114 File "<stdin>", line 1, in ?
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
115 StopIteration
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
116
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
117 However, they are not exactly equivalent:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
118
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
119 >>> def g1():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
120 ... try:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
121 ... return
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
122 ... except:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
123 ... yield 1
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
124 ...
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
125 >>> list(g1())
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
126 []
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
127
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
128 >>> def g2():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
129 ... try:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
130 ... raise StopIteration
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
131 ... except:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
132 ... yield 42
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
133 >>> print(list(g2()))
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
134 [42]
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
135
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
136 This may be surprising at first:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
137
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
138 >>> def g3():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
139 ... try:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
140 ... return
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
141 ... finally:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
142 ... yield 1
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
143 ...
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
144 >>> list(g3())
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
145 [1]
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
146
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
147 Let's create an alternate range() function implemented as a generator:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
148
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
149 >>> def yrange(n):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
150 ... for i in range(n):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
151 ... yield i
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
152 ...
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
153 >>> list(yrange(5))
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
154 [0, 1, 2, 3, 4]
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
155
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
156 Generators always return to the most recent caller:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
157
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
158 >>> def creator():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
159 ... r = yrange(5)
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
160 ... print("creator", next(r))
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
161 ... return r
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
162 ...
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
163 >>> def caller():
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
164 ... r = creator()
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
165 ... for i in r:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
166 ... print("caller", i)
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
167 ...
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
168 >>> caller()
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
169 creator 0
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
170 caller 1
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
171 caller 2
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
172 caller 3
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
173 caller 4
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
174
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
175 Generators can call other generators:
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
176
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
177 >>> def zrange(n):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
178 ... for i in yrange(n):
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
179 ... yield i
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
180 ...
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
181 >>> list(zrange(5))
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
182 [0, 1, 2, 3, 4]
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
183
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
184 """
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
185
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
186 # The examples from PEP 255.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
187
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
188 pep_tests = """
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
189
19264
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
190 Specification: Yield
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
191
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
192 Restriction: A generator cannot be resumed while it is actively
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
193 running:
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
194
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
195 >>> def g():
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
196 ... i = next(me)
19264
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
197 ... yield i
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
198 >>> me = g()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
199 >>> next(me)
19264
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
200 Traceback (most recent call last):
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
201 ...
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
202 File "<string>", line 2, in g
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
203 ValueError: generator already executing
eb5e2b68354f Move one of the tests into the "PEP 255" section, to reflect a change in
Tim Peters <tim.peters@gmail.com>
parents: 19154
diff changeset
204
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
205 Specification: Return
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
206
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
207 Note that return isn't always equivalent to raising StopIteration: the
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
208 difference lies in how enclosing try/except constructs are treated.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
209 For example,
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
210
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
211 >>> def f1():
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
212 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
213 ... return
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
214 ... except:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
215 ... yield 1
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
216 >>> print(list(f1()))
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
217 []
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
218
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
219 because, as in any function, return simply exits, but
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
220
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
221 >>> def f2():
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
222 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
223 ... raise StopIteration
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
224 ... except:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
225 ... yield 42
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
226 >>> print(list(f2()))
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
227 [42]
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
228
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
229 because StopIteration is captured by a bare "except", as is any
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
230 exception.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
231
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
232 Specification: Generators and Exception Propagation
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
233
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
234 >>> def f():
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
235 ... return 1//0
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
236 >>> def g():
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
237 ... yield f() # the zero division exception propagates
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
238 ... yield 42 # and we'll never get here
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
239 >>> k = g()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
240 >>> next(k)
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
241 Traceback (most recent call last):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
242 File "<stdin>", line 1, in ?
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
243 File "<stdin>", line 2, in g
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
244 File "<stdin>", line 2, in f
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
245 ZeroDivisionError: integer division or modulo by zero
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
246 >>> next(k) # and the generator cannot be resumed
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
247 Traceback (most recent call last):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
248 File "<stdin>", line 1, in ?
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
249 StopIteration
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
250 >>>
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
251
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
252 Specification: Try/Except/Finally
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
253
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
254 >>> def f():
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
255 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
256 ... yield 1
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
257 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
258 ... yield 2
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
259 ... 1//0
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
260 ... yield 3 # never get here
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
261 ... except ZeroDivisionError:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
262 ... yield 4
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
263 ... yield 5
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
264 ... raise
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
265 ... except:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
266 ... yield 6
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
267 ... yield 7 # the "raise" above stops this
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
268 ... except:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
269 ... yield 8
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
270 ... yield 9
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
271 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
272 ... x = 12
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
273 ... finally:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
274 ... yield 10
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
275 ... yield 11
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
276 >>> print(list(f()))
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
277 [1, 2, 4, 5, 8, 9, 10, 11]
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
278 >>>
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
279
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
280 Guido's binary tree example.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
281
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
282 >>> # A binary tree class.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
283 >>> class Tree:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
284 ...
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
285 ... def __init__(self, label, left=None, right=None):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
286 ... self.label = label
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
287 ... self.left = left
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
288 ... self.right = right
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
289 ...
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
290 ... def __repr__(self, level=0, indent=" "):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 26834
diff changeset
291 ... s = level*indent + repr(self.label)
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
292 ... if self.left:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
293 ... s = s + "\\n" + self.left.__repr__(level+1, indent)
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
294 ... if self.right:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
295 ... s = s + "\\n" + self.right.__repr__(level+1, indent)
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
296 ... return s
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
297 ...
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
298 ... def __iter__(self):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
299 ... return inorder(self)
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
300
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
301 >>> # Create a Tree from a list.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
302 >>> def tree(list):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
303 ... n = len(list)
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
304 ... if n == 0:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
305 ... return []
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
306 ... i = n // 2
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
307 ... return Tree(list[i], tree(list[:i]), tree(list[i+1:]))
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
308
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
309 >>> # Show it off: create a tree.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
310 >>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
311
22207
f0ae37625189 SF patch 499062: Minor typo in test_generators.py.
Tim Peters <tim.peters@gmail.com>
parents: 21251
diff changeset
312 >>> # A recursive generator that generates Tree labels in in-order.
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
313 >>> def inorder(t):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
314 ... if t:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
315 ... for x in inorder(t.left):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
316 ... yield x
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
317 ... yield t.label
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
318 ... for x in inorder(t.right):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
319 ... yield x
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
320
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
321 >>> # Show it off: create a tree.
33054
27ada25509ff Fixed doctest error (wrong prompts)
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 31515
diff changeset
322 >>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
27ada25509ff Fixed doctest error (wrong prompts)
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 31515
diff changeset
323 >>> # Print the nodes of the tree in in-order.
27ada25509ff Fixed doctest error (wrong prompts)
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 31515
diff changeset
324 >>> for x in t:
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
325 ... print(' '+x, end='')
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
326 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
327
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
328 >>> # A non-recursive generator.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
329 >>> def inorder(node):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
330 ... stack = []
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
331 ... while node:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
332 ... while node.left:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
333 ... stack.append(node)
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
334 ... node = node.left
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
335 ... yield node.label
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
336 ... while not node.right:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
337 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
338 ... node = stack.pop()
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
339 ... except IndexError:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
340 ... return
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
341 ... yield node.label
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
342 ... node = node.right
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
343
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
344 >>> # Exercise the non-recursive generator.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
345 >>> for x in t:
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
346 ... print(' '+x, end='')
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
347 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
348
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
349 """
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
350
18257
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
351 # Examples from Iterator-List and Python-Dev and c.l.py.
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
352
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
353 email_tests = """
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
354
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
355 The difference between yielding None and returning it.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
356
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
357 >>> def g():
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
358 ... for i in range(3):
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
359 ... yield None
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
360 ... yield None
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
361 ... return
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
362 >>> list(g())
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
363 [None, None, None, None]
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
364
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
365 Ensure that explicitly raising StopIteration acts like any other exception
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
366 in try/except, not like a return.
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
367
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
368 >>> def g():
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
369 ... yield 1
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
370 ... try:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
371 ... raise StopIteration
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
372 ... except:
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
373 ... yield 2
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
374 ... yield 3
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
375 >>> list(g())
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
376 [1, 2, 3]
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
377
18257
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
378 Next one was posted to c.l.py.
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
379
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
380 >>> def gcomb(x, k):
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
381 ... "Generate all combinations of k elements from list x."
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
382 ...
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
383 ... if k > len(x):
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
384 ... return
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
385 ... if k == 0:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
386 ... yield []
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
387 ... else:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
388 ... first, rest = x[0], x[1:]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
389 ... # A combination does or doesn't contain first.
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
390 ... # If it does, the remainder is a k-1 comb of rest.
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
391 ... for c in gcomb(rest, k-1):
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
392 ... c.insert(0, first)
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
393 ... yield c
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
394 ... # If it doesn't contain first, it's a k comb of rest.
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
395 ... for c in gcomb(rest, k):
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
396 ... yield c
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
397
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
398 >>> seq = list(range(1, 5))
18257
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
399 >>> for k in range(len(seq) + 2):
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
400 ... print("%d-combs of %s:" % (k, seq))
18257
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
401 ... for c in gcomb(seq, k):
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
402 ... print(" ", c)
18257
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
403 0-combs of [1, 2, 3, 4]:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
404 []
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
405 1-combs of [1, 2, 3, 4]:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
406 [1]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
407 [2]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
408 [3]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
409 [4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
410 2-combs of [1, 2, 3, 4]:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
411 [1, 2]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
412 [1, 3]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
413 [1, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
414 [2, 3]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
415 [2, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
416 [3, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
417 3-combs of [1, 2, 3, 4]:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
418 [1, 2, 3]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
419 [1, 2, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
420 [1, 3, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
421 [2, 3, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
422 4-combs of [1, 2, 3, 4]:
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
423 [1, 2, 3, 4]
358cb74c0864 Added a "generate k-combinations of a list" example posted to c.l.py.
Tim Peters <tim.peters@gmail.com>
parents: 18256
diff changeset
424 5-combs of [1, 2, 3, 4]:
18273
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
425
18293
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
426 From the Iterators list, about the types of these things.
18273
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
427
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
428 >>> def g():
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
429 ... yield 1
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
430 ...
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
431 >>> type(g)
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
432 <class 'function'>
18273
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
433 >>> i = g()
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
434 >>> type(i)
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
435 <class 'generator'>
19665
08c3c9edd99b Make dir() wordier (see the new docstring). The new behavior is a mixed
Tim Peters <tim.peters@gmail.com>
parents: 19298
diff changeset
436 >>> [s for s in dir(i) if not s.startswith('_')]
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
437 ['close', 'gi_code', 'gi_frame', 'gi_running', 'send', 'throw']
81856
9c0cd608464e Issue #17041: Fix doctesting when Python is configured with the
Serhiy Storchaka <storchaka@gmail.com>
parents: 72972
diff changeset
438 >>> from test.support import HAVE_DOCSTRINGS
9c0cd608464e Issue #17041: Fix doctesting when Python is configured with the
Serhiy Storchaka <storchaka@gmail.com>
parents: 72972
diff changeset
439 >>> print(i.__next__.__doc__ if HAVE_DOCSTRINGS else 'x.__next__() <==> next(x)')
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
440 x.__next__() <==> next(x)
18273
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
441 >>> iter(i) is i
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
442 True
18273
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
443 >>> import types
9e326c9e6be5 Teach the types module about generators. Thanks to James Althoff on the
Tim Peters <tim.peters@gmail.com>
parents: 18265
diff changeset
444 >>> isinstance(i, types.GeneratorType)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
445 True
18293
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
446
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
447 And more, added later.
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
448
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
449 >>> i.gi_running
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
450 0
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
451 >>> type(i.gi_frame)
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
452 <class 'frame'>
18293
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
453 >>> i.gi_running = 42
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
454 Traceback (most recent call last):
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
455 ...
41317
b897b77a004f Make readonly members defined in C throw an AttributeError on modification. This brings them into sync with Python-level attributes. Fixes bug #1687163.
Collin Winter <collinw@gmail.com>
parents: 40949
diff changeset
456 AttributeError: readonly attribute
18293
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
457 >>> def g():
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
458 ... yield me.gi_running
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
459 >>> me = g()
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
460 >>> me.gi_running
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
461 0
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
462 >>> next(me)
18293
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
463 1
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
464 >>> me.gi_running
0344e4ac70b0 gen_getattr: make the gi_running and gi_frame members discoverable (but
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
465 0
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
466
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
467 A clever union-find implementation from c.l.py, due to David Eppstein.
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
468 Sent: Friday, June 29, 2001 12:16 PM
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
469 To: python-list@python.org
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
470 Subject: Re: PEP 255: Simple Generators
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
471
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
472 >>> class disjointSet:
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
473 ... def __init__(self, name):
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
474 ... self.name = name
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
475 ... self.parent = None
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
476 ... self.generator = self.generate()
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
477 ...
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
478 ... def generate(self):
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
479 ... while not self.parent:
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
480 ... yield self
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
481 ... for x in self.parent.generator:
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
482 ... yield x
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
483 ...
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
484 ... def find(self):
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
485 ... return next(self.generator)
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
486 ...
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
487 ... def union(self, parent):
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
488 ... if self.parent:
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
489 ... raise ValueError("Sorry, I'm not a root!")
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
490 ... self.parent = parent
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
491 ...
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
492 ... def __str__(self):
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
493 ... return self.name
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
494
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
495 >>> names = "ABCDEFGHIJKLM"
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
496 >>> sets = [disjointSet(name) for name in names]
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
497 >>> roots = sets[:]
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
498
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
499 >>> import random
44662
c25760c21397 Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents: 44129
diff changeset
500 >>> gen = random.Random(42)
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
501 >>> while 1:
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
502 ... for s in sets:
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
503 ... print(" %s->%s" % (s, s.find()), end='')
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
504 ... print()
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
505 ... if len(roots) > 1:
26834
3e1bd137d3c2 This test depends on the exact ordering produced by the WichmannHill
Raymond Hettinger <python@rcn.com>
parents: 24719
diff changeset
506 ... s1 = gen.choice(roots)
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
507 ... roots.remove(s1)
26834
3e1bd137d3c2 This test depends on the exact ordering produced by the WichmannHill
Raymond Hettinger <python@rcn.com>
parents: 24719
diff changeset
508 ... s2 = gen.choice(roots)
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
509 ... s1.union(s2)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
510 ... print("merged", s1, "into", s2)
18355
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
511 ... else:
98556cdfe716 A clever union-find implementation from c.l.py, due to David Eppstein.
Tim Peters <tim.peters@gmail.com>
parents: 18352
diff changeset
512 ... break
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
513 A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->K L->L M->M
64523
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
514 merged K into B
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
515 A->A B->B C->C D->D E->E F->F G->G H->H I->I J->J K->B L->L M->M
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
516 merged A into F
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
517 A->F B->B C->C D->D E->E F->F G->G H->H I->I J->J K->B L->L M->M
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
518 merged E into F
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
519 A->F B->B C->C D->D E->F F->F G->G H->H I->I J->J K->B L->L M->M
44662
c25760c21397 Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents: 44129
diff changeset
520 merged D into C
64523
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
521 A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->M
44662
c25760c21397 Remove defunct parts of the random module
Raymond Hettinger <python@rcn.com>
parents: 44129
diff changeset
522 merged M into C
64523
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
523 A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
524 merged J into B
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
525 A->F B->B C->C D->C E->F F->F G->G H->H I->I J->B K->B L->L M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
526 merged B into C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
527 A->F B->C C->C D->C E->F F->F G->G H->H I->I J->C K->C L->L M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
528 merged F into G
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
529 A->G B->C C->C D->C E->G F->G G->G H->H I->I J->C K->C L->L M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
530 merged L into C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
531 A->G B->C C->C D->C E->G F->G G->G H->H I->I J->C K->C L->C M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
532 merged G into I
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
533 A->I B->C C->C D->C E->I F->I G->I H->H I->I J->C K->C L->C M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
534 merged I into H
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
535 A->H B->C C->C D->C E->H F->H G->H H->H I->H J->C K->C L->C M->C
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
536 merged C into H
6095ea9dcff4 Fix test that depends on a particular implementation of random.choice().
Raymond Hettinger <python@rcn.com>
parents: 54918
diff changeset
537 A->H B->H C->H D->H E->H F->H G->H H->H I->H J->H K->H L->H M->H
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
538
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
539 """
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 24263
diff changeset
540 # Emacs turd '
18250
45de1ac2a376 Add all the examples from PEP 255, and a few email examples.
Tim Peters <tim.peters@gmail.com>
parents: 18249
diff changeset
541
18251
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
542 # Fun tests (for sufficiently warped notions of "fun").
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
543
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
544 fun_tests = """
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
545
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
546 Build up to a recursive Sieve of Eratosthenes generator.
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
547
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
548 >>> def firstn(g, n):
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
549 ... return [next(g) for i in range(n)]
18251
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
550
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
551 >>> def intsfrom(i):
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
552 ... while 1:
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
553 ... yield i
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
554 ... i += 1
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
555
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
556 >>> firstn(intsfrom(5), 7)
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
557 [5, 6, 7, 8, 9, 10, 11]
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
558
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
559 >>> def exclude_multiples(n, ints):
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
560 ... for i in ints:
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
561 ... if i % n:
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
562 ... yield i
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
563
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
564 >>> firstn(exclude_multiples(3, intsfrom(1)), 6)
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
565 [1, 2, 4, 5, 7, 8]
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
566
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
567 >>> def sieve(ints):
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
568 ... prime = next(ints)
18251
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
569 ... yield prime
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
570 ... not_divisible_by_prime = exclude_multiples(prime, ints)
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
571 ... for p in sieve(not_divisible_by_prime):
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
572 ... yield p
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
573
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
574 >>> primes = sieve(intsfrom(2))
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
575 >>> firstn(primes, 20)
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
576 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
577
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
578
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
579 Another famous problem: generate all integers of the form
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
580 2**i * 3**j * 5**k
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
581 in increasing order, where i,j,k >= 0. Trickier than it may look at first!
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
582 Try writing it without generators, and correctly, and without generating
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
583 3 internal results for each result output.
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
584
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
585 >>> def times(n, g):
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
586 ... for i in g:
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
587 ... yield n * i
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
588 >>> firstn(times(10, intsfrom(1)), 10)
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
589 [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
590
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
591 >>> def merge(g, h):
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
592 ... ng = next(g)
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
593 ... nh = next(h)
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
594 ... while 1:
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
595 ... if ng < nh:
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
596 ... yield ng
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
597 ... ng = next(g)
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
598 ... elif ng > nh:
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
599 ... yield nh
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
600 ... nh = next(h)
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
601 ... else:
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
602 ... yield ng
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
603 ... ng = next(g)
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
604 ... nh = next(h)
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
605
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
606 The following works, but is doing a whale of a lot of redundant work --
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
607 it's not clear how to get the internal uses of m235 to share a single
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
608 generator. Note that me_times2 (etc) each need to see every element in the
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
609 result sequence. So this is an example where lazy lists are more natural
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
610 (you can look at the head of a lazy list any number of times).
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
611
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
612 >>> def m235():
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
613 ... yield 1
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
614 ... me_times2 = times(2, m235())
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
615 ... me_times3 = times(3, m235())
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
616 ... me_times5 = times(5, m235())
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
617 ... for i in merge(merge(me_times2,
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
618 ... me_times3),
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
619 ... me_times5):
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
620 ... yield i
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
621
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
622 Don't print "too many" of these -- the implementation above is extremely
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
623 inefficient: each call of m235() leads to 3 recursive calls, and in
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
624 turn each of those 3 more, and so on, and so on, until we've descended
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
625 enough levels to satisfy the print stmts. Very odd: when I printed 5
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
626 lines of results below, this managed to screw up Win98's malloc in "the
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
627 usual" way, i.e. the heap grew over 4Mb so Win98 started fragmenting
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
628 address space, and it *looked* like a very slow leak.
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
629
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
630 >>> result = m235()
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
631 >>> for i in range(3):
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
632 ... print(firstn(result, 15))
18252
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
633 [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
634 [25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
065b17fc0d2e More tests.
Tim Peters <tim.peters@gmail.com>
parents: 18251
diff changeset
635 [81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
18254
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
636
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
637 Heh. Here's one way to get a shared list, complete with an excruciating
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
638 namespace renaming trick. The *pretty* part is that the times() and merge()
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
639 functions can be reused as-is, because they only assume their stream
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
640 arguments are iterable -- a LazyList is the same as a generator to times().
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
641
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
642 >>> class LazyList:
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
643 ... def __init__(self, g):
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
644 ... self.sofar = []
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
645 ... self.fetch = g.__next__
18254
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
646 ...
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
647 ... def __getitem__(self, i):
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
648 ... sofar, fetch = self.sofar, self.fetch
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
649 ... while i >= len(sofar):
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
650 ... sofar.append(fetch())
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
651 ... return sofar[i]
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
652
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
653 >>> def m235():
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
654 ... yield 1
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
655 ... # Gack: m235 below actually refers to a LazyList.
18254
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
656 ... me_times2 = times(2, m235)
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
657 ... me_times3 = times(3, m235)
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
658 ... me_times5 = times(5, m235)
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
659 ... for i in merge(merge(me_times2,
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
660 ... me_times3),
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
661 ... me_times5):
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
662 ... yield i
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
663
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
664 Print as many of these as you like -- *this* implementation is memory-
18509
254a4a9af0fb Remove reference cycle breaking code. The GC now takes care of it.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18384
diff changeset
665 efficient.
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
666
18254
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
667 >>> m235 = LazyList(m235())
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
668 >>> for i in range(5):
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
669 ... print([m235[j] for j in range(15*i, 15*(i+1))])
18254
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
670 [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
671 [25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
672 [81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
673 [200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
674 [400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
675
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
676 Ye olde Fibonacci generator, LazyList style.
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
677
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
678 >>> def fibgen(a, b):
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
679 ...
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
680 ... def sum(g, h):
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
681 ... while 1:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
682 ... yield next(g) + next(h)
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
683 ...
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
684 ... def tail(g):
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
685 ... next(g) # throw first away
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
686 ... for x in g:
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
687 ... yield x
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
688 ...
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
689 ... yield a
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
690 ... yield b
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
691 ... for s in sum(iter(fib),
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
692 ... tail(iter(fib))):
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
693 ... yield s
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
694
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
695 >>> fib = LazyList(fibgen(1, 2))
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
696 >>> firstn(iter(fib), 17)
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
697 [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
698
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
699
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
700 Running after your tail with itertools.tee (new in version 2.4)
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
701
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
702 The algorithms "m235" (Hamming) and Fibonacci presented above are both
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
703 examples of a whole family of FP (functional programming) algorithms
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
704 where a function produces and returns a list while the production algorithm
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
705 suppose the list as already produced by recursively calling itself.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
706 For these algorithms to work, they must:
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
707
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
708 - produce at least a first element without presupposing the existence of
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
709 the rest of the list
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
710 - produce their elements in a lazy manner
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
711
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
712 To work efficiently, the beginning of the list must not be recomputed over
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
713 and over again. This is ensured in most FP languages as a built-in feature.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
714 In python, we have to explicitly maintain a list of already computed results
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
715 and abandon genuine recursivity.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
716
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
717 This is what had been attempted above with the LazyList class. One problem
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
718 with that class is that it keeps a list of all of the generated results and
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
719 therefore continually grows. This partially defeats the goal of the generator
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
720 concept, viz. produce the results only as needed instead of producing them
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
721 all and thereby wasting memory.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
722
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
723 Thanks to itertools.tee, it is now clear "how to get the internal uses of
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
724 m235 to share a single generator".
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
725
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
726 >>> from itertools import tee
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
727 >>> def m235():
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
728 ... def _m235():
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
729 ... yield 1
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
730 ... for n in merge(times(2, m2),
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
731 ... merge(times(3, m3),
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
732 ... times(5, m5))):
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
733 ... yield n
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
734 ... m1 = _m235()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
735 ... m2, m3, m5, mRes = tee(m1, 4)
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
736 ... return mRes
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
737
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
738 >>> it = m235()
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
739 >>> for i in range(5):
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
740 ... print(firstn(it, 15))
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
741 [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
742 [25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
743 [81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
744 [200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
745 [400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
746
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
747 The "tee" function does just what we want. It internally keeps a generated
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
748 result for as long as it has not been "consumed" from all of the duplicated
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
749 iterators, whereupon it is deleted. You can therefore print the hamming
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
750 sequence during hours without increasing memory usage, or very little.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
751
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
752 The beauty of it is that recursive running-after-their-tail FP algorithms
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
753 are quite straightforwardly expressed with this Python idiom.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
754
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
755 Ye olde Fibonacci generator, tee style.
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
756
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
757 >>> def fib():
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35419
diff changeset
758 ...
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
759 ... def _isum(g, h):
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
760 ... while 1:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
761 ... yield next(g) + next(h)
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35419
diff changeset
762 ...
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
763 ... def _fib():
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
764 ... yield 1
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
765 ... yield 2
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
766 ... next(fibTail) # throw first away
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
767 ... for res in _isum(fibHead, fibTail):
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
768 ... yield res
35465
3e5c07faab11 Whitespace normalization (via reindent.py).
Tim Peters <tim.peters@gmail.com>
parents: 35419
diff changeset
769 ...
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
770 ... realfib = _fib()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
771 ... fibHead, fibTail, fibRes = tee(realfib, 3)
35419
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
772 ... return fibRes
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
773
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
774 >>> firstn(fib(), 17)
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
775 [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
2a6b6942a7f7 [ 1113421 ] New tutorial tests in test_generators.py
Georg Brandl <georg@python.org>
parents: 35320
diff changeset
776
18251
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
777 """
5d3e7f99cf52 Add a recursive Sieve of Eratosthenes prime generator. Not practical,
Tim Peters <tim.peters@gmail.com>
parents: 18250
diff changeset
778
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
779 # syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
780 # hackery.
18254
0514b16c1a68 Another variant of the 2-3-5 test, mixing generators with a LazyList class.
Tim Peters <tim.peters@gmail.com>
parents: 18252
diff changeset
781
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
782 syntax_tests = """
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
783
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
784 These are fine:
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
785
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
786 >>> def f():
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
787 ... yield 1
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
788 ... return
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
789
33308
f28072e64f81 Don't really need ellipsis doctests for the syntax errors, because
Tim Peters <tim.peters@gmail.com>
parents: 33306
diff changeset
790 >>> def f():
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
791 ... try:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
792 ... yield 1
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
793 ... finally:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
794 ... pass
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
795
33308
f28072e64f81 Don't really need ellipsis doctests for the syntax errors, because
Tim Peters <tim.peters@gmail.com>
parents: 33306
diff changeset
796 >>> def f():
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
797 ... try:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
798 ... try:
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
799 ... 1//0
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
800 ... except ZeroDivisionError:
36121
686306a67e9b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 35959
diff changeset
801 ... yield 666
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
802 ... except:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
803 ... pass
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
804 ... finally:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
805 ... pass
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
806
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
807 >>> def f():
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
808 ... try:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
809 ... try:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
810 ... yield 12
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
811 ... 1//0
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
812 ... except ZeroDivisionError:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
813 ... yield 666
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
814 ... except:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
815 ... try:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
816 ... x = 12
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
817 ... finally:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
818 ... yield 12
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
819 ... except:
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
820 ... return
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
821 >>> list(f())
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
822 [12, 666]
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
823
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
824 >>> def f():
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
825 ... yield
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
826 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
827 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
828
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
829
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
830 >>> def f():
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
831 ... if 0:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
832 ... yield
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
833 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
834 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
835
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
836
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
837 >>> def f():
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
838 ... if 0:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
839 ... yield 1
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
840 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
841 <class 'generator'>
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
842
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
843 >>> def f():
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
844 ... if "":
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
845 ... yield None
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
846 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
847 <class 'generator'>
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
848
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
849 >>> def f():
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
850 ... return
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
851 ... try:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
852 ... if x==4:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
853 ... pass
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
854 ... elif 0:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
855 ... try:
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
856 ... 1//0
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
857 ... except SyntaxError:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
858 ... pass
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
859 ... else:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
860 ... if 0:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
861 ... while 12:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
862 ... x += 1
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
863 ... yield 2 # don't blink
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
864 ... f(a, b, c, d, e)
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
865 ... else:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
866 ... pass
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
867 ... except:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
868 ... x = 1
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
869 ... return
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
870 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
871 <class 'generator'>
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
872
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
873 >>> def f():
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
874 ... if 0:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
875 ... def g():
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
876 ... yield 1
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
877 ...
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
878 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
879 <class 'NoneType'>
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
880
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
881 >>> def f():
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
882 ... if 0:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
883 ... class C:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
884 ... def __init__(self):
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
885 ... yield 1
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
886 ... def f(self):
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
887 ... yield 2
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18273
diff changeset
888 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
889 <class 'NoneType'>
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
890
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
891 >>> def f():
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
892 ... if 0:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
893 ... return
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
894 ... if 0:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
895 ... yield 2
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
896 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
897 <class 'generator'>
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18299
diff changeset
898
23785
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
899 This one caused a crash (see SF bug 567538):
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
900
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
901 >>> def f():
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
902 ... for i in range(3):
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
903 ... try:
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
904 ... continue
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
905 ... finally:
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
906 ... yield i
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23785
diff changeset
907 ...
23785
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
908 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
909 >>> print(next(g))
23785
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
910 0
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
911 >>> print(next(g))
23785
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
912 1
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
913 >>> print(next(g))
23785
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
914 2
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
915 >>> print(next(g))
23785
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
916 Traceback (most recent call last):
7a32c2356e56 SF bug 567538: Generator can crash the interpreter (Finn Bock).
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
917 StopIteration
44999
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
918
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
919
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
920 Test the gi_code attribute
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
921
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
922 >>> def f():
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
923 ... yield 5
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
924 ...
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
925 >>> g = f()
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
926 >>> g.gi_code is f.__code__
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
927 True
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
928 >>> next(g)
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
929 5
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
930 >>> next(g)
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
931 Traceback (most recent call last):
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
932 StopIteration
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
933 >>> g.gi_code is f.__code__
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
934 True
7c02182e2979 Merged revisions 60284-60349 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44662
diff changeset
935
47082
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
936
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
937 Test the __name__ attribute and the repr()
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
938
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
939 >>> def f():
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
940 ... yield 5
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
941 ...
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
942 >>> g = f()
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
943 >>> g.__name__
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
944 'f'
b630729571e6 Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46336
diff changeset
945 >>> repr(g) # doctest: +ELLIPSIS
47109
a02bc2a84d19 Merged revisions 63361-63373,63375,63377-63380 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47082
diff changeset
946 '<generator object f at ...>'
50313
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
947
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
948 Lambdas shouldn't have their usual return behavior.
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
949
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
950 >>> x = lambda: (yield 1)
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
951 >>> list(x())
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
952 [1]
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
953
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
954 >>> x = lambda: ((yield 1), (yield 2))
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
955 >>> list(x())
bcdfdc369570 Merged revisions 67954 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
956 [1, 2]
18256
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
957 """
d50d4f81aa4c New tests to provoke SyntaxErrors unique to generators. Minor fiddling
Tim Peters <tim.peters@gmail.com>
parents: 18254
diff changeset
958
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
959 # conjoin is a simple backtracking generator, named in honor of Icon's
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
960 # "conjunction" control structure. Pass a list of no-argument functions
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
961 # that return iterable objects. Easiest to explain by example: assume the
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
962 # function list [x, y, z] is passed. Then conjoin acts like:
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
963 #
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
964 # def g():
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
965 # values = [None] * 3
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
966 # for values[0] in x():
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
967 # for values[1] in y():
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
968 # for values[2] in z():
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
969 # yield values
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
970 #
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
971 # So some 3-lists of values *may* be generated, each time we successfully
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
972 # get into the innermost loop. If an iterator fails (is exhausted) before
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
973 # then, it "backtracks" to get the next value from the nearest enclosing
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
974 # iterator (the one "to the left"), and starts all over again at the next
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
975 # slot (pumps a fresh iterator). Of course this is most useful when the
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
976 # iterators have side-effects, so that which values *can* be generated at
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
977 # each slot depend on the values iterated at previous slots.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
978
54918
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54703
diff changeset
979 def simple_conjoin(gs):
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
980
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
981 values = [None] * len(gs)
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
982
54918
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54703
diff changeset
983 def gen(i):
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
984 if i >= len(gs):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
985 yield values
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
986 else:
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
987 for values[i] in gs[i]():
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
988 for x in gen(i+1):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
989 yield x
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
990
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
991 for x in gen(0):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
992 yield x
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
993
18352
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
994 # That works fine, but recursing a level and checking i against len(gs) for
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
995 # each item produced is inefficient. By doing manual loop unrolling across
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
996 # generator boundaries, it's possible to eliminate most of that overhead.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
997 # This isn't worth the bother *in general* for generators, but conjoin() is
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
998 # a core building block for some CPU-intensive generator applications.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
999
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1000 def conjoin(gs):
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1001
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1002 n = len(gs)
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1003 values = [None] * n
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1004
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1005 # Do one loop nest at time recursively, until the # of loop nests
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1006 # remaining is divisible by 3.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1007
54918
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54703
diff changeset
1008 def gen(i):
18352
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1009 if i >= n:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1010 yield values
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1011
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1012 elif (n-i) % 3:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1013 ip1 = i+1
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1014 for values[i] in gs[i]():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1015 for x in gen(ip1):
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1016 yield x
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1017
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1018 else:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1019 for x in _gen3(i):
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1020 yield x
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1021
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1022 # Do three loop nests at a time, recursing only if at least three more
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1023 # remain. Don't call directly: this is an internal optimization for
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1024 # gen's use.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1025
54918
0704f15b4682 Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54703
diff changeset
1026 def _gen3(i):
18352
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1027 assert i < n and (n-i) % 3 == 0
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1028 ip1, ip2, ip3 = i+1, i+2, i+3
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1029 g, g1, g2 = gs[i : ip3]
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1030
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1031 if ip3 >= n:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1032 # These are the last three, so we can yield values directly.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1033 for values[i] in g():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1034 for values[ip1] in g1():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1035 for values[ip2] in g2():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1036 yield values
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1037
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1038 else:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1039 # At least 6 loop nests remain; peel off 3 and recurse for the
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1040 # rest.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1041 for values[i] in g():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1042 for values[ip1] in g1():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1043 for values[ip2] in g2():
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1044 for x in _gen3(ip3):
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1045 yield x
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1046
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1047 for x in gen(0):
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1048 yield x
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1049
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1050 # And one more approach: For backtracking apps like the Knight's Tour
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1051 # solver below, the number of backtracking levels can be enormous (one
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1052 # level per square, for the Knight's Tour, so that e.g. a 100x100 board
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1053 # needs 10,000 levels). In such cases Python is likely to run out of
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1054 # stack space due to recursion. So here's a recursion-free version of
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1055 # conjoin too.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1056 # NOTE WELL: This allows large problems to be solved with only trivial
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1057 # demands on stack space. Without explicitly resumable generators, this is
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1058 # much harder to achieve. OTOH, this is much slower (up to a factor of 2)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1059 # than the fancy unrolled recursive conjoin.
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1060
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1061 def flat_conjoin(gs): # rename to conjoin to run tests with this instead
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1062 n = len(gs)
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1063 values = [None] * n
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1064 iters = [None] * n
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1065 _StopIteration = StopIteration # make local because caught a *lot*
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1066 i = 0
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1067 while 1:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1068 # Descend.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1069 try:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1070 while i < n:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1071 it = iters[i] = gs[i]().__next__
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1072 values[i] = it()
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1073 i += 1
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1074 except _StopIteration:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1075 pass
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1076 else:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1077 assert i == n
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1078 yield values
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1079
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1080 # Backtrack until an older iterator can be resumed.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1081 i -= 1
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1082 while i >= 0:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1083 try:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1084 values[i] = iters[i]()
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1085 # Success! Start fresh at next level.
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1086 i += 1
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1087 break
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1088 except _StopIteration:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1089 # Continue backtracking.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1090 i -= 1
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1091 else:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1092 assert i < 0
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1093 break
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1094
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1095 # A conjoin-based N-Queens solver.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1096
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1097 class Queens:
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1098 def __init__(self, n):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1099 self.n = n
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1100 rangen = range(n)
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1101
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1102 # Assign a unique int to each column and diagonal.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1103 # columns: n of those, range(n).
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1104 # NW-SE diagonals: 2n-1 of these, i-j unique and invariant along
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1105 # each, smallest i-j is 0-(n-1) = 1-n, so add n-1 to shift to 0-
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1106 # based.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1107 # NE-SW diagonals: 2n-1 of these, i+j unique and invariant along
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1108 # each, smallest i+j is 0, largest is 2n-2.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1109
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1110 # For each square, compute a bit vector of the columns and
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1111 # diagonals it covers, and for each row compute a function that
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1112 # generates the possiblities for the columns in that row.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1113 self.rowgenerators = []
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1114 for i in rangen:
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1115 rowuses = [(1 << j) | # column ordinal
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1116 (1 << (n + i-j + n-1)) | # NW-SE ordinal
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
1117 (1 << (n + 2*n-1 + i+j)) # NE-SW ordinal
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1118 for j in rangen]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1119
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1120 def rowgen(rowuses=rowuses):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1121 for j in rangen:
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1122 uses = rowuses[j]
18352
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1123 if uses & self.used == 0:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1124 self.used |= uses
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1125 yield j
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1126 self.used &= ~uses
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1127
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1128 self.rowgenerators.append(rowgen)
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1129
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1130 # Generate solutions.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1131 def solve(self):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1132 self.used = 0
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1133 for row2col in conjoin(self.rowgenerators):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1134 yield row2col
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1135
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1136 def printsolution(self, row2col):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1137 n = self.n
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1138 assert n == len(row2col)
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1139 sep = "+" + "-+" * n
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1140 print(sep)
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1141 for i in range(n):
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1142 squares = [" " for j in range(n)]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1143 squares[row2col[i]] = "Q"
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1144 print("|" + "|".join(squares) + "|")
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1145 print(sep)
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1146
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1147 # A conjoin-based Knight's Tour solver. This is pretty sophisticated
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1148 # (e.g., when used with flat_conjoin above, and passing hard=1 to the
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1149 # constructor, a 200x200 Knight's Tour was found quickly -- note that we're
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1150 # creating 10s of thousands of generators then!), and is lengthy.
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1151
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1152 class Knights:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1153 def __init__(self, m, n, hard=0):
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1154 self.m, self.n = m, n
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1155
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1156 # solve() will set up succs[i] to be a list of square #i's
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1157 # successors.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1158 succs = self.succs = []
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1159
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1160 # Remove i0 from each of its successor's successor lists, i.e.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1161 # successors can't go back to i0 again. Return 0 if we can
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1162 # detect this makes a solution impossible, else return 1.
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1163
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1164 def remove_from_successors(i0, len=len):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1165 # If we remove all exits from a free square, we're dead:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1166 # even if we move to it next, we can't leave it again.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1167 # If we create a square with one exit, we must visit it next;
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1168 # else somebody else will have to visit it, and since there's
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1169 # only one adjacent, there won't be a way to leave it again.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1170 # Finelly, if we create more than one free square with a
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1171 # single exit, we can only move to one of them next, leaving
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1172 # the other one a dead end.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1173 ne0 = ne1 = 0
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1174 for i in succs[i0]:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1175 s = succs[i]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1176 s.remove(i0)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1177 e = len(s)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1178 if e == 0:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1179 ne0 += 1
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1180 elif e == 1:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1181 ne1 += 1
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1182 return ne0 == 0 and ne1 < 2
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1183
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1184 # Put i0 back in each of its successor's successor lists.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1185
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1186 def add_to_successors(i0):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1187 for i in succs[i0]:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1188 succs[i].append(i0)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1189
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1190 # Generate the first move.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1191 def first():
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1192 if m < 1 or n < 1:
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1193 return
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1194
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1195 # Since we're looking for a cycle, it doesn't matter where we
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1196 # start. Starting in a corner makes the 2nd move easy.
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1197 corner = self.coords2index(0, 0)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1198 remove_from_successors(corner)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1199 self.lastij = corner
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1200 yield corner
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1201 add_to_successors(corner)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1202
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1203 # Generate the second moves.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1204 def second():
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1205 corner = self.coords2index(0, 0)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1206 assert self.lastij == corner # i.e., we started in the corner
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1207 if m < 3 or n < 3:
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1208 return
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1209 assert len(succs[corner]) == 2
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1210 assert self.coords2index(1, 2) in succs[corner]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1211 assert self.coords2index(2, 1) in succs[corner]
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1212 # Only two choices. Whichever we pick, the other must be the
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1213 # square picked on move m*n, as it's the only way to get back
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1214 # to (0, 0). Save its index in self.final so that moves before
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1215 # the last know it must be kept free.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1216 for i, j in (1, 2), (2, 1):
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1217 this = self.coords2index(i, j)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1218 final = self.coords2index(3-i, 3-j)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1219 self.final = final
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1220
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1221 remove_from_successors(this)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1222 succs[final].append(corner)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1223 self.lastij = this
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1224 yield this
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1225 succs[final].remove(corner)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1226 add_to_successors(this)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1227
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1228 # Generate moves 3 thru m*n-1.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1229 def advance(len=len):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1230 # If some successor has only one exit, must take it.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1231 # Else favor successors with fewer exits.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1232 candidates = []
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1233 for i in succs[self.lastij]:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1234 e = len(succs[i])
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1235 assert e > 0, "else remove_from_successors() pruning flawed"
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1236 if e == 1:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1237 candidates = [(e, i)]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1238 break
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1239 candidates.append((e, i))
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1240 else:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1241 candidates.sort()
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1242
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1243 for e, i in candidates:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1244 if i != self.final:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1245 if remove_from_successors(i):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1246 self.lastij = i
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1247 yield i
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1248 add_to_successors(i)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1249
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1250 # Generate moves 3 thru m*n-1. Alternative version using a
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1251 # stronger (but more expensive) heuristic to order successors.
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1252 # Since the # of backtracking levels is m*n, a poor move early on
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1253 # can take eons to undo. Smallest square board for which this
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1254 # matters a lot is 52x52.
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1255 def advance_hard(vmid=(m-1)/2.0, hmid=(n-1)/2.0, len=len):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1256 # If some successor has only one exit, must take it.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1257 # Else favor successors with fewer exits.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1258 # Break ties via max distance from board centerpoint (favor
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1259 # corners and edges whenever possible).
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1260 candidates = []
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1261 for i in succs[self.lastij]:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1262 e = len(succs[i])
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1263 assert e > 0, "else remove_from_successors() pruning flawed"
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1264 if e == 1:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1265 candidates = [(e, 0, i)]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1266 break
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1267 i1, j1 = self.index2coords(i)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1268 d = (i1 - vmid)**2 + (j1 - hmid)**2
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1269 candidates.append((e, -d, i))
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1270 else:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1271 candidates.sort()
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1272
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1273 for e, d, i in candidates:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1274 if i != self.final:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1275 if remove_from_successors(i):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1276 self.lastij = i
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1277 yield i
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1278 add_to_successors(i)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1279
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1280 # Generate the last move.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1281 def last():
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1282 assert self.final in succs[self.lastij]
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1283 yield self.final
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1284
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1285 if m*n < 4:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1286 self.squaregenerators = [first]
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1287 else:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1288 self.squaregenerators = [first, second] + \
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1289 [hard and advance_hard or advance] * (m*n - 3) + \
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1290 [last]
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1291
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1292 def coords2index(self, i, j):
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1293 assert 0 <= i < self.m
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1294 assert 0 <= j < self.n
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1295 return i * self.n + j
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1296
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1297 def index2coords(self, index):
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1298 assert 0 <= index < self.m * self.n
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1299 return divmod(index, self.n)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1300
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1301 def _init_board(self):
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1302 succs = self.succs
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1303 del succs[:]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1304 m, n = self.m, self.n
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1305 c2i = self.coords2index
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1306
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1307 offsets = [( 1, 2), ( 2, 1), ( 2, -1), ( 1, -2),
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1308 (-1, -2), (-2, -1), (-2, 1), (-1, 2)]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1309 rangen = range(n)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1310 for i in range(m):
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1311 for j in rangen:
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1312 s = [c2i(i+io, j+jo) for io, jo in offsets
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1313 if 0 <= i+io < m and
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1314 0 <= j+jo < n]
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1315 succs.append(s)
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1316
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1317 # Generate solutions.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1318 def solve(self):
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1319 self._init_board()
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1320 for x in conjoin(self.squaregenerators):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1321 yield x
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1322
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1323 def printsolution(self, x):
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1324 m, n = self.m, self.n
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1325 assert len(x) == m*n
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1326 w = len(str(m*n))
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1327 format = "%" + str(w) + "d"
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1328
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1329 squares = [[None] * n for i in range(m)]
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1330 k = 1
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1331 for i in x:
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1332 i1, j1 = self.index2coords(i)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1333 squares[i1][j1] = format % k
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1334 k += 1
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1335
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1336 sep = "+" + ("-" * w + "+") * n
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1337 print(sep)
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1338 for i in range(m):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1339 row = squares[i]
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1340 print("|" + "|".join(row) + "|")
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1341 print(sep)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1342
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1343 conjoin_tests = """
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1344
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1345 Generate the 3-bit binary numbers in order. This illustrates dumbest-
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1346 possible use of conjoin, just to generate the full cross-product.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1347
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1348 >>> for c in conjoin([lambda: iter((0, 1))] * 3):
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1349 ... print(c)
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1350 [0, 0, 0]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1351 [0, 0, 1]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1352 [0, 1, 0]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1353 [0, 1, 1]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1354 [1, 0, 0]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1355 [1, 0, 1]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1356 [1, 1, 0]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1357 [1, 1, 1]
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1358
18352
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1359 For efficiency in typical backtracking apps, conjoin() yields the same list
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1360 object each time. So if you want to save away a full account of its
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1361 generated sequence, you need to copy its results.
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1362
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1363 >>> def gencopy(iterator):
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1364 ... for x in iterator:
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1365 ... yield x[:]
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1366
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1367 >>> for n in range(10):
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1368 ... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1369 ... print(n, len(all), all[0] == [0] * n, all[-1] == [1] * n)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1370 0 1 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1371 1 2 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1372 2 4 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1373 3 8 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1374 4 16 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1375 5 32 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1376 6 64 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1377 7 128 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1378 8 256 True True
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1379 9 512 True True
18352
3eda6231e1c2 Derive an industrial-strength conjoin() via cross-recursion loop unrolling,
Tim Peters <tim.peters@gmail.com>
parents: 18336
diff changeset
1380
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1381 And run an 8-queens solver.
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1382
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1383 >>> q = Queens(8)
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1384 >>> LIMIT = 2
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1385 >>> count = 0
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1386 >>> for row2col in q.solve():
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1387 ... count += 1
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1388 ... if count <= LIMIT:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1389 ... print("Solution", count)
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1390 ... q.printsolution(row2col)
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1391 Solution 1
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1392 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1393 |Q| | | | | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1394 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1395 | | | | |Q| | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1396 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1397 | | | | | | | |Q|
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1398 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1399 | | | | | |Q| | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1400 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1401 | | |Q| | | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1402 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1403 | | | | | | |Q| |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1404 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1405 | |Q| | | | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1406 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1407 | | | |Q| | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1408 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1409 Solution 2
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1410 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1411 |Q| | | | | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1412 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1413 | | | | | |Q| | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1414 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1415 | | | | | | | |Q|
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1416 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1417 | | |Q| | | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1418 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1419 | | | | | | |Q| |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1420 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1421 | | | |Q| | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1422 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1423 | |Q| | | | | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1424 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1425 | | | | |Q| | | |
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1426 +-+-+-+-+-+-+-+-+
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1427
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1428 >>> print(count, "solutions in all.")
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1429 92 solutions in all.
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1430
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1431 And run a Knight's Tour on a 10x10 board. Note that there are about
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1432 20,000 solutions even on a 6x6 board, so don't dare run this to exhaustion.
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1433
18541
70461f2d2109 Having fun on my own time: quicker flat_conjoin; Knights Tour solver
Tim Peters <tim.peters@gmail.com>
parents: 18526
diff changeset
1434 >>> k = Knights(10, 10)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1435 >>> LIMIT = 2
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1436 >>> count = 0
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1437 >>> for x in k.solve():
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1438 ... count += 1
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1439 ... if count <= LIMIT:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1440 ... print("Solution", count)
18384
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1441 ... k.printsolution(x)
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1442 ... else:
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1443 ... break
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1444 Solution 1
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1445 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1446 | 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1447 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1448 | 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1449 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1450 | 59|100| 73| 36| 41| 56| 39| 32| 9| 6|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1451 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1452 | 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1453 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1454 | 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1455 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1456 | 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1457 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1458 | 87| 98| 91| 80| 77| 84| 53| 46| 65| 44|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1459 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1460 | 90| 23| 88| 95| 70| 79| 68| 83| 14| 17|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1461 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1462 | 97| 92| 21| 78| 81| 94| 19| 16| 45| 66|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1463 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1464 | 22| 89| 96| 93| 20| 69| 82| 67| 18| 15|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1465 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1466 Solution 2
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1467 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1468 | 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1469 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1470 | 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1471 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1472 | 59|100| 73| 36| 41| 56| 39| 32| 9| 6|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1473 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1474 | 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1475 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1476 | 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1477 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1478 | 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1479 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1480 | 87| 98| 89| 80| 77| 84| 53| 46| 65| 44|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1481 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1482 | 90| 23| 92| 95| 70| 79| 68| 83| 14| 17|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1483 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1484 | 97| 88| 21| 78| 81| 94| 19| 16| 45| 66|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1485 +---+---+---+---+---+---+---+---+---+---+
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1486 | 22| 91| 96| 93| 20| 69| 82| 67| 18| 15|
95e3947ca3ce Added a non-recursive implementation of conjoin(), and a Knight's Tour
unknown <tools@python.org>
parents: 18355
diff changeset
1487 +---+---+---+---+---+---+---+---+---+---+
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1488 """
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1489
24719
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1490 weakref_tests = """\
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1491 Generators are weakly referencable:
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1492
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1493 >>> import weakref
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1494 >>> def gen():
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1495 ... yield 'foo!'
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1496 ...
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1497 >>> wr = weakref.ref(gen)
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1498 >>> wr() is gen
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1499 True
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1500 >>> p = weakref.proxy(gen)
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1501
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1502 Generator-iterators are weakly referencable as well:
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1503
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1504 >>> gi = gen()
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1505 >>> wr = weakref.ref(gi)
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1506 >>> wr() is gi
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1507 True
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1508 >>> p = weakref.proxy(gi)
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1509 >>> list(p)
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1510 ['foo!']
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1511
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1512 """
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1513
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1514 coroutine_tests = """\
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1515 Sending a value into a started generator:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1516
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1517 >>> def f():
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1518 ... print((yield 1))
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1519 ... yield 2
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1520 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1521 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1522 1
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1523 >>> g.send(42)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1524 42
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1525 2
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1526
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1527 Sending a value into a new generator produces a TypeError:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1528
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1529 >>> f().send("foo")
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1530 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1531 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1532 TypeError: can't send non-None value to a just-started generator
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1533
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1534
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1535 Yield by itself yields None:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1536
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1537 >>> def f(): yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1538 >>> list(f())
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1539 [None]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1540
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1541
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1542
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1543 An obscene abuse of a yield expression within a generator expression:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1544
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1545 >>> list((yield 21) for i in range(4))
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1546 [21, None, 21, None, 21, None, 21, None]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1547
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1548 And a more sane, but still weird usage:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1549
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1550 >>> def f(): list(i for i in [(yield 26)])
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1551 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
1552 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1553
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1554
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1555 A yield expression with augmented assignment.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1556
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1557 >>> def coroutine(seq):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1558 ... count = 0
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1559 ... while count < 200:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1560 ... count += yield
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1561 ... seq.append(count)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1562 >>> seq = []
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1563 >>> c = coroutine(seq)
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1564 >>> next(c)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1565 >>> print(seq)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1566 []
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1567 >>> c.send(10)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1568 >>> print(seq)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1569 [10]
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1570 >>> c.send(10)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1571 >>> print(seq)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1572 [10, 20]
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1573 >>> c.send(10)
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1574 >>> print(seq)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1575 [10, 20, 30]
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1576
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1577
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1578 Check some syntax errors for yield expressions:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1579
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1580 >>> f=lambda: (yield 1),(yield 2)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1581 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1582 ...
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1583 SyntaxError: 'yield' outside function
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1584
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1585 >>> def f(): x = yield = y
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1586 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1587 ...
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1588 SyntaxError: assignment to yield expression not possible
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1589
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1590 >>> def f(): (yield bar) = y
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1591 Traceback (most recent call last):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1592 ...
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1593 SyntaxError: can't assign to yield expression
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1594
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1595 >>> def f(): (yield bar) += y
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1596 Traceback (most recent call last):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1597 ...
54703
6370d71e3ca3 Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50313
diff changeset
1598 SyntaxError: can't assign to yield expression
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1599
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1600
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1601 Now check some throw() conditions:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1602
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1603 >>> def f():
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1604 ... while True:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1605 ... try:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1606 ... print((yield))
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39543
diff changeset
1607 ... except ValueError as v:
40790
8d9a0ec4d976 Fix another test.
Guido van Rossum <guido@python.org>
parents: 40785
diff changeset
1608 ... print("caught ValueError (%s)" % (v))
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1609 >>> import sys
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1610 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1611 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1612
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1613 >>> g.throw(ValueError) # type only
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1614 caught ValueError ()
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1615
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1616 >>> g.throw(ValueError("xyz")) # value only
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1617 caught ValueError (xyz)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1618
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1619 >>> g.throw(ValueError, ValueError(1)) # value+matching type
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1620 caught ValueError (1)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1621
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1622 >>> g.throw(ValueError, TypeError(1)) # mismatched type, rewrapped
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1623 caught ValueError (1)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1624
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1625 >>> g.throw(ValueError, ValueError(1), None) # explicit None traceback
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1626 caught ValueError (1)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1627
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1628 >>> g.throw(ValueError(1), "foo") # bad args
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1629 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1630 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1631 TypeError: instance exception may not have a separate value
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1632
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1633 >>> g.throw(ValueError, "foo", 23) # bad args
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1634 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1635 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1636 TypeError: throw() third argument must be a traceback object
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1637
39543
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1638 >>> g.throw("abc")
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1639 Traceback (most recent call last):
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1640 ...
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1641 TypeError: exceptions must be classes or instances deriving from BaseException, not str
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1642
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1643 >>> g.throw(0)
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1644 Traceback (most recent call last):
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1645 ...
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1646 TypeError: exceptions must be classes or instances deriving from BaseException, not int
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1647
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1648 >>> g.throw(list)
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1649 Traceback (most recent call last):
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1650 ...
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1651 TypeError: exceptions must be classes or instances deriving from BaseException, not type
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1652
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1653 >>> def throw(g,exc):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1654 ... try:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1655 ... raise exc
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1656 ... except:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1657 ... g.throw(*sys.exc_info())
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1658 >>> throw(g,ValueError) # do it with traceback included
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1659 caught ValueError ()
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1660
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1661 >>> g.send(1)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1662 1
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1663
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1664 >>> throw(g,TypeError) # terminate the generator
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1665 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1666 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1667 TypeError
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1668
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1669 >>> print(g.gi_frame)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1670 None
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1671
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1672 >>> g.send(2)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1673 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1674 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1675 StopIteration
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1676
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1677 >>> g.throw(ValueError,6) # throw on closed generator
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1678 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1679 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1680 ValueError: 6
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1681
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1682 >>> f().throw(ValueError,7) # throw on just-opened generator
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1683 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1684 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1685 ValueError: 7
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1686
72972
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1687 Plain "raise" inside a generator should preserve the traceback (#13188).
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1688 The traceback should have 3 levels:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1689 - g.throw()
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1690 - f()
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1691 - 1/0
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1692
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1693 >>> def f():
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1694 ... try:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1695 ... yield
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1696 ... except:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1697 ... raise
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1698 >>> g = f()
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1699 >>> try:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1700 ... 1/0
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1701 ... except ZeroDivisionError as v:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1702 ... try:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1703 ... g.throw(v)
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1704 ... except Exception as w:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1705 ... tb = w.__traceback__
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1706 >>> levels = 0
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1707 >>> while tb:
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1708 ... levels += 1
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1709 ... tb = tb.tb_next
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1710 >>> levels
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1711 3
dcf5cc88d5c9 Issue #13188: When called without an explicit traceback argument,
Antoine Pitrou <solipsis@pitrou.net>
parents: 64523
diff changeset
1712
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1713 Now let's try closing a generator:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1714
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1715 >>> def f():
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1716 ... try: yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1717 ... except GeneratorExit:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1718 ... print("exiting")
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1719
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1720 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1721 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1722 >>> g.close()
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1723 exiting
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1724 >>> g.close() # should be no-op now
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1725
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1726 >>> f().close() # close on just-opened generator should be fine
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1727
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1728 >>> def f(): yield # an even simpler generator
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1729 >>> f().close() # close before opening
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1730 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1731 >>> next(g)
35320
fa95fe91cdf7 Whitespace normalization (ran reindent.py over the whole tree).
Tim Peters <tim.peters@gmail.com>
parents: 35298
diff changeset
1732 >>> g.close() # close normally
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1733
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1734 And finalization:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1735
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1736 >>> def f():
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1737 ... try: yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1738 ... finally:
40785
22760d53d4da Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1739 ... print("exiting")
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1740
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1741 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1742 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1743 >>> del g
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1744 exiting
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1745
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1746
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1747 GeneratorExit is not caught by except Exception:
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1748
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1749 >>> def f():
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1750 ... try: yield
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1751 ... except Exception:
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1752 ... print('except')
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1753 ... finally:
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1754 ... print('finally')
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1755
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1756 >>> g = f()
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1757 >>> next(g)
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1758 >>> del g
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1759 finally
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1760
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44125
diff changeset
1761
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1762 Now let's try some ill-behaved generators:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1763
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1764 >>> def f():
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1765 ... try: yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1766 ... except GeneratorExit:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1767 ... yield "foo!"
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1768 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1769 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1770 >>> g.close()
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1771 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1772 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1773 RuntimeError: generator ignored GeneratorExit
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1774 >>> g.close()
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1775
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1776
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1777 Our ill-behaved code should be invoked during GC:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1778
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1779 >>> import sys, io
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1780 >>> old, sys.stderr = sys.stderr, io.StringIO()
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1781 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1782 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1783 >>> del g
80189
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1784 >>> "RuntimeError: generator ignored GeneratorExit" in sys.stderr.getvalue()
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1785 True
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1786 >>> sys.stderr = old
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1787
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1788
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1789 And errors thrown during closing should propagate:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1790
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1791 >>> def f():
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1792 ... try: yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1793 ... except GeneratorExit:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1794 ... raise TypeError("fie!")
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1795 >>> g = f()
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1796 >>> next(g)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1797 >>> g.close()
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1798 Traceback (most recent call last):
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1799 ...
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1800 TypeError: fie!
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1801
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1802
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1803 Ensure that various yield expression constructs make their
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1804 enclosing function a generator:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1805
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1806 >>> def f(): x += yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1807 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
1808 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1809
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1810 >>> def f(): x = yield
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1811 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
1812 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1813
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1814 >>> def f(): lambda x=(yield): 1
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1815 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
1816 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1817
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1818 >>> def f(): x=(i for i in (yield) if (yield))
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1819 >>> type(f())
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
1820 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1821
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1822 >>> def f(d): d[(yield "a")] = d[(yield "b")] = 27
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1823 >>> data = [1,2]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1824 >>> g = f(data)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1825 >>> type(g)
46336
56e49952d1a4 Bug #2565: The repr() of type objects now calls them 'class',
Martin v. Löwis <martin@v.loewis.de>
parents: 44999
diff changeset
1826 <class 'generator'>
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1827 >>> g.send(None)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1828 'a'
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1829 >>> data
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1830 [1, 2]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1831 >>> g.send(0)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1832 'b'
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1833 >>> data
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1834 [27, 2]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1835 >>> try: g.send(1)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1836 ... except StopIteration: pass
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1837 >>> data
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1838 [27, 27]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1839
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1840 """
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1841
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1842 refleaks_tests = """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1843 Prior to adding cycle-GC support to itertools.tee, this code would leak
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1844 references. We add it to the standard suite so the routine refleak-tests
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1845 would trigger if it starts being uncleanable again.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1846
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1847 >>> import itertools
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1848 >>> def leak():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1849 ... class gen:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1850 ... def __iter__(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1851 ... return self
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1852 ... def __next__(self):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1853 ... return self.item
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1854 ... g = gen()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1855 ... head, tail = itertools.tee(g)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1856 ... g.item = head
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1857 ... return head
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1858 >>> it = leak()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1859
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1860 Make sure to also test the involvement of the tee-internal teedataobject,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1861 which stores returned items.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1862
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 41317
diff changeset
1863 >>> item = next(it)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1864
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1865
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1866
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1867 This test leaked at one point due to generator finalization/destruction.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1868 It was copied from Lib/test/leakers/test_generator_cycle.py before the file
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1869 was removed.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1870
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1871 >>> def leak():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1872 ... def gen():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1873 ... while True:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1874 ... yield g
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1875 ... g = gen()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1876
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1877 >>> leak()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1878
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1880
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1881 This test isn't really generator related, but rather exception-in-cleanup
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1882 related. The coroutine tests (above) just happen to cause an exception in
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1883 the generator's __del__ (tp_del) method. We can also test for this
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1884 explicitly, without generators. We do have to redirect stderr to avoid
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1885 printing warnings and to doublecheck that we actually tested what we wanted
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1886 to test.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1887
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1888 >>> import sys, io
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1889 >>> old = sys.stderr
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1890 >>> try:
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
1891 ... sys.stderr = io.StringIO()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1892 ... class Leaker:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1893 ... def __del__(self):
80189
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1894 ... def invoke(message):
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1895 ... raise RuntimeError(message)
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1896 ... invoke("test")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1897 ...
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1898 ... l = Leaker()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1899 ... del l
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1900 ... err = sys.stderr.getvalue().strip()
80189
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1901 ... "Exception ignored in" in err
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1902 ... "RuntimeError: test" in err
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1903 ... "Traceback" in err
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1904 ... "in invoke" in err
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1905 ... finally:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1906 ... sys.stderr = old
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1907 True
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1908 True
80189
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1909 True
a281604a5c9a Issue #7317: Display full tracebacks when an error occurs asynchronously.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 74356
diff changeset
1910 True
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1911
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1912
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1913 These refleak tests should perhaps be in a testfile of their own,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1914 test_generators just happened to be the test that drew these out.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1915
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1916 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1917
18299
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
1918 __test__ = {"tut": tutorial_tests,
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
1919 "pep": pep_tests,
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
1920 "email": email_tests,
68c12d5556df This no longer leaks memory when run in an infinite loop. However,
Tim Peters <tim.peters@gmail.com>
parents: 18293
diff changeset
1921 "fun": fun_tests,
18336
794582fc0623 Added a simple but general backtracking generator (conjoin), and a couple
Tim Peters <tim.peters@gmail.com>
parents: 18333
diff changeset
1922 "syntax": syntax_tests,
24719
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1923 "conjoin": conjoin_tests,
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1924 "weakref": weakref_tests,
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 33468
diff changeset
1925 "coroutine": coroutine_tests,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36121
diff changeset
1926 "refleaks": refleaks_tests,
24719
5c77316b21f7 Add tests for weakref support for generator-iterators.
Fred Drake <fdrake@acm.org>
parents: 24364
diff changeset
1927 }
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1928
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1929 # Magic test name that regrtest.py invokes *after* importing this module.
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1930 # This worms around a bootstrap problem.
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1931 # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1932 # so this works as expected in both ways of running regrtest.
19817
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19665
diff changeset
1933 def test_main(verbose=None):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47109
diff changeset
1934 from test import support, test_generators
84911
3f994367a979 Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents: 83771
diff changeset
1935 support.run_unittest(__name__)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47109
diff changeset
1936 support.run_doctest(test_generators, verbose)
18249
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1937
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1938 # This part isn't needed for regrtest, but for running the test directly.
4826a6cdafc4 New std test for generators, initially populated with doctests NeilS put
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1939 if __name__ == "__main__":
19817
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19665
diff changeset
1940 test_main(1)