|
14 | 14 | 1 |
15 | 15 | >>> g.next() |
16 | 16 | 2 |
| 17 | +
|
| 18 | + "Falling off the end" stops the generator: |
| 19 | +
|
17 | 20 | >>> g.next() |
18 | 21 | Traceback (most recent call last): |
19 | 22 | File "<stdin>", line 1, in ? |
20 | 23 | File "<stdin>", line 2, in g |
21 | 24 | StopIteration |
22 | 25 |
|
23 | | -"return" stops the generator: |
| 26 | +"return" also stops the generator: |
24 | 27 |
|
25 | 28 | >>> def f(): |
26 | 29 | ... yield 1 |
|
424 | 427 |
|
425 | 428 | >>> def m235(): |
426 | 429 | ... yield 1 |
427 | | -... # Gack: m235 below actually refers to a LazyList. |
| 430 | +... # Gack: m235 below actually refers to a LazyList. |
428 | 431 | ... me_times2 = times(2, m235) |
429 | 432 | ... me_times3 = times(3, m235) |
430 | 433 | ... me_times5 = times(5, m235) |
|
443 | 446 | [400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675] |
444 | 447 | """ |
445 | 448 |
|
| 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 | +""" |
446 | 524 |
|
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} |
451 | 530 |
|
452 | 531 | # Magic test name that regrtest.py invokes *after* importing this module. |
453 | 532 | # This worms around a bootstrap problem. |
|
0 commit comments