Created
February 18, 2015 21:11
-
-
Save ramalho/c72b2966d71084083610 to your computer and use it in GitHub Desktop.
Expansion of the `yield from` statement, from PEP 380 -- Syntax for Delegating to a Subgenerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code below is the expansion of the statement: | |
| # | |
| # RESULT = yield from EXPR | |
| # | |
| # Copied verbatim from the Formal Semantics section of | |
| # PEP 380 -- Syntax for Delegating to a Subgenerator | |
| # | |
| # https://www.python.org/dev/peps/pep-0380/#formal-semantics | |
| _i = iter(EXPR) | |
| try: | |
| _y = next(_i) | |
| except StopIteration as _e: | |
| _r = _e.value | |
| else: | |
| while 1: | |
| try: | |
| _s = yield _y | |
| except GeneratorExit as _e: | |
| try: | |
| _m = _i.close | |
| except AttributeError: | |
| pass | |
| else: | |
| _m() | |
| raise _e | |
| except BaseException as _e: | |
| _x = sys.exc_info() | |
| try: | |
| _m = _i.throw | |
| except AttributeError: | |
| raise _e | |
| else: | |
| try: | |
| _y = _m(*_x) | |
| except StopIteration as _e: | |
| _r = _e.value | |
| break | |
| else: | |
| try: | |
| if _s is None: | |
| _y = next(_i) | |
| else: | |
| _y = _i.send(_s) | |
| except StopIteration as _e: | |
| _r = _e.value | |
| break | |
| RESULT = _r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment