+++ /dev/null
-tutorial_tests = """\r
-Let's try a simple generator:\r
-\r
- >>> def f():\r
- ... yield 1\r
- ... yield 2\r
-\r
- >>> for i in f():\r
- ... print i\r
- 1\r
- 2\r
- >>> g = f()\r
- >>> g.next()\r
- 1\r
- >>> g.next()\r
- 2\r
-\r
-"Falling off the end" stops the generator:\r
-\r
- >>> g.next()\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- File "<stdin>", line 2, in g\r
- StopIteration\r
-\r
-"return" also stops the generator:\r
-\r
- >>> def f():\r
- ... yield 1\r
- ... return\r
- ... yield 2 # never reached\r
- ...\r
- >>> g = f()\r
- >>> g.next()\r
- 1\r
- >>> g.next()\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- File "<stdin>", line 3, in f\r
- StopIteration\r
- >>> g.next() # once stopped, can't be resumed\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- StopIteration\r
-\r
-"raise StopIteration" stops the generator too:\r
-\r
- >>> def f():\r
- ... yield 1\r
- ... raise StopIteration\r
- ... yield 2 # never reached\r
- ...\r
- >>> g = f()\r
- >>> g.next()\r
- 1\r
- >>> g.next()\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- StopIteration\r
- >>> g.next()\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- StopIteration\r
-\r
-However, they are not exactly equivalent:\r
-\r
- >>> def g1():\r
- ... try:\r
- ... return\r
- ... except:\r
- ... yield 1\r
- ...\r
- >>> list(g1())\r
- []\r
-\r
- >>> def g2():\r
- ... try:\r
- ... raise StopIteration\r
- ... except:\r
- ... yield 42\r
- >>> print list(g2())\r
- [42]\r
-\r
-This may be surprising at first:\r
-\r
- >>> def g3():\r
- ... try:\r
- ... return\r
- ... finally:\r
- ... yield 1\r
- ...\r
- >>> list(g3())\r
- [1]\r
-\r
-Let's create an alternate range() function implemented as a generator:\r
-\r
- >>> def yrange(n):\r
- ... for i in range(n):\r
- ... yield i\r
- ...\r
- >>> list(yrange(5))\r
- [0, 1, 2, 3, 4]\r
-\r
-Generators always return to the most recent caller:\r
-\r
- >>> def creator():\r
- ... r = yrange(5)\r
- ... print "creator", r.next()\r
- ... return r\r
- ...\r
- >>> def caller():\r
- ... r = creator()\r
- ... for i in r:\r
- ... print "caller", i\r
- ...\r
- >>> caller()\r
- creator 0\r
- caller 1\r
- caller 2\r
- caller 3\r
- caller 4\r
-\r
-Generators can call other generators:\r
-\r
- >>> def zrange(n):\r
- ... for i in yrange(n):\r
- ... yield i\r
- ...\r
- >>> list(zrange(5))\r
- [0, 1, 2, 3, 4]\r
-\r
-"""\r
-\r
-# The examples from PEP 255.\r
-\r
-pep_tests = """\r
-\r
-Specification: Yield\r
-\r
- Restriction: A generator cannot be resumed while it is actively\r
- running:\r
-\r
- >>> def g():\r
- ... i = me.next()\r
- ... yield i\r
- >>> me = g()\r
- >>> me.next()\r
- Traceback (most recent call last):\r
- ...\r
- File "<string>", line 2, in g\r
- ValueError: generator already executing\r
-\r
-Specification: Return\r
-\r
- Note that return isn't always equivalent to raising StopIteration: the\r
- difference lies in how enclosing try/except constructs are treated.\r
- For example,\r
-\r
- >>> def f1():\r
- ... try:\r
- ... return\r
- ... except:\r
- ... yield 1\r
- >>> print list(f1())\r
- []\r
-\r
- because, as in any function, return simply exits, but\r
-\r
- >>> def f2():\r
- ... try:\r
- ... raise StopIteration\r
- ... except:\r
- ... yield 42\r
- >>> print list(f2())\r
- [42]\r
-\r
- because StopIteration is captured by a bare "except", as is any\r
- exception.\r
-\r
-Specification: Generators and Exception Propagation\r
-\r
- >>> def f():\r
- ... return 1//0\r
- >>> def g():\r
- ... yield f() # the zero division exception propagates\r
- ... yield 42 # and we'll never get here\r
- >>> k = g()\r
- >>> k.next()\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- File "<stdin>", line 2, in g\r
- File "<stdin>", line 2, in f\r
- ZeroDivisionError: integer division or modulo by zero\r
- >>> k.next() # and the generator cannot be resumed\r
- Traceback (most recent call last):\r
- File "<stdin>", line 1, in ?\r
- StopIteration\r
- >>>\r
-\r
-Specification: Try/Except/Finally\r
-\r
- >>> def f():\r
- ... try:\r
- ... yield 1\r
- ... try:\r
- ... yield 2\r
- ... 1//0\r
- ... yield 3 # never get here\r
- ... except ZeroDivisionError:\r
- ... yield 4\r
- ... yield 5\r
- ... raise\r
- ... except:\r
- ... yield 6\r
- ... yield 7 # the "raise" above stops this\r
- ... except:\r
- ... yield 8\r
- ... yield 9\r
- ... try:\r
- ... x = 12\r
- ... finally:\r
- ... yield 10\r
- ... yield 11\r
- >>> print list(f())\r
- [1, 2, 4, 5, 8, 9, 10, 11]\r
- >>>\r
-\r
-Guido's binary tree example.\r
-\r
- >>> # A binary tree class.\r
- >>> class Tree:\r
- ...\r
- ... def __init__(self, label, left=None, right=None):\r
- ... self.label = label\r
- ... self.left = left\r
- ... self.right = right\r
- ...\r
- ... def __repr__(self, level=0, indent=" "):\r
- ... s = level*indent + repr(self.label)\r
- ... if self.left:\r
- ... s = s + "\\n" + self.left.__repr__(level+1, indent)\r
- ... if self.right:\r
- ... s = s + "\\n" + self.right.__repr__(level+1, indent)\r
- ... return s\r
- ...\r
- ... def __iter__(self):\r
- ... return inorder(self)\r
-\r
- >>> # Create a Tree from a list.\r
- >>> def tree(list):\r
- ... n = len(list)\r
- ... if n == 0:\r
- ... return []\r
- ... i = n // 2\r
- ... return Tree(list[i], tree(list[:i]), tree(list[i+1:]))\r
-\r
- >>> # Show it off: create a tree.\r
- >>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")\r
-\r
- >>> # A recursive generator that generates Tree labels in in-order.\r
- >>> def inorder(t):\r
- ... if t:\r
- ... for x in inorder(t.left):\r
- ... yield x\r
- ... yield t.label\r
- ... for x in inorder(t.right):\r
- ... yield x\r
-\r
- >>> # Show it off: create a tree.\r
- >>> t = tree("ABCDEFGHIJKLMNOPQRSTUVWXYZ")\r
- >>> # Print the nodes of the tree in in-order.\r
- >>> for x in t:\r
- ... print x,\r
- 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\r
-\r
- >>> # A non-recursive generator.\r
- >>> def inorder(node):\r
- ... stack = []\r
- ... while node:\r
- ... while node.left:\r
- ... stack.append(node)\r
- ... node = node.left\r
- ... yield node.label\r
- ... while not node.right:\r
- ... try:\r
- ... node = stack.pop()\r
- ... except IndexError:\r
- ... return\r
- ... yield node.label\r
- ... node = node.right\r
-\r
- >>> # Exercise the non-recursive generator.\r
- >>> for x in t:\r
- ... print x,\r
- 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\r
-\r
-"""\r
-\r
-# Examples from Iterator-List and Python-Dev and c.l.py.\r
-\r
-email_tests = """\r
-\r
-The difference between yielding None and returning it.\r
-\r
->>> def g():\r
-... for i in range(3):\r
-... yield None\r
-... yield None\r
-... return\r
->>> list(g())\r
-[None, None, None, None]\r
-\r
-Ensure that explicitly raising StopIteration acts like any other exception\r
-in try/except, not like a return.\r
-\r
->>> def g():\r
-... yield 1\r
-... try:\r
-... raise StopIteration\r
-... except:\r
-... yield 2\r
-... yield 3\r
->>> list(g())\r
-[1, 2, 3]\r
-\r
-Next one was posted to c.l.py.\r
-\r
->>> def gcomb(x, k):\r
-... "Generate all combinations of k elements from list x."\r
-...\r
-... if k > len(x):\r
-... return\r
-... if k == 0:\r
-... yield []\r
-... else:\r
-... first, rest = x[0], x[1:]\r
-... # A combination does or doesn't contain first.\r
-... # If it does, the remainder is a k-1 comb of rest.\r
-... for c in gcomb(rest, k-1):\r
-... c.insert(0, first)\r
-... yield c\r
-... # If it doesn't contain first, it's a k comb of rest.\r
-... for c in gcomb(rest, k):\r
-... yield c\r
-\r
->>> seq = range(1, 5)\r
->>> for k in range(len(seq) + 2):\r
-... print "%d-combs of %s:" % (k, seq)\r
-... for c in gcomb(seq, k):\r
-... print " ", c\r
-0-combs of [1, 2, 3, 4]:\r
- []\r
-1-combs of [1, 2, 3, 4]:\r
- [1]\r
- [2]\r
- [3]\r
- [4]\r
-2-combs of [1, 2, 3, 4]:\r
- [1, 2]\r
- [1, 3]\r
- [1, 4]\r
- [2, 3]\r
- [2, 4]\r
- [3, 4]\r
-3-combs of [1, 2, 3, 4]:\r
- [1, 2, 3]\r
- [1, 2, 4]\r
- [1, 3, 4]\r
- [2, 3, 4]\r
-4-combs of [1, 2, 3, 4]:\r
- [1, 2, 3, 4]\r
-5-combs of [1, 2, 3, 4]:\r
-\r
-From the Iterators list, about the types of these things.\r
-\r
->>> def g():\r
-... yield 1\r
-...\r
->>> type(g)\r
-<type 'function'>\r
->>> i = g()\r
->>> type(i)\r
-<type 'generator'>\r
->>> [s for s in dir(i) if not s.startswith('_')]\r
-['close', 'gi_code', 'gi_frame', 'gi_running', 'next', 'send', 'throw']\r
->>> print i.next.__doc__\r
-x.next() -> the next value, or raise StopIteration\r
->>> iter(i) is i\r
-True\r
->>> import types\r
->>> isinstance(i, types.GeneratorType)\r
-True\r
-\r
-And more, added later.\r
-\r
->>> i.gi_running\r
-0\r
->>> type(i.gi_frame)\r
-<type 'frame'>\r
->>> i.gi_running = 42\r
-Traceback (most recent call last):\r
- ...\r
-TypeError: readonly attribute\r
->>> def g():\r
-... yield me.gi_running\r
->>> me = g()\r
->>> me.gi_running\r
-0\r
->>> me.next()\r
-1\r
->>> me.gi_running\r
-0\r
-\r
-A clever union-find implementation from c.l.py, due to David Eppstein.\r
-Sent: Friday, June 29, 2001 12:16 PM\r
-To: python-list@python.org\r
-Subject: Re: PEP 255: Simple Generators\r
-\r
->>> class disjointSet:\r
-... def __init__(self, name):\r
-... self.name = name\r
-... self.parent = None\r
-... self.generator = self.generate()\r
-...\r
-... def generate(self):\r
-... while not self.parent:\r
-... yield self\r
-... for x in self.parent.generator:\r
-... yield x\r
-...\r
-... def find(self):\r
-... return self.generator.next()\r
-...\r
-... def union(self, parent):\r
-... if self.parent:\r
-... raise ValueError("Sorry, I'm not a root!")\r
-... self.parent = parent\r
-...\r
-... def __str__(self):\r
-... return self.name\r
-\r
->>> names = "ABCDEFGHIJKLM"\r
->>> sets = [disjointSet(name) for name in names]\r
->>> roots = sets[:]\r
-\r
->>> import random\r
->>> gen = random.WichmannHill(42)\r
->>> while 1:\r
-... for s in sets:\r
-... print "%s->%s" % (s, s.find()),\r
-... print\r
-... if len(roots) > 1:\r
-... s1 = gen.choice(roots)\r
-... roots.remove(s1)\r
-... s2 = gen.choice(roots)\r
-... s1.union(s2)\r
-... print "merged", s1, "into", s2\r
-... else:\r
-... break\r
-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\r
-merged D into G\r
-A->A B->B C->C D->G E->E F->F G->G H->H I->I J->J K->K L->L M->M\r
-merged C into F\r
-A->A B->B C->F D->G E->E F->F G->G H->H I->I J->J K->K L->L M->M\r
-merged L into A\r
-A->A B->B C->F D->G E->E F->F G->G H->H I->I J->J K->K L->A M->M\r
-merged H into E\r
-A->A B->B C->F D->G E->E F->F G->G H->E I->I J->J K->K L->A M->M\r
-merged B into E\r
-A->A B->E C->F D->G E->E F->F G->G H->E I->I J->J K->K L->A M->M\r
-merged J into G\r
-A->A B->E C->F D->G E->E F->F G->G H->E I->I J->G K->K L->A M->M\r
-merged E into G\r
-A->A B->G C->F D->G E->G F->F G->G H->G I->I J->G K->K L->A M->M\r
-merged M into G\r
-A->A B->G C->F D->G E->G F->F G->G H->G I->I J->G K->K L->A M->G\r
-merged I into K\r
-A->A B->G C->F D->G E->G F->F G->G H->G I->K J->G K->K L->A M->G\r
-merged K into A\r
-A->A B->G C->F D->G E->G F->F G->G H->G I->A J->G K->A L->A M->G\r
-merged F into A\r
-A->A B->G C->A D->G E->G F->A G->G H->G I->A J->G K->A L->A M->G\r
-merged A into G\r
-A->G B->G C->G D->G E->G F->G G->G H->G I->G J->G K->G L->G M->G\r
-\r
-"""\r
-# Emacs turd '\r
-\r
-# Fun tests (for sufficiently warped notions of "fun").\r
-\r
-fun_tests = """\r
-\r
-Build up to a recursive Sieve of Eratosthenes generator.\r
-\r
->>> def firstn(g, n):\r
-... return [g.next() for i in range(n)]\r
-\r
->>> def intsfrom(i):\r
-... while 1:\r
-... yield i\r
-... i += 1\r
-\r
->>> firstn(intsfrom(5), 7)\r
-[5, 6, 7, 8, 9, 10, 11]\r
-\r
->>> def exclude_multiples(n, ints):\r
-... for i in ints:\r
-... if i % n:\r
-... yield i\r
-\r
->>> firstn(exclude_multiples(3, intsfrom(1)), 6)\r
-[1, 2, 4, 5, 7, 8]\r
-\r
->>> def sieve(ints):\r
-... prime = ints.next()\r
-... yield prime\r
-... not_divisible_by_prime = exclude_multiples(prime, ints)\r
-... for p in sieve(not_divisible_by_prime):\r
-... yield p\r
-\r
->>> primes = sieve(intsfrom(2))\r
->>> firstn(primes, 20)\r
-[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]\r
-\r
-\r
-Another famous problem: generate all integers of the form\r
- 2**i * 3**j * 5**k\r
-in increasing order, where i,j,k >= 0. Trickier than it may look at first!\r
-Try writing it without generators, and correctly, and without generating\r
-3 internal results for each result output.\r
-\r
->>> def times(n, g):\r
-... for i in g:\r
-... yield n * i\r
->>> firstn(times(10, intsfrom(1)), 10)\r
-[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\r
-\r
->>> def merge(g, h):\r
-... ng = g.next()\r
-... nh = h.next()\r
-... while 1:\r
-... if ng < nh:\r
-... yield ng\r
-... ng = g.next()\r
-... elif ng > nh:\r
-... yield nh\r
-... nh = h.next()\r
-... else:\r
-... yield ng\r
-... ng = g.next()\r
-... nh = h.next()\r
-\r
-The following works, but is doing a whale of a lot of redundant work --\r
-it's not clear how to get the internal uses of m235 to share a single\r
-generator. Note that me_times2 (etc) each need to see every element in the\r
-result sequence. So this is an example where lazy lists are more natural\r
-(you can look at the head of a lazy list any number of times).\r
-\r
->>> def m235():\r
-... yield 1\r
-... me_times2 = times(2, m235())\r
-... me_times3 = times(3, m235())\r
-... me_times5 = times(5, m235())\r
-... for i in merge(merge(me_times2,\r
-... me_times3),\r
-... me_times5):\r
-... yield i\r
-\r
-Don't print "too many" of these -- the implementation above is extremely\r
-inefficient: each call of m235() leads to 3 recursive calls, and in\r
-turn each of those 3 more, and so on, and so on, until we've descended\r
-enough levels to satisfy the print stmts. Very odd: when I printed 5\r
-lines of results below, this managed to screw up Win98's malloc in "the\r
-usual" way, i.e. the heap grew over 4Mb so Win98 started fragmenting\r
-address space, and it *looked* like a very slow leak.\r
-\r
->>> result = m235()\r
->>> for i in range(3):\r
-... print firstn(result, 15)\r
-[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]\r
-[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]\r
-[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]\r
-\r
-Heh. Here's one way to get a shared list, complete with an excruciating\r
-namespace renaming trick. The *pretty* part is that the times() and merge()\r
-functions can be reused as-is, because they only assume their stream\r
-arguments are iterable -- a LazyList is the same as a generator to times().\r
-\r
->>> class LazyList:\r
-... def __init__(self, g):\r
-... self.sofar = []\r
-... self.fetch = g.next\r
-...\r
-... def __getitem__(self, i):\r
-... sofar, fetch = self.sofar, self.fetch\r
-... while i >= len(sofar):\r
-... sofar.append(fetch())\r
-... return sofar[i]\r
-\r
->>> def m235():\r
-... yield 1\r
-... # Gack: m235 below actually refers to a LazyList.\r
-... me_times2 = times(2, m235)\r
-... me_times3 = times(3, m235)\r
-... me_times5 = times(5, m235)\r
-... for i in merge(merge(me_times2,\r
-... me_times3),\r
-... me_times5):\r
-... yield i\r
-\r
-Print as many of these as you like -- *this* implementation is memory-\r
-efficient.\r
-\r
->>> m235 = LazyList(m235())\r
->>> for i in range(5):\r
-... print [m235[j] for j in range(15*i, 15*(i+1))]\r
-[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]\r
-[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]\r
-[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]\r
-[200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]\r
-[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]\r
-\r
-Ye olde Fibonacci generator, LazyList style.\r
-\r
->>> def fibgen(a, b):\r
-...\r
-... def sum(g, h):\r
-... while 1:\r
-... yield g.next() + h.next()\r
-...\r
-... def tail(g):\r
-... g.next() # throw first away\r
-... for x in g:\r
-... yield x\r
-...\r
-... yield a\r
-... yield b\r
-... for s in sum(iter(fib),\r
-... tail(iter(fib))):\r
-... yield s\r
-\r
->>> fib = LazyList(fibgen(1, 2))\r
->>> firstn(iter(fib), 17)\r
-[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]\r
-\r
-\r
-Running after your tail with itertools.tee (new in version 2.4)\r
-\r
-The algorithms "m235" (Hamming) and Fibonacci presented above are both\r
-examples of a whole family of FP (functional programming) algorithms\r
-where a function produces and returns a list while the production algorithm\r
-suppose the list as already produced by recursively calling itself.\r
-For these algorithms to work, they must:\r
-\r
-- produce at least a first element without presupposing the existence of\r
- the rest of the list\r
-- produce their elements in a lazy manner\r
-\r
-To work efficiently, the beginning of the list must not be recomputed over\r
-and over again. This is ensured in most FP languages as a built-in feature.\r
-In python, we have to explicitly maintain a list of already computed results\r
-and abandon genuine recursivity.\r
-\r
-This is what had been attempted above with the LazyList class. One problem\r
-with that class is that it keeps a list of all of the generated results and\r
-therefore continually grows. This partially defeats the goal of the generator\r
-concept, viz. produce the results only as needed instead of producing them\r
-all and thereby wasting memory.\r
-\r
-Thanks to itertools.tee, it is now clear "how to get the internal uses of\r
-m235 to share a single generator".\r
-\r
->>> from itertools import tee\r
->>> def m235():\r
-... def _m235():\r
-... yield 1\r
-... for n in merge(times(2, m2),\r
-... merge(times(3, m3),\r
-... times(5, m5))):\r
-... yield n\r
-... m1 = _m235()\r
-... m2, m3, m5, mRes = tee(m1, 4)\r
-... return mRes\r
-\r
->>> it = m235()\r
->>> for i in range(5):\r
-... print firstn(it, 15)\r
-[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]\r
-[25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80]\r
-[81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192]\r
-[200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360, 375, 384]\r
-[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]\r
-\r
-The "tee" function does just what we want. It internally keeps a generated\r
-result for as long as it has not been "consumed" from all of the duplicated\r
-iterators, whereupon it is deleted. You can therefore print the hamming\r
-sequence during hours without increasing memory usage, or very little.\r
-\r
-The beauty of it is that recursive running-after-their-tail FP algorithms\r
-are quite straightforwardly expressed with this Python idiom.\r
-\r
-Ye olde Fibonacci generator, tee style.\r
-\r
->>> def fib():\r
-...\r
-... def _isum(g, h):\r
-... while 1:\r
-... yield g.next() + h.next()\r
-...\r
-... def _fib():\r
-... yield 1\r
-... yield 2\r
-... fibTail.next() # throw first away\r
-... for res in _isum(fibHead, fibTail):\r
-... yield res\r
-...\r
-... realfib = _fib()\r
-... fibHead, fibTail, fibRes = tee(realfib, 3)\r
-... return fibRes\r
-\r
->>> firstn(fib(), 17)\r
-[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]\r
-\r
-"""\r
-\r
-# syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0\r
-# hackery.\r
-\r
-syntax_tests = """\r
-\r
->>> def f():\r
-... return 22\r
-... yield 1\r
-Traceback (most recent call last):\r
- ..\r
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[0]>, line 3)\r
-\r
->>> def f():\r
-... yield 1\r
-... return 22\r
-Traceback (most recent call last):\r
- ..\r
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[1]>, line 3)\r
-\r
-"return None" is not the same as "return" in a generator:\r
-\r
->>> def f():\r
-... yield 1\r
-... return None\r
-Traceback (most recent call last):\r
- ..\r
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[2]>, line 3)\r
-\r
-These are fine:\r
-\r
->>> def f():\r
-... yield 1\r
-... return\r
-\r
->>> def f():\r
-... try:\r
-... yield 1\r
-... finally:\r
-... pass\r
-\r
->>> def f():\r
-... try:\r
-... try:\r
-... 1//0\r
-... except ZeroDivisionError:\r
-... yield 666\r
-... except:\r
-... pass\r
-... finally:\r
-... pass\r
-\r
->>> def f():\r
-... try:\r
-... try:\r
-... yield 12\r
-... 1//0\r
-... except ZeroDivisionError:\r
-... yield 666\r
-... except:\r
-... try:\r
-... x = 12\r
-... finally:\r
-... yield 12\r
-... except:\r
-... return\r
->>> list(f())\r
-[12, 666]\r
-\r
->>> def f():\r
-... yield\r
->>> type(f())\r
-<type 'generator'>\r
-\r
-\r
->>> def f():\r
-... if 0:\r
-... yield\r
->>> type(f())\r
-<type 'generator'>\r
-\r
-\r
->>> def f():\r
-... if 0:\r
-... yield 1\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f():\r
-... if "":\r
-... yield None\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f():\r
-... return\r
-... try:\r
-... if x==4:\r
-... pass\r
-... elif 0:\r
-... try:\r
-... 1//0\r
-... except SyntaxError:\r
-... pass\r
-... else:\r
-... if 0:\r
-... while 12:\r
-... x += 1\r
-... yield 2 # don't blink\r
-... f(a, b, c, d, e)\r
-... else:\r
-... pass\r
-... except:\r
-... x = 1\r
-... return\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f():\r
-... if 0:\r
-... def g():\r
-... yield 1\r
-...\r
->>> type(f())\r
-<type 'NoneType'>\r
-\r
->>> def f():\r
-... if 0:\r
-... class C:\r
-... def __init__(self):\r
-... yield 1\r
-... def f(self):\r
-... yield 2\r
->>> type(f())\r
-<type 'NoneType'>\r
-\r
->>> def f():\r
-... if 0:\r
-... return\r
-... if 0:\r
-... yield 2\r
->>> type(f())\r
-<type 'generator'>\r
-\r
-\r
->>> def f():\r
-... if 0:\r
-... lambda x: x # shouldn't trigger here\r
-... return # or here\r
-... def f(i):\r
-... return 2*i # or here\r
-... if 0:\r
-... return 3 # but *this* sucks (line 8)\r
-... if 0:\r
-... yield 2 # because it's a generator (line 10)\r
-Traceback (most recent call last):\r
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.syntax[24]>, line 10)\r
-\r
-This one caused a crash (see SF bug 567538):\r
-\r
->>> def f():\r
-... for i in range(3):\r
-... try:\r
-... continue\r
-... finally:\r
-... yield i\r
-...\r
->>> g = f()\r
->>> print g.next()\r
-0\r
->>> print g.next()\r
-1\r
->>> print g.next()\r
-2\r
->>> print g.next()\r
-Traceback (most recent call last):\r
-StopIteration\r
-\r
-\r
-Test the gi_code attribute\r
-\r
->>> def f():\r
-... yield 5\r
-...\r
->>> g = f()\r
->>> g.gi_code is f.func_code\r
-True\r
->>> g.next()\r
-5\r
->>> g.next()\r
-Traceback (most recent call last):\r
-StopIteration\r
->>> g.gi_code is f.func_code\r
-True\r
-\r
-\r
-Test the __name__ attribute and the repr()\r
-\r
->>> def f():\r
-... yield 5\r
-...\r
->>> g = f()\r
->>> g.__name__\r
-'f'\r
->>> repr(g) # doctest: +ELLIPSIS\r
-'<generator object f at ...>'\r
-\r
-Lambdas shouldn't have their usual return behavior.\r
-\r
->>> x = lambda: (yield 1)\r
->>> list(x())\r
-[1]\r
-\r
->>> x = lambda: ((yield 1), (yield 2))\r
->>> list(x())\r
-[1, 2]\r
-"""\r
-\r
-# conjoin is a simple backtracking generator, named in honor of Icon's\r
-# "conjunction" control structure. Pass a list of no-argument functions\r
-# that return iterable objects. Easiest to explain by example: assume the\r
-# function list [x, y, z] is passed. Then conjoin acts like:\r
-#\r
-# def g():\r
-# values = [None] * 3\r
-# for values[0] in x():\r
-# for values[1] in y():\r
-# for values[2] in z():\r
-# yield values\r
-#\r
-# So some 3-lists of values *may* be generated, each time we successfully\r
-# get into the innermost loop. If an iterator fails (is exhausted) before\r
-# then, it "backtracks" to get the next value from the nearest enclosing\r
-# iterator (the one "to the left"), and starts all over again at the next\r
-# slot (pumps a fresh iterator). Of course this is most useful when the\r
-# iterators have side-effects, so that which values *can* be generated at\r
-# each slot depend on the values iterated at previous slots.\r
-\r
-def simple_conjoin(gs):\r
-\r
- values = [None] * len(gs)\r
-\r
- def gen(i):\r
- if i >= len(gs):\r
- yield values\r
- else:\r
- for values[i] in gs[i]():\r
- for x in gen(i+1):\r
- yield x\r
-\r
- for x in gen(0):\r
- yield x\r
-\r
-# That works fine, but recursing a level and checking i against len(gs) for\r
-# each item produced is inefficient. By doing manual loop unrolling across\r
-# generator boundaries, it's possible to eliminate most of that overhead.\r
-# This isn't worth the bother *in general* for generators, but conjoin() is\r
-# a core building block for some CPU-intensive generator applications.\r
-\r
-def conjoin(gs):\r
-\r
- n = len(gs)\r
- values = [None] * n\r
-\r
- # Do one loop nest at time recursively, until the # of loop nests\r
- # remaining is divisible by 3.\r
-\r
- def gen(i):\r
- if i >= n:\r
- yield values\r
-\r
- elif (n-i) % 3:\r
- ip1 = i+1\r
- for values[i] in gs[i]():\r
- for x in gen(ip1):\r
- yield x\r
-\r
- else:\r
- for x in _gen3(i):\r
- yield x\r
-\r
- # Do three loop nests at a time, recursing only if at least three more\r
- # remain. Don't call directly: this is an internal optimization for\r
- # gen's use.\r
-\r
- def _gen3(i):\r
- assert i < n and (n-i) % 3 == 0\r
- ip1, ip2, ip3 = i+1, i+2, i+3\r
- g, g1, g2 = gs[i : ip3]\r
-\r
- if ip3 >= n:\r
- # These are the last three, so we can yield values directly.\r
- for values[i] in g():\r
- for values[ip1] in g1():\r
- for values[ip2] in g2():\r
- yield values\r
-\r
- else:\r
- # At least 6 loop nests remain; peel off 3 and recurse for the\r
- # rest.\r
- for values[i] in g():\r
- for values[ip1] in g1():\r
- for values[ip2] in g2():\r
- for x in _gen3(ip3):\r
- yield x\r
-\r
- for x in gen(0):\r
- yield x\r
-\r
-# And one more approach: For backtracking apps like the Knight's Tour\r
-# solver below, the number of backtracking levels can be enormous (one\r
-# level per square, for the Knight's Tour, so that e.g. a 100x100 board\r
-# needs 10,000 levels). In such cases Python is likely to run out of\r
-# stack space due to recursion. So here's a recursion-free version of\r
-# conjoin too.\r
-# NOTE WELL: This allows large problems to be solved with only trivial\r
-# demands on stack space. Without explicitly resumable generators, this is\r
-# much harder to achieve. OTOH, this is much slower (up to a factor of 2)\r
-# than the fancy unrolled recursive conjoin.\r
-\r
-def flat_conjoin(gs): # rename to conjoin to run tests with this instead\r
- n = len(gs)\r
- values = [None] * n\r
- iters = [None] * n\r
- _StopIteration = StopIteration # make local because caught a *lot*\r
- i = 0\r
- while 1:\r
- # Descend.\r
- try:\r
- while i < n:\r
- it = iters[i] = gs[i]().next\r
- values[i] = it()\r
- i += 1\r
- except _StopIteration:\r
- pass\r
- else:\r
- assert i == n\r
- yield values\r
-\r
- # Backtrack until an older iterator can be resumed.\r
- i -= 1\r
- while i >= 0:\r
- try:\r
- values[i] = iters[i]()\r
- # Success! Start fresh at next level.\r
- i += 1\r
- break\r
- except _StopIteration:\r
- # Continue backtracking.\r
- i -= 1\r
- else:\r
- assert i < 0\r
- break\r
-\r
-# A conjoin-based N-Queens solver.\r
-\r
-class Queens:\r
- def __init__(self, n):\r
- self.n = n\r
- rangen = range(n)\r
-\r
- # Assign a unique int to each column and diagonal.\r
- # columns: n of those, range(n).\r
- # NW-SE diagonals: 2n-1 of these, i-j unique and invariant along\r
- # each, smallest i-j is 0-(n-1) = 1-n, so add n-1 to shift to 0-\r
- # based.\r
- # NE-SW diagonals: 2n-1 of these, i+j unique and invariant along\r
- # each, smallest i+j is 0, largest is 2n-2.\r
-\r
- # For each square, compute a bit vector of the columns and\r
- # diagonals it covers, and for each row compute a function that\r
- # generates the possiblities for the columns in that row.\r
- self.rowgenerators = []\r
- for i in rangen:\r
- rowuses = [(1L << j) | # column ordinal\r
- (1L << (n + i-j + n-1)) | # NW-SE ordinal\r
- (1L << (n + 2*n-1 + i+j)) # NE-SW ordinal\r
- for j in rangen]\r
-\r
- def rowgen(rowuses=rowuses):\r
- for j in rangen:\r
- uses = rowuses[j]\r
- if uses & self.used == 0:\r
- self.used |= uses\r
- yield j\r
- self.used &= ~uses\r
-\r
- self.rowgenerators.append(rowgen)\r
-\r
- # Generate solutions.\r
- def solve(self):\r
- self.used = 0\r
- for row2col in conjoin(self.rowgenerators):\r
- yield row2col\r
-\r
- def printsolution(self, row2col):\r
- n = self.n\r
- assert n == len(row2col)\r
- sep = "+" + "-+" * n\r
- print sep\r
- for i in range(n):\r
- squares = [" " for j in range(n)]\r
- squares[row2col[i]] = "Q"\r
- print "|" + "|".join(squares) + "|"\r
- print sep\r
-\r
-# A conjoin-based Knight's Tour solver. This is pretty sophisticated\r
-# (e.g., when used with flat_conjoin above, and passing hard=1 to the\r
-# constructor, a 200x200 Knight's Tour was found quickly -- note that we're\r
-# creating 10s of thousands of generators then!), and is lengthy.\r
-\r
-class Knights:\r
- def __init__(self, m, n, hard=0):\r
- self.m, self.n = m, n\r
-\r
- # solve() will set up succs[i] to be a list of square #i's\r
- # successors.\r
- succs = self.succs = []\r
-\r
- # Remove i0 from each of its successor's successor lists, i.e.\r
- # successors can't go back to i0 again. Return 0 if we can\r
- # detect this makes a solution impossible, else return 1.\r
-\r
- def remove_from_successors(i0, len=len):\r
- # If we remove all exits from a free square, we're dead:\r
- # even if we move to it next, we can't leave it again.\r
- # If we create a square with one exit, we must visit it next;\r
- # else somebody else will have to visit it, and since there's\r
- # only one adjacent, there won't be a way to leave it again.\r
- # Finelly, if we create more than one free square with a\r
- # single exit, we can only move to one of them next, leaving\r
- # the other one a dead end.\r
- ne0 = ne1 = 0\r
- for i in succs[i0]:\r
- s = succs[i]\r
- s.remove(i0)\r
- e = len(s)\r
- if e == 0:\r
- ne0 += 1\r
- elif e == 1:\r
- ne1 += 1\r
- return ne0 == 0 and ne1 < 2\r
-\r
- # Put i0 back in each of its successor's successor lists.\r
-\r
- def add_to_successors(i0):\r
- for i in succs[i0]:\r
- succs[i].append(i0)\r
-\r
- # Generate the first move.\r
- def first():\r
- if m < 1 or n < 1:\r
- return\r
-\r
- # Since we're looking for a cycle, it doesn't matter where we\r
- # start. Starting in a corner makes the 2nd move easy.\r
- corner = self.coords2index(0, 0)\r
- remove_from_successors(corner)\r
- self.lastij = corner\r
- yield corner\r
- add_to_successors(corner)\r
-\r
- # Generate the second moves.\r
- def second():\r
- corner = self.coords2index(0, 0)\r
- assert self.lastij == corner # i.e., we started in the corner\r
- if m < 3 or n < 3:\r
- return\r
- assert len(succs[corner]) == 2\r
- assert self.coords2index(1, 2) in succs[corner]\r
- assert self.coords2index(2, 1) in succs[corner]\r
- # Only two choices. Whichever we pick, the other must be the\r
- # square picked on move m*n, as it's the only way to get back\r
- # to (0, 0). Save its index in self.final so that moves before\r
- # the last know it must be kept free.\r
- for i, j in (1, 2), (2, 1):\r
- this = self.coords2index(i, j)\r
- final = self.coords2index(3-i, 3-j)\r
- self.final = final\r
-\r
- remove_from_successors(this)\r
- succs[final].append(corner)\r
- self.lastij = this\r
- yield this\r
- succs[final].remove(corner)\r
- add_to_successors(this)\r
-\r
- # Generate moves 3 thru m*n-1.\r
- def advance(len=len):\r
- # If some successor has only one exit, must take it.\r
- # Else favor successors with fewer exits.\r
- candidates = []\r
- for i in succs[self.lastij]:\r
- e = len(succs[i])\r
- assert e > 0, "else remove_from_successors() pruning flawed"\r
- if e == 1:\r
- candidates = [(e, i)]\r
- break\r
- candidates.append((e, i))\r
- else:\r
- candidates.sort()\r
-\r
- for e, i in candidates:\r
- if i != self.final:\r
- if remove_from_successors(i):\r
- self.lastij = i\r
- yield i\r
- add_to_successors(i)\r
-\r
- # Generate moves 3 thru m*n-1. Alternative version using a\r
- # stronger (but more expensive) heuristic to order successors.\r
- # Since the # of backtracking levels is m*n, a poor move early on\r
- # can take eons to undo. Smallest square board for which this\r
- # matters a lot is 52x52.\r
- def advance_hard(vmid=(m-1)/2.0, hmid=(n-1)/2.0, len=len):\r
- # If some successor has only one exit, must take it.\r
- # Else favor successors with fewer exits.\r
- # Break ties via max distance from board centerpoint (favor\r
- # corners and edges whenever possible).\r
- candidates = []\r
- for i in succs[self.lastij]:\r
- e = len(succs[i])\r
- assert e > 0, "else remove_from_successors() pruning flawed"\r
- if e == 1:\r
- candidates = [(e, 0, i)]\r
- break\r
- i1, j1 = self.index2coords(i)\r
- d = (i1 - vmid)**2 + (j1 - hmid)**2\r
- candidates.append((e, -d, i))\r
- else:\r
- candidates.sort()\r
-\r
- for e, d, i in candidates:\r
- if i != self.final:\r
- if remove_from_successors(i):\r
- self.lastij = i\r
- yield i\r
- add_to_successors(i)\r
-\r
- # Generate the last move.\r
- def last():\r
- assert self.final in succs[self.lastij]\r
- yield self.final\r
-\r
- if m*n < 4:\r
- self.squaregenerators = [first]\r
- else:\r
- self.squaregenerators = [first, second] + \\r
- [hard and advance_hard or advance] * (m*n - 3) + \\r
- [last]\r
-\r
- def coords2index(self, i, j):\r
- assert 0 <= i < self.m\r
- assert 0 <= j < self.n\r
- return i * self.n + j\r
-\r
- def index2coords(self, index):\r
- assert 0 <= index < self.m * self.n\r
- return divmod(index, self.n)\r
-\r
- def _init_board(self):\r
- succs = self.succs\r
- del succs[:]\r
- m, n = self.m, self.n\r
- c2i = self.coords2index\r
-\r
- offsets = [( 1, 2), ( 2, 1), ( 2, -1), ( 1, -2),\r
- (-1, -2), (-2, -1), (-2, 1), (-1, 2)]\r
- rangen = range(n)\r
- for i in range(m):\r
- for j in rangen:\r
- s = [c2i(i+io, j+jo) for io, jo in offsets\r
- if 0 <= i+io < m and\r
- 0 <= j+jo < n]\r
- succs.append(s)\r
-\r
- # Generate solutions.\r
- def solve(self):\r
- self._init_board()\r
- for x in conjoin(self.squaregenerators):\r
- yield x\r
-\r
- def printsolution(self, x):\r
- m, n = self.m, self.n\r
- assert len(x) == m*n\r
- w = len(str(m*n))\r
- format = "%" + str(w) + "d"\r
-\r
- squares = [[None] * n for i in range(m)]\r
- k = 1\r
- for i in x:\r
- i1, j1 = self.index2coords(i)\r
- squares[i1][j1] = format % k\r
- k += 1\r
-\r
- sep = "+" + ("-" * w + "+") * n\r
- print sep\r
- for i in range(m):\r
- row = squares[i]\r
- print "|" + "|".join(row) + "|"\r
- print sep\r
-\r
-conjoin_tests = """\r
-\r
-Generate the 3-bit binary numbers in order. This illustrates dumbest-\r
-possible use of conjoin, just to generate the full cross-product.\r
-\r
->>> for c in conjoin([lambda: iter((0, 1))] * 3):\r
-... print c\r
-[0, 0, 0]\r
-[0, 0, 1]\r
-[0, 1, 0]\r
-[0, 1, 1]\r
-[1, 0, 0]\r
-[1, 0, 1]\r
-[1, 1, 0]\r
-[1, 1, 1]\r
-\r
-For efficiency in typical backtracking apps, conjoin() yields the same list\r
-object each time. So if you want to save away a full account of its\r
-generated sequence, you need to copy its results.\r
-\r
->>> def gencopy(iterator):\r
-... for x in iterator:\r
-... yield x[:]\r
-\r
->>> for n in range(10):\r
-... all = list(gencopy(conjoin([lambda: iter((0, 1))] * n)))\r
-... print n, len(all), all[0] == [0] * n, all[-1] == [1] * n\r
-0 1 True True\r
-1 2 True True\r
-2 4 True True\r
-3 8 True True\r
-4 16 True True\r
-5 32 True True\r
-6 64 True True\r
-7 128 True True\r
-8 256 True True\r
-9 512 True True\r
-\r
-And run an 8-queens solver.\r
-\r
->>> q = Queens(8)\r
->>> LIMIT = 2\r
->>> count = 0\r
->>> for row2col in q.solve():\r
-... count += 1\r
-... if count <= LIMIT:\r
-... print "Solution", count\r
-... q.printsolution(row2col)\r
-Solution 1\r
-+-+-+-+-+-+-+-+-+\r
-|Q| | | | | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | |Q| | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | | | | |Q|\r
-+-+-+-+-+-+-+-+-+\r
-| | | | | |Q| | |\r
-+-+-+-+-+-+-+-+-+\r
-| | |Q| | | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | | | |Q| |\r
-+-+-+-+-+-+-+-+-+\r
-| |Q| | | | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | |Q| | | | |\r
-+-+-+-+-+-+-+-+-+\r
-Solution 2\r
-+-+-+-+-+-+-+-+-+\r
-|Q| | | | | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | | |Q| | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | | | | |Q|\r
-+-+-+-+-+-+-+-+-+\r
-| | |Q| | | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | | | |Q| |\r
-+-+-+-+-+-+-+-+-+\r
-| | | |Q| | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| |Q| | | | | | |\r
-+-+-+-+-+-+-+-+-+\r
-| | | | |Q| | | |\r
-+-+-+-+-+-+-+-+-+\r
-\r
->>> print count, "solutions in all."\r
-92 solutions in all.\r
-\r
-And run a Knight's Tour on a 10x10 board. Note that there are about\r
-20,000 solutions even on a 6x6 board, so don't dare run this to exhaustion.\r
-\r
->>> k = Knights(10, 10)\r
->>> LIMIT = 2\r
->>> count = 0\r
->>> for x in k.solve():\r
-... count += 1\r
-... if count <= LIMIT:\r
-... print "Solution", count\r
-... k.printsolution(x)\r
-... else:\r
-... break\r
-Solution 1\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 59|100| 73| 36| 41| 56| 39| 32| 9| 6|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 87| 98| 91| 80| 77| 84| 53| 46| 65| 44|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 90| 23| 88| 95| 70| 79| 68| 83| 14| 17|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 97| 92| 21| 78| 81| 94| 19| 16| 45| 66|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 22| 89| 96| 93| 20| 69| 82| 67| 18| 15|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-Solution 2\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 1| 58| 27| 34| 3| 40| 29| 10| 5| 8|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 26| 35| 2| 57| 28| 33| 4| 7| 30| 11|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 59|100| 73| 36| 41| 56| 39| 32| 9| 6|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 74| 25| 60| 55| 72| 37| 42| 49| 12| 31|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 61| 86| 99| 76| 63| 52| 47| 38| 43| 50|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 24| 75| 62| 85| 54| 71| 64| 51| 48| 13|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 87| 98| 89| 80| 77| 84| 53| 46| 65| 44|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 90| 23| 92| 95| 70| 79| 68| 83| 14| 17|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 97| 88| 21| 78| 81| 94| 19| 16| 45| 66|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-| 22| 91| 96| 93| 20| 69| 82| 67| 18| 15|\r
-+---+---+---+---+---+---+---+---+---+---+\r
-"""\r
-\r
-weakref_tests = """\\r
-Generators are weakly referencable:\r
-\r
->>> import weakref\r
->>> def gen():\r
-... yield 'foo!'\r
-...\r
->>> wr = weakref.ref(gen)\r
->>> wr() is gen\r
-True\r
->>> p = weakref.proxy(gen)\r
-\r
-Generator-iterators are weakly referencable as well:\r
-\r
->>> gi = gen()\r
->>> wr = weakref.ref(gi)\r
->>> wr() is gi\r
-True\r
->>> p = weakref.proxy(gi)\r
->>> list(p)\r
-['foo!']\r
-\r
-"""\r
-\r
-coroutine_tests = """\\r
-Sending a value into a started generator:\r
-\r
->>> def f():\r
-... print (yield 1)\r
-... yield 2\r
->>> g = f()\r
->>> g.next()\r
-1\r
->>> g.send(42)\r
-42\r
-2\r
-\r
-Sending a value into a new generator produces a TypeError:\r
-\r
->>> f().send("foo")\r
-Traceback (most recent call last):\r
-...\r
-TypeError: can't send non-None value to a just-started generator\r
-\r
-\r
-Yield by itself yields None:\r
-\r
->>> def f(): yield\r
->>> list(f())\r
-[None]\r
-\r
-\r
-\r
-An obscene abuse of a yield expression within a generator expression:\r
-\r
->>> list((yield 21) for i in range(4))\r
-[21, None, 21, None, 21, None, 21, None]\r
-\r
-And a more sane, but still weird usage:\r
-\r
->>> def f(): list(i for i in [(yield 26)])\r
->>> type(f())\r
-<type 'generator'>\r
-\r
-\r
-A yield expression with augmented assignment.\r
-\r
->>> def coroutine(seq):\r
-... count = 0\r
-... while count < 200:\r
-... count += yield\r
-... seq.append(count)\r
->>> seq = []\r
->>> c = coroutine(seq)\r
->>> c.next()\r
->>> print seq\r
-[]\r
->>> c.send(10)\r
->>> print seq\r
-[10]\r
->>> c.send(10)\r
->>> print seq\r
-[10, 20]\r
->>> c.send(10)\r
->>> print seq\r
-[10, 20, 30]\r
-\r
-\r
-Check some syntax errors for yield expressions:\r
-\r
->>> f=lambda: (yield 1),(yield 2)\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_generators.__test__.coroutine[21]>", line 1\r
-SyntaxError: 'yield' outside function\r
-\r
->>> def f(): return lambda x=(yield): 1\r
-Traceback (most recent call last):\r
- ...\r
-SyntaxError: 'return' with argument inside generator (<doctest test.test_generators.__test__.coroutine[22]>, line 1)\r
-\r
->>> def f(): x = yield = y\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_generators.__test__.coroutine[23]>", line 1\r
-SyntaxError: assignment to yield expression not possible\r
-\r
->>> def f(): (yield bar) = y\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_generators.__test__.coroutine[24]>", line 1\r
-SyntaxError: can't assign to yield expression\r
-\r
->>> def f(): (yield bar) += y\r
-Traceback (most recent call last):\r
- ...\r
- File "<doctest test.test_generators.__test__.coroutine[25]>", line 1\r
-SyntaxError: can't assign to yield expression\r
-\r
-\r
-Now check some throw() conditions:\r
-\r
->>> def f():\r
-... while True:\r
-... try:\r
-... print (yield)\r
-... except ValueError,v:\r
-... print "caught ValueError (%s)" % (v),\r
->>> import sys\r
->>> g = f()\r
->>> g.next()\r
-\r
->>> g.throw(ValueError) # type only\r
-caught ValueError ()\r
-\r
->>> g.throw(ValueError("xyz")) # value only\r
-caught ValueError (xyz)\r
-\r
->>> g.throw(ValueError, ValueError(1)) # value+matching type\r
-caught ValueError (1)\r
-\r
->>> g.throw(ValueError, TypeError(1)) # mismatched type, rewrapped\r
-caught ValueError (1)\r
-\r
->>> g.throw(ValueError, ValueError(1), None) # explicit None traceback\r
-caught ValueError (1)\r
-\r
->>> g.throw(ValueError(1), "foo") # bad args\r
-Traceback (most recent call last):\r
- ...\r
-TypeError: instance exception may not have a separate value\r
-\r
->>> g.throw(ValueError, "foo", 23) # bad args\r
-Traceback (most recent call last):\r
- ...\r
-TypeError: throw() third argument must be a traceback object\r
-\r
->>> def throw(g,exc):\r
-... try:\r
-... raise exc\r
-... except:\r
-... g.throw(*sys.exc_info())\r
->>> throw(g,ValueError) # do it with traceback included\r
-caught ValueError ()\r
-\r
->>> g.send(1)\r
-1\r
-\r
->>> throw(g,TypeError) # terminate the generator\r
-Traceback (most recent call last):\r
- ...\r
-TypeError\r
-\r
->>> print g.gi_frame\r
-None\r
-\r
->>> g.send(2)\r
-Traceback (most recent call last):\r
- ...\r
-StopIteration\r
-\r
->>> g.throw(ValueError,6) # throw on closed generator\r
-Traceback (most recent call last):\r
- ...\r
-ValueError: 6\r
-\r
->>> f().throw(ValueError,7) # throw on just-opened generator\r
-Traceback (most recent call last):\r
- ...\r
-ValueError: 7\r
-\r
->>> f().throw("abc") # throw on just-opened generator\r
-Traceback (most recent call last):\r
- ...\r
-TypeError: exceptions must be classes, or instances, not str\r
-\r
-Now let's try closing a generator:\r
-\r
->>> def f():\r
-... try: yield\r
-... except GeneratorExit:\r
-... print "exiting"\r
-\r
->>> g = f()\r
->>> g.next()\r
->>> g.close()\r
-exiting\r
->>> g.close() # should be no-op now\r
-\r
->>> f().close() # close on just-opened generator should be fine\r
-\r
->>> def f(): yield # an even simpler generator\r
->>> f().close() # close before opening\r
->>> g = f()\r
->>> g.next()\r
->>> g.close() # close normally\r
-\r
-And finalization:\r
-\r
->>> def f():\r
-... try: yield\r
-... finally:\r
-... print "exiting"\r
-\r
->>> g = f()\r
->>> g.next()\r
->>> del g\r
-exiting\r
-\r
->>> class context(object):\r
-... def __enter__(self): pass\r
-... def __exit__(self, *args): print 'exiting'\r
->>> def f():\r
-... with context():\r
-... yield\r
->>> g = f()\r
->>> g.next()\r
->>> del g\r
-exiting\r
-\r
-\r
-GeneratorExit is not caught by except Exception:\r
-\r
->>> def f():\r
-... try: yield\r
-... except Exception: print 'except'\r
-... finally: print 'finally'\r
-\r
->>> g = f()\r
->>> g.next()\r
->>> del g\r
-finally\r
-\r
-\r
-Now let's try some ill-behaved generators:\r
-\r
->>> def f():\r
-... try: yield\r
-... except GeneratorExit:\r
-... yield "foo!"\r
->>> g = f()\r
->>> g.next()\r
->>> g.close()\r
-Traceback (most recent call last):\r
- ...\r
-RuntimeError: generator ignored GeneratorExit\r
->>> g.close()\r
-\r
-\r
-Our ill-behaved code should be invoked during GC:\r
-\r
->>> import sys, StringIO\r
->>> old, sys.stderr = sys.stderr, StringIO.StringIO()\r
->>> g = f()\r
->>> g.next()\r
->>> del g\r
->>> sys.stderr.getvalue().startswith(\r
-... "Exception RuntimeError: 'generator ignored GeneratorExit' in "\r
-... )\r
-True\r
->>> sys.stderr = old\r
-\r
-\r
-And errors thrown during closing should propagate:\r
-\r
->>> def f():\r
-... try: yield\r
-... except GeneratorExit:\r
-... raise TypeError("fie!")\r
->>> g = f()\r
->>> g.next()\r
->>> g.close()\r
-Traceback (most recent call last):\r
- ...\r
-TypeError: fie!\r
-\r
-\r
-Ensure that various yield expression constructs make their\r
-enclosing function a generator:\r
-\r
->>> def f(): x += yield\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f(): x = yield\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f(): lambda x=(yield): 1\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f(): x=(i for i in (yield) if (yield))\r
->>> type(f())\r
-<type 'generator'>\r
-\r
->>> def f(d): d[(yield "a")] = d[(yield "b")] = 27\r
->>> data = [1,2]\r
->>> g = f(data)\r
->>> type(g)\r
-<type 'generator'>\r
->>> g.send(None)\r
-'a'\r
->>> data\r
-[1, 2]\r
->>> g.send(0)\r
-'b'\r
->>> data\r
-[27, 2]\r
->>> try: g.send(1)\r
-... except StopIteration: pass\r
->>> data\r
-[27, 27]\r
-\r
-"""\r
-\r
-refleaks_tests = """\r
-Prior to adding cycle-GC support to itertools.tee, this code would leak\r
-references. We add it to the standard suite so the routine refleak-tests\r
-would trigger if it starts being uncleanable again.\r
-\r
->>> import itertools\r
->>> def leak():\r
-... class gen:\r
-... def __iter__(self):\r
-... return self\r
-... def next(self):\r
-... return self.item\r
-... g = gen()\r
-... head, tail = itertools.tee(g)\r
-... g.item = head\r
-... return head\r
->>> it = leak()\r
-\r
-Make sure to also test the involvement of the tee-internal teedataobject,\r
-which stores returned items.\r
-\r
->>> item = it.next()\r
-\r
-\r
-\r
-This test leaked at one point due to generator finalization/destruction.\r
-It was copied from Lib/test/leakers/test_generator_cycle.py before the file\r
-was removed.\r
-\r
->>> def leak():\r
-... def gen():\r
-... while True:\r
-... yield g\r
-... g = gen()\r
-\r
->>> leak()\r
-\r
-\r
-\r
-This test isn't really generator related, but rather exception-in-cleanup\r
-related. The coroutine tests (above) just happen to cause an exception in\r
-the generator's __del__ (tp_del) method. We can also test for this\r
-explicitly, without generators. We do have to redirect stderr to avoid\r
-printing warnings and to doublecheck that we actually tested what we wanted\r
-to test.\r
-\r
->>> import sys, StringIO\r
->>> old = sys.stderr\r
->>> try:\r
-... sys.stderr = StringIO.StringIO()\r
-... class Leaker:\r
-... def __del__(self):\r
-... raise RuntimeError\r
-...\r
-... l = Leaker()\r
-... del l\r
-... err = sys.stderr.getvalue().strip()\r
-... err.startswith(\r
-... "Exception RuntimeError: RuntimeError() in <"\r
-... )\r
-... err.endswith("> ignored")\r
-... len(err.splitlines())\r
-... finally:\r
-... sys.stderr = old\r
-True\r
-True\r
-1\r
-\r
-\r
-\r
-These refleak tests should perhaps be in a testfile of their own,\r
-test_generators just happened to be the test that drew these out.\r
-\r
-"""\r
-\r
-__test__ = {"tut": tutorial_tests,\r
- "pep": pep_tests,\r
- "email": email_tests,\r
- "fun": fun_tests,\r
- "syntax": syntax_tests,\r
- "conjoin": conjoin_tests,\r
- "weakref": weakref_tests,\r
- "coroutine": coroutine_tests,\r
- "refleaks": refleaks_tests,\r
- }\r
-\r
-# Magic test name that regrtest.py invokes *after* importing this module.\r
-# This worms around a bootstrap problem.\r
-# Note that doctest and regrtest both look in sys.argv for a "-v" argument,\r
-# so this works as expected in both ways of running regrtest.\r
-def test_main(verbose=None):\r
- from test import test_support, test_generators\r
- test_support.run_doctest(test_generators, verbose)\r
-\r
-# This part isn't needed for regrtest, but for running the test directly.\r
-if __name__ == "__main__":\r
- test_main(1)\r