Push one or more lines of input. This stores the given lines and returns a status code indicating whether the code forms a complete Python block or not. Any exceptions generated in compilation are swallowed, but if an exception was produced, the method returns True.
(self, lines:str)
| 386 | self.reset() |
| 387 | |
| 388 | def push(self, lines:str) -> bool: |
| 389 | """Push one or more lines of input. |
| 390 | |
| 391 | This stores the given lines and returns a status code indicating |
| 392 | whether the code forms a complete Python block or not. |
| 393 | |
| 394 | Any exceptions generated in compilation are swallowed, but if an |
| 395 | exception was produced, the method returns True. |
| 396 | |
| 397 | Parameters |
| 398 | ---------- |
| 399 | lines : string |
| 400 | One or more lines of Python input. |
| 401 | |
| 402 | Returns |
| 403 | ------- |
| 404 | is_complete : boolean |
| 405 | True if the current input source (the result of the current input |
| 406 | plus prior inputs) forms a complete Python execution block. Note that |
| 407 | this value is also stored as a private attribute (``_is_complete``), so it |
| 408 | can be queried at any time. |
| 409 | """ |
| 410 | assert isinstance(lines, str) |
| 411 | self._store(lines) |
| 412 | source = self.source |
| 413 | |
| 414 | # Before calling _compile(), reset the code object to None so that if an |
| 415 | # exception is raised in compilation, we don't mislead by having |
| 416 | # inconsistent code/source attributes. |
| 417 | self.code, self._is_complete = None, None |
| 418 | self._is_invalid = False |
| 419 | |
| 420 | # Honor termination lines properly |
| 421 | if source.endswith('\\\n'): |
| 422 | return False |
| 423 | |
| 424 | try: |
| 425 | with warnings.catch_warnings(): |
| 426 | warnings.simplefilter('error', SyntaxWarning) |
| 427 | self.code = self._compile(source, symbol="exec") |
| 428 | # Invalid syntax can produce any of a number of different errors from |
| 429 | # inside the compiler, so we have to catch them all. Syntax errors |
| 430 | # immediately produce a 'ready' block, so the invalid Python can be |
| 431 | # sent to the kernel for evaluation with possible ipython |
| 432 | # special-syntax conversion. |
| 433 | except (SyntaxError, OverflowError, ValueError, TypeError, |
| 434 | MemoryError, SyntaxWarning): |
| 435 | self._is_complete = True |
| 436 | self._is_invalid = True |
| 437 | else: |
| 438 | # Compilation didn't produce any exceptions (though it may not have |
| 439 | # given a complete code object) |
| 440 | self._is_complete = self.code is not None |
| 441 | |
| 442 | return self._is_complete |
| 443 | |
| 444 | def push_accepts_more(self): |
| 445 | """Return whether a block of interactive input can accept more input. |