Extract the block of code at the top of the given list of lines.
(lines)
| 1113 | raise EndOfBlock |
| 1114 | |
| 1115 | def getblock(lines): |
| 1116 | """Extract the block of code at the top of the given list of lines.""" |
| 1117 | blockfinder = BlockFinder() |
| 1118 | try: |
| 1119 | tokens = tokenize.generate_tokens(iter(lines).__next__) |
| 1120 | for _token in tokens: |
| 1121 | blockfinder.tokeneater(*_token) |
| 1122 | except (EndOfBlock, IndentationError): |
| 1123 | pass |
| 1124 | except SyntaxError as e: |
| 1125 | if "unmatched" not in e.msg: |
| 1126 | raise e from None |
| 1127 | _, *_token_info = _token |
| 1128 | try: |
| 1129 | blockfinder.tokeneater(tokenize.NEWLINE, *_token_info) |
| 1130 | except (EndOfBlock, IndentationError): |
| 1131 | pass |
| 1132 | return lines[:blockfinder.last] |
| 1133 | |
| 1134 | def getsourcelines(object): |
| 1135 | """Return a list of source lines and starting line number for an object. |
no test coverage detected