|
| 1 | +# `libvt.contrib.subprocess` |
| 2 | + |
| 3 | +Use {mod}`libvt.contrib.subprocess` to drive a terminal application from |
| 4 | +synchronous Python. The session reads merged terminal output in a background |
| 5 | +thread while your caller waits for text, sends input, drains backpressure, or |
| 6 | +resizes the viewport. |
| 7 | + |
| 8 | +```python |
| 9 | +>>> import sys |
| 10 | +>>> from libvt.contrib import subprocess as sp |
| 11 | +>>> command = [ |
| 12 | +... sys.executable, |
| 13 | +... "-u", |
| 14 | +... "-c", |
| 15 | +... "print('Ready'); print('Result:', input()[::-1])", |
| 16 | +... ] |
| 17 | +>>> try: |
| 18 | +... with sp.open_terminal(command, vt=sp.VT(rows=6, cols=40)) as child: |
| 19 | +... _ = child.wait_for("Ready", timeout=8) |
| 20 | +... child.resize(rows=8, cols=48) |
| 21 | +... child.paste("pear") |
| 22 | +... child.send_key("enter") |
| 23 | +... status = child.wait(timeout=8) |
| 24 | +... final = child.wait_eof(timeout=8) |
| 25 | +... assert (status, final.snapshot.lines[2]) == (0, "Result: raep") |
| 26 | +... except NotImplementedError: |
| 27 | +... pass |
| 28 | +``` |
| 29 | + |
| 30 | +For commands that finish on their own, call |
| 31 | +{func}`~libvt.contrib.subprocess.run_terminal` with a |
| 32 | +{class}`~libvt.contrib.subprocess.VT` geometry configuration and pass the |
| 33 | +returned {class}`~libvt.contrib.subprocess.CompletedTerminal` directly to |
| 34 | +{func}`~libvt.testing.vt_screen` or |
| 35 | +{func}`~libvt.testing.serialize_vt_snapshot`: |
| 36 | + |
| 37 | +```python |
| 38 | +>>> import sys |
| 39 | +>>> from libvt.contrib import subprocess as sp |
| 40 | +>>> from libvt.testing import serialize_vt_snapshot |
| 41 | +>>> try: |
| 42 | +... done = sp.run_terminal( |
| 43 | +... [sys.executable, "-c", "print('OK')"], |
| 44 | +... vt=sp.VT(rows=2, cols=8), |
| 45 | +... check=True, |
| 46 | +... timeout=8, |
| 47 | +... ) |
| 48 | +... assert "│OK │" in serialize_vt_snapshot(done) |
| 49 | +... except NotImplementedError: |
| 50 | +... pass |
| 51 | +``` |
| 52 | + |
| 53 | +## Waiting, timeouts, and EOF |
| 54 | + |
| 55 | +{meth}`~libvt.contrib.subprocess.TerminalProcess.wait` returns the direct |
| 56 | +child's exit status. Use |
| 57 | +{meth}`~libvt.contrib.subprocess.TerminalProcess.wait_eof` to collect final |
| 58 | +output after every process attached to the terminal closes its terminal |
| 59 | +handles, since descendants can keep the terminal open after the direct child |
| 60 | +exits. {meth}`~libvt.contrib.subprocess.TerminalProcess.drain` waits until all |
| 61 | +queued outbound bytes flush to the terminal. Each of `wait()`, `wait_eof()`, |
| 62 | +and `drain()` leaves the live handle open on timeout. |
| 63 | + |
| 64 | +Two distinct timeout exception families apply depending on which call timed |
| 65 | +out; catching only builtin {exc}`TimeoutError` misses |
| 66 | +{exc}`~libvt.contrib.subprocess.TerminalTimeoutExpired`: |
| 67 | + |
| 68 | +| Call | Timeout exception | Base classes | Failure detail | |
| 69 | +| --- | --- | --- | --- | |
| 70 | +| {meth}`~libvt.contrib.subprocess.TerminalProcess.wait_for` | {exc}`~libvt.exc.TerminalTimeoutError` | {exc}`TimeoutError`, {exc}`~libvt.exc.LibVtError` | Check `.eof` to distinguish an unmet predicate at terminal EOF from deadline expiry. | |
| 71 | +| {meth}`~libvt.contrib.subprocess.TerminalProcess.wait`, {meth}`~libvt.contrib.subprocess.TerminalProcess.wait_eof`, {meth}`~libvt.contrib.subprocess.TerminalProcess.drain`, {func}`~libvt.contrib.subprocess.run_terminal` | {exc}`~libvt.contrib.subprocess.TerminalTimeoutExpired` | {exc}`subprocess.TimeoutExpired`, {exc}`~libvt.exc.LibVtError` | Inspect `.phase` (`"drain"`, `"process"`, or `"eof"`) and `.capture`. | |
| 72 | + |
| 73 | +{func}`~libvt.contrib.subprocess.run_terminal` waits for outbound input drain, |
| 74 | +child exit, and terminal EOF under a shared timeout. When `check=True` and the |
| 75 | +direct child exits nonzero, it raises |
| 76 | +{exc}`~libvt.contrib.subprocess.CalledTerminalError` (exposing `.result`). On |
| 77 | +timeout, it closes the terminal and reaps the direct child before raising |
| 78 | +{exc}`~libvt.contrib.subprocess.TerminalTimeoutExpired`. Interactive commands |
| 79 | +that wait for a prompt work with |
| 80 | +{func}`~libvt.contrib.subprocess.open_terminal`. |
| 81 | + |
| 82 | +## Snapshots and output retention |
| 83 | + |
| 84 | +Use a live {class}`~libvt.contrib.subprocess.TerminalProcess` handle from its |
| 85 | +creating thread and close it via `with` or |
| 86 | +{meth}`~libvt.contrib.subprocess.TerminalProcess.close`. Call `wait_eof()` |
| 87 | +first when descendant output matters. |
| 88 | +{class}`~libvt.contrib.subprocess.Capture`, |
| 89 | +{class}`~libvt.contrib.subprocess.CompletedTerminal`, and |
| 90 | +{class}`~libvt.contrib.subprocess.TerminalProcess` implement |
| 91 | +{class}`~libvt.testing.SupportsVtTarget`, so |
| 92 | +{func}`~libvt.testing.vt_screen` and |
| 93 | +{func}`~libvt.testing.serialize_vt_snapshot` accept them directly. |
| 94 | + |
| 95 | +Each capture holds the last committed {class}`~libvt.snapshot.Snapshot`, the |
| 96 | +bounded `raw_tail` (up to `VT.raw_limit`; `0` disables tail retention while |
| 97 | +still counting `raw_total`), and whether EOF was reached. Raw bytes include |
| 98 | +terminal control sequences and native console translation. |
| 99 | + |
| 100 | +## Platform notes |
| 101 | + |
| 102 | +- **Linux / POSIX PTY**: `close()` escalates `SIGHUP`, `SIGTERM`, and `SIGKILL` |
| 103 | + to the child process group. A descendant that ignores hangup can outlive the |
| 104 | + terminal after the direct child exits. |
| 105 | +- **macOS (Darwin)**: A single-trampoline helper owns the controlling PTY |
| 106 | + session over a socketpair so direct-child exit leaves the terminal open for |
| 107 | + descendants; `close()` terminates the direct child with `SIGKILL` and shuts |
| 108 | + down the session leader. Opening `/dev/tty` can hold a Darwin kernel |
| 109 | + reference until the session closes (see |
| 110 | + [Apple's controlling-terminal driver](https://github.com/apple-oss-distributions/xnu/blob/f6217f891ac0bb64f3d375211650a4c1ff8ca1ea/bsd/kern/tty_tty.c)); |
| 111 | + call `close()` once you have collected the output you need. |
| 112 | +- **Windows (ConPTY)**: Requires `ReleasePseudoConsole` (Windows 11 24H2 or |
| 113 | + Windows Server 2025, build 26100; see |
| 114 | + [Microsoft's ReleasePseudoConsole documentation](https://learn.microsoft.com/en-us/windows/console/releasepseudoconsole)) |
| 115 | + to observe natural EOF after the last console client exits. `close()` |
| 116 | + terminates the child via `TerminateProcess` and closes the pseudoconsole. |
| 117 | + Pass an absolute executable path for consistent executable lookup across |
| 118 | + operating systems. |
| 119 | + |
| 120 | +## API reference |
| 121 | + |
| 122 | +```{eval-rst} |
| 123 | +.. automodule:: libvt.contrib.subprocess |
| 124 | + :members: |
| 125 | +``` |
0 commit comments