Return whether a block of interactive input can accept more input. This method is meant to be used by line-oriented frontends, who need to guess whether a block is complete or not based solely on prior and current input lines. The InputSplitter considers it has a complete
(self)
| 442 | return self._is_complete |
| 443 | |
| 444 | def push_accepts_more(self): |
| 445 | """Return whether a block of interactive input can accept more input. |
| 446 | |
| 447 | This method is meant to be used by line-oriented frontends, who need to |
| 448 | guess whether a block is complete or not based solely on prior and |
| 449 | current input lines. The InputSplitter considers it has a complete |
| 450 | interactive block and will not accept more input when either: |
| 451 | |
| 452 | * A SyntaxError is raised |
| 453 | |
| 454 | * The code is complete and consists of a single line or a single |
| 455 | non-compound statement |
| 456 | |
| 457 | * The code is complete and has a blank line at the end |
| 458 | |
| 459 | If the current input produces a syntax error, this method immediately |
| 460 | returns False but does *not* raise the syntax error exception, as |
| 461 | typically clients will want to send invalid syntax to an execution |
| 462 | backend which might convert the invalid syntax into valid Python via |
| 463 | one of the dynamic IPython mechanisms. |
| 464 | """ |
| 465 | |
| 466 | # With incomplete input, unconditionally accept more |
| 467 | # A syntax error also sets _is_complete to True - see push() |
| 468 | if not self._is_complete: |
| 469 | #print("Not complete") # debug |
| 470 | return True |
| 471 | |
| 472 | # The user can make any (complete) input execute by leaving a blank line |
| 473 | last_line = self.source.splitlines()[-1] |
| 474 | if (not last_line) or last_line.isspace(): |
| 475 | #print("Blank line") # debug |
| 476 | return False |
| 477 | |
| 478 | # If there's just a single line or AST node, and we're flush left, as is |
| 479 | # the case after a simple statement such as 'a=1', we want to execute it |
| 480 | # straight away. |
| 481 | if self.get_indent_spaces() == 0: |
| 482 | if len(self.source.splitlines()) <= 1: |
| 483 | return False |
| 484 | |
| 485 | try: |
| 486 | code_ast = ast.parse(u''.join(self._buffer)) |
| 487 | except Exception: |
| 488 | #print("Can't parse AST") # debug |
| 489 | return False |
| 490 | else: |
| 491 | if len(code_ast.body) == 1 and \ |
| 492 | not hasattr(code_ast.body[0], 'body'): |
| 493 | #print("Simple statement") # debug |
| 494 | return False |
| 495 | |
| 496 | # General fallback - accept more code |
| 497 | return True |
| 498 | |
| 499 | def get_indent_spaces(self): |
| 500 | sourcefor, n = self._indent_spaces_cache |