Control read-eval-print-loops — Pexpect 4.9 documentation (original) (raw)

Pexpect

Generic wrapper for read-eval-print-loops, a.k.a. interactive shells

Added in version 3.3.

class pexpect.replwrap.REPLWrapper(cmd_or_spawn, orig_prompt, prompt_change, new_prompt='[PEXPECT_PROMPT>', continuation_prompt='[PEXPECT_PROMPT+', extra_init_cmd=None)[source]

Wrapper for a REPL.

Parameters:

run_command(command, timeout=-1, async_=False)[source]

Send a command to the REPL, wait for and return output.

Parameters:

pexpect.replwrap.PEXPECT_PROMPT

A string that can be used as a prompt, and is unlikely to be found in output.

Using the objects above, it is easy to wrap a REPL. For instance, to use a Python shell:

py = REPLWrapper("python", ">>> ", "import sys; sys.ps1={!r}; sys.ps2={!r}") py.run_command("4+7")

Convenience functions are provided for Python and bash shells:

pexpect.replwrap.python(command='/home/docs/checkouts/readthedocs.org/user_builds/pexpect/envs/latest/bin/python')[source]

Start a Python shell and return a REPLWrapper object.

pexpect.replwrap.bash(command='bash')[source]

Start a bash shell and return a REPLWrapper object.