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

Side by Side Diff: Lib/test/test_generators.py

Issue 11682: PEP 380 reference implementation for 3.3
Patch Set: Created 7 years, 6 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:
View unified diff | Download patch
OLDNEW
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 709 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 720
721 >>> firstn(fib(), 17) 721 >>> firstn(fib(), 17)
722 [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584] 722 [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
723 723
724 """ 724 """
725 725
726 # syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0 726 # syntax_tests mostly provokes SyntaxErrors. Also fiddling with #if 0
727 # hackery. 727 # hackery.
728 728
729 syntax_tests = """ 729 syntax_tests = """
730
731 >>> def f():
732 ... return 22
733 ... yield 1
734 Traceback (most recent call last):
735 ..
736 SyntaxError: 'return' with argument inside generator
737
738 >>> def f():
739 ... yield 1
740 ... return 22
741 Traceback (most recent call last):
742 ..
743 SyntaxError: 'return' with argument inside generator
744
745 "return None" is not the same as "return" in a generator:
746
747 >>> def f():
748 ... yield 1
749 ... return None
750 Traceback (most recent call last):
751 ..
752 SyntaxError: 'return' with argument inside generator
753 730
754 These are fine: 731 These are fine:
755 732
756 >>> def f(): 733 >>> def f():
757 ... yield 1 734 ... yield 1
758 ... return 735 ... return
759 736
760 >>> def f(): 737 >>> def f():
761 ... try: 738 ... try:
762 ... yield 1 739 ... yield 1
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 >>> type(f()) 835 >>> type(f())
859 <class 'NoneType'> 836 <class 'NoneType'>
860 837
861 >>> def f(): 838 >>> def f():
862 ... if 0: 839 ... if 0:
863 ... return 840 ... return
864 ... if 0: 841 ... if 0:
865 ... yield 2 842 ... yield 2
866 >>> type(f()) 843 >>> type(f())
867 <class 'generator'> 844 <class 'generator'>
868
869
870 >>> def f():
871 ... if 0:
872 ... lambda x: x # shouldn't trigger here
873 ... return # or here
874 ... def f(i):
875 ... return 2*i # or here
876 ... if 0:
877 ... return 3 # but *this* sucks (line 8)
878 ... if 0:
879 ... yield 2 # because it's a generator (line 10)
880 Traceback (most recent call last):
881 SyntaxError: 'return' with argument inside generator
882 845
883 This one caused a crash (see SF bug 567538): 846 This one caused a crash (see SF bug 567538):
884 847
885 >>> def f(): 848 >>> def f():
886 ... for i in range(3): 849 ... for i in range(3):
887 ... try: 850 ... try:
888 ... continue 851 ... continue
889 ... finally: 852 ... finally:
890 ... yield i 853 ... yield i
891 ... 854 ...
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after
1558 >>> print(seq) 1521 >>> print(seq)
1559 [10, 20, 30] 1522 [10, 20, 30]
1560 1523
1561 1524
1562 Check some syntax errors for yield expressions: 1525 Check some syntax errors for yield expressions:
1563 1526
1564 >>> f=lambda: (yield 1),(yield 2) 1527 >>> f=lambda: (yield 1),(yield 2)
1565 Traceback (most recent call last): 1528 Traceback (most recent call last):
1566 ... 1529 ...
1567 SyntaxError: 'yield' outside function 1530 SyntaxError: 'yield' outside function
1568
1569 >>> def f(): return lambda x=(yield): 1
1570 Traceback (most recent call last):
1571 ...
1572 SyntaxError: 'return' with argument inside generator
1573 1531
1574 >>> def f(): x = yield = y 1532 >>> def f(): x = yield = y
1575 Traceback (most recent call last): 1533 Traceback (most recent call last):
1576 ... 1534 ...
1577 SyntaxError: assignment to yield expression not possible 1535 SyntaxError: assignment to yield expression not possible
1578 1536
1579 >>> def f(): (yield bar) = y 1537 >>> def f(): (yield bar) = y
1580 Traceback (most recent call last): 1538 Traceback (most recent call last):
1581 ... 1539 ...
1582 SyntaxError: can't assign to yield expression 1540 SyntaxError: can't assign to yield expression
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1920 # This worms around a bootstrap problem. 1878 # This worms around a bootstrap problem.
1921 # 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,
1922 # so this works as expected in both ways of running regrtest. 1880 # so this works as expected in both ways of running regrtest.
1923 def test_main(verbose=None): 1881 def test_main(verbose=None):
1924 from test import support, test_generators 1882 from test import support, test_generators
1925 support.run_doctest(test_generators, verbose) 1883 support.run_doctest(test_generators, verbose)
1926 1884
1927 # 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.
1928 if __name__ == "__main__": 1886 if __name__ == "__main__":
1929 test_main(1) 1887 test_main(1)
OLDNEW
« no previous file with comments | « Lib/test/test_ast.py ('k') | Lib/test/test_grammar.py » ('j') | Modules/parsermodule.c » ('J')

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