Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea2e97a

Browse files
committed
New tests to provoke SyntaxErrors unique to generators. Minor fiddling
of other tests.
1 parent 08bba95 commit ea2e97a

1 file changed

Lines changed: 85 additions & 6 deletions

File tree

Lib/test/test_generators.py

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
1
1515
>>> g.next()
1616
2
17+
18+
"Falling off the end" stops the generator:
19+
1720
>>> g.next()
1821
Traceback (most recent call last):
1922
File "<stdin>", line 1, in ?
2023
File "<stdin>", line 2, in g
2124
StopIteration
2225
23-
"return" stops the generator:
26+
"return" also stops the generator:
2427
2528
>>> def f():
2629
... yield 1
@@ -424,7 +427,7 @@
424427
425428
>>> def m235():
426429
... yield 1
427-
... # Gack: m235 below actually refers to a LazyList.
430+
... # Gack: m235 below actually refers to a LazyList.
428431
... me_times2 = times(2, m235)
429432
... me_times3 = times(3, m235)
430433
... me_times5 = times(5, m235)
@@ -443,11 +446,87 @@
443446
[400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675]
444447
"""
445448

449+
# syntax_tests mostly provokes SyntaxErrors.
450+
451+
syntax_tests = """
452+
453+
>>> def f():
454+
... return 22
455+
... yield 1
456+
Traceback (most recent call last):
457+
...
458+
SyntaxError: 'return' with argument inside generator (<string>, line 2)
459+
460+
>>> def f():
461+
... yield 1
462+
... return 22
463+
Traceback (most recent call last):
464+
...
465+
SyntaxError: 'return' with argument inside generator (<string>, line 3)
466+
467+
"return None" is not the same as "return" in a generator:
468+
469+
>>> def f():
470+
... yield 1
471+
... return None
472+
Traceback (most recent call last):
473+
...
474+
SyntaxError: 'return' with argument inside generator (<string>, line 3)
475+
476+
This one is fine:
477+
478+
>>> def f():
479+
... yield 1
480+
... return
481+
482+
>>> def f():
483+
... try:
484+
... yield 1
485+
... finally:
486+
... pass
487+
Traceback (most recent call last):
488+
...
489+
SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<string>, line 3)
490+
491+
>>> def f():
492+
... try:
493+
... try:
494+
... 1/0
495+
... except ZeroDivisionError:
496+
... yield 666 # bad because *outer* try has finally
497+
... except:
498+
... pass
499+
... finally:
500+
... pass
501+
Traceback (most recent call last):
502+
...
503+
SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<string>, line 6)
504+
505+
But this is fine:
506+
507+
>>> def f():
508+
... try:
509+
... try:
510+
... yield 12
511+
... 1/0
512+
... except ZeroDivisionError:
513+
... yield 666
514+
... except:
515+
... try:
516+
... x = 12
517+
... finally:
518+
... yield 12
519+
... except:
520+
... return
521+
>>> list(f())
522+
[12, 666]
523+
"""
446524

447-
__test__ = {"tut": tutorial_tests,
448-
"pep": pep_tests,
449-
"email": email_tests,
450-
"fun": fun_tests}
525+
__test__ = {"tut": tutorial_tests,
526+
"pep": pep_tests,
527+
"email": email_tests,
528+
"fun": fun_tests,
529+
"syntax": syntax_tests}
451530

452531
# Magic test name that regrtest.py invokes *after* importing this module.
453532
# This worms around a bootstrap problem.

0 commit comments

Comments
 (0)