ptyprocess
Launch and interact with a subprocess running inside a pseudo terminal (pty).
Repository Health
Technical Analysis
ptyprocess launches a subprocess in a pseudo terminal (pty) and lets you interact with both the process and its controlling terminal. When piping stdin and stdout is not enough — password prompts that read directly from the terminal, output that changes when not attached to a tty, or curses-style interfaces — running the process in a pty is the answer.
The library provides byte-oriented and Unicode-oriented process classes with methods to spawn, read, write, resize, and cleanly terminate the child, forming the low-level foundation that tools like pexpect build upon.
What You Get
- PtyProcess and PtyProcessUnicode classes for spawning processes in a pty
- Methods to read, write, resize the terminal window, and terminate the child
- Cross-platform Unix pty handling that underpins higher-level tools like pexpect
Common Use Cases
- Automating interactive CLIs that prompt for passwords outside of stdin
- Driving curses or full-screen terminal applications programmatically
- Capturing terminal-style output from tools that detect whether a tty is attached
Under The Hood
Architecture
ptyprocess wraps the Unix pty system calls: PtyProcess.spawn forks, opens a pty master/slave pair, sets the child’s controlling terminal, and execs the target. The ptyprocess package splits byte and Unicode handling across ptyprocess.py and _fork_pty.py, with helpers for window-size ioctls and signal delivery.
Tech Stack
Pure Python targeting POSIX systems, built on os, fcntl, termios, and pty from the standard library with no third-party runtime dependencies. It is packaged via pyproject.toml and documented with a Sphinx docs directory.
Code Quality
The repository includes a real pytest suite under tests/ covering spawning, echo, waiting, and invalid-binary handling, which is notable for a low-level terminal library. The code is compact and stable, matching its role as a dependency of the widely used pexpect project.
API Design
The public API is deliberately small — spawn, read, write, terminate, setwinsize — mirroring how you would think about a terminal session, so common tasks take only a few lines. The README’s interactive example gets users started immediately, though effective use still assumes familiarity with pty semantics.