Return whether a block of code is ready to execute, or should be continued Parameters ---------- source : string Python input code, which can be multiline. Returns ------- status : str One of 'complete', 'incomplete', or 'invalid'
(self, cell: str)
| 594 | return ''.join(lines) |
| 595 | |
| 596 | def check_complete(self, cell: str): |
| 597 | """Return whether a block of code is ready to execute, or should be continued |
| 598 | |
| 599 | Parameters |
| 600 | ---------- |
| 601 | source : string |
| 602 | Python input code, which can be multiline. |
| 603 | |
| 604 | Returns |
| 605 | ------- |
| 606 | status : str |
| 607 | One of 'complete', 'incomplete', or 'invalid' if source is not a |
| 608 | prefix of valid code. |
| 609 | indent_spaces : int or None |
| 610 | The number of spaces by which to indent the next line of code. If |
| 611 | status is not 'incomplete', this is None. |
| 612 | """ |
| 613 | # Remember if the lines ends in a new line. |
| 614 | ends_with_newline = False |
| 615 | for character in reversed(cell): |
| 616 | if character == '\n': |
| 617 | ends_with_newline = True |
| 618 | break |
| 619 | elif character.strip(): |
| 620 | break |
| 621 | else: |
| 622 | continue |
| 623 | |
| 624 | if not ends_with_newline: |
| 625 | # Append an newline for consistent tokenization |
| 626 | # See https://bugs.python.org/issue33899 |
| 627 | cell += '\n' |
| 628 | |
| 629 | lines = cell.splitlines(keepends=True) |
| 630 | |
| 631 | if not lines: |
| 632 | return 'complete', None |
| 633 | |
| 634 | if lines[-1].endswith('\\'): |
| 635 | # Explicit backslash continuation |
| 636 | return 'incomplete', find_last_indent(lines) |
| 637 | |
| 638 | try: |
| 639 | for transform in self.cleanup_transforms: |
| 640 | if not getattr(transform, 'has_side_effects', False): |
| 641 | lines = transform(lines) |
| 642 | except SyntaxError: |
| 643 | return 'invalid', None |
| 644 | |
| 645 | if lines[0].startswith('%%'): |
| 646 | # Special case for cell magics - completion marked by blank line |
| 647 | if lines[-1].strip(): |
| 648 | return 'incomplete', find_last_indent(lines) |
| 649 | else: |
| 650 | return 'complete', None |
| 651 | |
| 652 | try: |
| 653 | for transform in self.line_transforms: |