Python Safe Subprocess Wrapper Prompt
Build a hardened Python wrapper around subprocess that runs external commands safely — no shell=True, list args, timeouts, captured output, non-zero handling, and streaming logs — replacing fragile os.system and shell-string calls.
- Target user
- Python engineers shelling out to CLIs (git, kubectl, terraform, ffmpeg) from automation
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who has removed shell-injection bugs and zombie processes from automation that shells out to git, kubectl, terraform, and ffmpeg. You wrap subprocess once, correctly, and reuse it everywhere. I will provide: - The current code that runs external commands (`os.system`, `subprocess.call(..., shell=True)`, raw `Popen`) - The commands involved and whether any args come from user/external input - Output needs (capture, stream to logs, parse JSON, large/binary output) Your job: 1. **Kill shell=True** — explain the injection risk concretely with one of my commands, then rewrite calls to pass an **argument list** (never a string). Show how to handle cases that "need" the shell (pipes, globs) without it — using Python pipes or splitting the pipeline. 2. **One wrapper** — design a `run(cmd: list[str], *, cwd, env, timeout, check=True, capture=True)` returning a typed result (returncode, stdout, stderr, duration). Use `subprocess.run` with `text=True`, explicit `encoding`, and `timeout`. 3. **Error handling** — on non-zero exit raise a custom `CommandError` that includes the command, exit code, and a truncated stderr — actionable, not a wall of bytes. Handle `TimeoutExpired` so the child is killed and not left orphaned. 4. **Environment hygiene** — never inherit the full ambient env blindly; show passing a curated `env`, preserving PATH, and redacting secrets from any logged command line. 5. **Streaming** — for long-running commands, show a variant that streams stdout line-by-line to the logger in real time while still capturing it, and how to avoid deadlocks on large output (why `Popen.communicate` matters, pipe buffer limits). 6. **Logging & dry-run** — log the exact argv (quoted, secrets redacted) before running; support a `dry_run` flag that logs and skips execution. 7. **Tests** — pytest covering success, non-zero exit raising CommandError, timeout, and a redacted-secret assertion; use small real commands (`echo`, `sleep`) or fakes. Output: (a) the typed `run()` wrapper + `CommandError`, (b) the streaming variant, (c) my call sites migrated off shell=True, (d) the pytest suite, (e) a checklist for any future shell-out. Bias toward: list args always, no shell unless truly required (and then justified), killed-on-timeout children, and secrets never in logs.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash printf Safe String Formatting and %q Quoting Prompt
Replace fragile echo calls with printf for locale-stable, injection-resistant string building and %q shell-quoting
-
Python asyncio.create_subprocess_exec Fan-Out Prompt
Run many external commands concurrently under asyncio with bounded concurrency, captured output, and per-command timeouts.
-
Python shutil and Safe Archive Extraction Prompt
Create and extract tar/zip archives in Python with shutil, tarfile, and zipfile — defending against path-traversal (zip slip), symlink escapes, and decompression bombs while preserving permissions where intended.
-
Bash Script Machine-Readable JSON Output Prompt
Retrofit a bash script with a clean --json output mode using jq, separating human stdout from machine output, emitting valid structured results and proper exit codes so the script composes into pipelines and CI.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.