Skip to content

Offer typed terminal control alongside ordinary subprocess calls #106

Done

Description

@tony

Summary

Python’s ordinary subprocess interface accepts prepared descriptors and rejects an unknown vt= keyword. Libvt supplies terminal control through its async POSIX session. A synchronous caller must provide an owner and a background reader to combine those capabilities.

Motivation

A caller can get normal redirected streams immediately through the standard library. Obtaining the same executable as a continuously drained, resizable terminal currently requires an asynchronous owner and a distinct Windows launcher.

From the tagged libvt checkout, install the locked development dependencies with uv sync --frozen --python 3.14 --all-groups --all-extras. Use the checkout root as the working directory.

Save the following as case.py at the checkout root and run uv run --no-sync python case.py.

import asyncio
import subprocess
import sys
from libvt.aio import open_pty

cmd = [sys.executable, "-c", "print('ready', flush=True); print(input()[::-1])"]
plain = subprocess.run(cmd, input="pear\n", text=True, capture_output=True, check=True)
print("ordinary:", type(plain).__name__, type(plain.stdout).__name__, plain.stdout.splitlines())
try:
    subprocess.run(cmd, vt=True)
except TypeError as exc:
    print("stdlib VT option:", type(exc).__name__, "vt" in str(exc))

async def main():
    async with open_pty(cmd, rows=5, cols=30, tty_mode="raw") as session:
        await session.wait_for("ready")
        session.resize(rows=7, cols=40)
        session.paste("pear")
        session.send(b"\n")
        snap = await session.wait_for("raep")
        await session.wait_for(lambda _: session.eof, timeout=3)
        print("PTY:", snap.rows, snap.cols, "raep" in snap.text,
              await session.process.wait(), bool(session.terminal.recorder.tail()))

asyncio.run(main())

Evidence

CPython 3.14.6 on Linux under WSL2 ARM64. Identical output on release v0.0.1a4 (09611e5) and trunk (0864aa2).

Observed output
ordinary: CompletedProcess str ['ready', 'raep']
stdlib VT option: TypeError True
PTY: 7 40 True 0 True

Proposal

Explore libvt.contrib.subprocess.Popen(..., vt=...) and run(..., vt=...), with POSIX PTY and Windows ConPTY backends. Preserve precise signatures for ordinary text/binary calls and invalid-keyword diagnostics. Define ownership of merged terminal output, timeouts, bounded recording, final draining, direct process status and EOF. Keep persistent reattachment an explicit separate capability. A tested alternative keeps the genuine stdlib class and function and adds open_terminal(..., vt=...) and run_terminal(..., vt=...). Across eight ty platform and version targets this layout retained ten exact result types and rejected six misuses. Both Linux and Windows ran its ordinary and terminal calls. Keeping the exact same function names with an additional keyword still needs a separate overload strategy.

  • Developer experience — The same process launch vocabulary works in noninteractive tests and terminal tests, with supported terminal arguments discoverable by the type checker.
  • Immediate benefit — Removes repeated thread, event-loop and output-pump boilerplate from synchronous test suites.
  • Longer-term benefit — Provides a portable base for process control across Python testing tools while keeping ordinary subprocess behavior intact.
  • End-user experience — A developer can drive an existing debugger, editor or game in the background and inspect its actual screen.

References

Timeout and platform contracts

A live child waiting for input reproduced two exception families on Linux CPython 3.12–3.15. A missing wait_for predicate raises TerminalTimeoutError, a TimeoutError carrying the terminal diagnostic. A short wait or wait_eof raises TerminalTimeoutExpired, a subprocess.TimeoutExpired carrying capture and the process or eof phase. All three leave the live handle usable on deadline. A reader catching only TimeoutError misses the latter two. Document the catches side by side and distinguish an unmet predicate at EOF.

The synchronous source PR also owns the Darwin session-leader helper and Windows build 26100 floor for natural EOF. These are support decisions for the synchronous API. The native checks passed at the reviewed omnibus head. The direct stdlib aliases preserve the ordinary overloads selected in this proposal. Current API and platform contract

Activity

  1. added
    enhancement
    p2
    on Nov 30, 2026
  2. tony commented on Dec 3, 2026

    @tony
    ContributorAuthor

    The local Linux descendant replay distinguished process completion from terminal EOF. The direct child exited before the descendant's last output. wait() returned the child status, and wait_eof() collected the remaining PTY output. Related PR #142

  3. added
    area:blocking
    python
    topic:api
    topic:compatibility
    topic:timeouts
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:blockingenhancementp2pythontopic:apitopic:compatibilitytopic:timeouts

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions