Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4917)

Delta Between Two Patch Sets: Lib/test/test_generators.py

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 10 months ago
Right Patch Set: Created 7 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 tutorial_tests = """ 1 tutorial_tests = """
2 Let's try a simple generator: 2 Let's try a simple generator:
3 3
4 >>> def f(): 4 >>> def f():
5 ... yield 1 5 ... yield 1
6 ... yield 2 6 ... yield 2
7 7
8 >>> for i in f(): 8 >>> for i in f():
9 ... print(i) 9 ... print(i)
10 1 10 1
(...skipping 1613 matching lines...) Expand 10 before | Expand all | Expand 10 after
1624 >>> g.throw(ValueError,6) # throw on closed generator 1624 >>> g.throw(ValueError,6) # throw on closed generator
1625 Traceback (most recent call last): 1625 Traceback (most recent call last):
1626 ... 1626 ...
1627 ValueError: 6 1627 ValueError: 6
1628 1628
1629 >>> f().throw(ValueError,7) # throw on just-opened generator 1629 >>> f().throw(ValueError,7) # throw on just-opened generator
1630 Traceback (most recent call last): 1630 Traceback (most recent call last):
1631 ... 1631 ...
1632 ValueError: 7 1632 ValueError: 7
1633 1633
1634 Plain "raise" inside a generator should preserve the traceback (#13188).
1635 The traceback should have 3 levels:
1636 - g.throw()
1637 - f()
1638 - 1/0
1639
1640 >>> def f():
1641 ... try:
1642 ... yield
1643 ... except:
1644 ... raise
1645 >>> g = f()
1646 >>> try:
1647 ... 1/0
1648 ... except ZeroDivisionError as v:
1649 ... try:
1650 ... g.throw(v)
1651 ... except Exception as w:
1652 ... tb = w.__traceback__
1653 >>> levels = 0
1654 >>> while tb:
1655 ... levels += 1
1656 ... tb = tb.tb_next
1657 >>> levels
1658 3
1659
1634 Now let's try closing a generator: 1660 Now let's try closing a generator:
1635 1661
1636 >>> def f(): 1662 >>> def f():
1637 ... try: yield 1663 ... try: yield
1638 ... except GeneratorExit: 1664 ... except GeneratorExit:
1639 ... print("exiting") 1665 ... print("exiting")
1640 1666
1641 >>> g = f() 1667 >>> g = f()
1642 >>> next(g) 1668 >>> next(g)
1643 >>> g.close() 1669 >>> g.close()
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1852 # This worms around a bootstrap problem. 1878 # This worms around a bootstrap problem.
1853 # Note that doctest and regrtest both look in sys.argv for a "-v" argument, 1879 # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
1854 # so this works as expected in both ways of running regrtest. 1880 # so this works as expected in both ways of running regrtest.
1855 def test_main(verbose=None): 1881 def test_main(verbose=None):
1856 from test import support, test_generators 1882 from test import support, test_generators
1857 support.run_doctest(test_generators, verbose) 1883 support.run_doctest(test_generators, verbose)
1858 1884
1859 # This part isn't needed for regrtest, but for running the test directly. 1885 # This part isn't needed for regrtest, but for running the test directly.
1860 if __name__ == "__main__": 1886 if __name__ == "__main__":
1861 test_main(1) 1887 test_main(1)
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+