Send a command to the REPL, wait for and return output. :param str command: The command to send. Trailing newlines are not needed. This should be a complete block of input that will trigger execution; if a continuation prompt is found after sending input, :exc:`ValueErro
(self, command, timeout=-1, async_=False)
| 66 | timeout=timeout, async_=async_) |
| 67 | |
| 68 | def run_command(self, command, timeout=-1, async_=False): |
| 69 | """Send a command to the REPL, wait for and return output. |
| 70 | |
| 71 | :param str command: The command to send. Trailing newlines are not needed. |
| 72 | This should be a complete block of input that will trigger execution; |
| 73 | if a continuation prompt is found after sending input, :exc:`ValueError` |
| 74 | will be raised. |
| 75 | :param int timeout: How long to wait for the next prompt. -1 means the |
| 76 | default from the :class:`pexpect.spawn` object (default 30 seconds). |
| 77 | None means to wait indefinitely. |
| 78 | :param bool async_: On Python 3.4, or Python 3.3 with asyncio |
| 79 | installed, passing ``async_=True`` will make this return an |
| 80 | :mod:`asyncio` Future, which you can yield from to get the same |
| 81 | result that this method would normally give directly. |
| 82 | """ |
| 83 | # Split up multiline commands and feed them in bit-by-bit |
| 84 | cmdlines = command.splitlines() |
| 85 | # splitlines ignores trailing newlines - add it back in manually |
| 86 | if command.endswith('\n'): |
| 87 | cmdlines.append('') |
| 88 | if not cmdlines: |
| 89 | raise ValueError("No command was given") |
| 90 | |
| 91 | if async_: |
| 92 | from ._async import repl_run_command_async |
| 93 | return repl_run_command_async(self, cmdlines, timeout) |
| 94 | |
| 95 | res = [] |
| 96 | self.child.sendline(cmdlines[0]) |
| 97 | for line in cmdlines[1:]: |
| 98 | self._expect_prompt(timeout=timeout) |
| 99 | res.append(self.child.before) |
| 100 | self.child.sendline(line) |
| 101 | |
| 102 | # Command was fully submitted, now wait for the next prompt |
| 103 | if self._expect_prompt(timeout=timeout) == 1: |
| 104 | # We got the continuation prompt - command was incomplete |
| 105 | self.child.kill(signal.SIGINT) |
| 106 | self._expect_prompt(timeout=1) |
| 107 | raise ValueError("Continuation prompt found - input was incomplete:\n" |
| 108 | + command) |
| 109 | return u''.join(res + [self.child.before]) |
| 110 | |
| 111 | def python(command=sys.executable): |
| 112 | """Start a Python shell and return a :class:`REPLWrapper` object.""" |