Issue
Is there a workaround for https://bugs.python.org/issue37424 ?
That bug is biting me. I'm on Raspberry Pi OS which got Python 3.7.3 when I installed with sudo apt install python3
.
Unfortunately that bug is fixed only in Python 3.7.5 and later.
- Is there a workaround on Python 3.7.3?
- If not, can someone point me at a detailed explanation of how to (I suppose, compile and) install Python 3.7.5 (or later) on Raspberry Pi OS? I've been told it's "complicated" and may break other things (see https://www.raspberrypi.org/forums/viewtopic.php?t=291158 )
I just need the timeout to work.
FWIW, here's my code:
def command(string, verbose=None, echo_commands=ECHO_COMMANDS, timeout=SHORT_TIMEOUT):
''' Executes string as a command by the OS.
Returns subprocess.CompletedProcess (stout, stderr, returncode, etc.).
Note usual security precautions since shell=True (DO NOT use this with user input).
'''
if verbose is None:
verbose = VERBOSE
while True:
try:
if echo_commands:
print("command:", string)
cp = subprocess.run(string, shell=True, capture_output=True, text=True, timeout=timeout)
if verbose:
print(cp.stderr, cp.stdout)
return cp
except Exception as e:
print(e)
reboot_camera() #...and try again
Solution
I found a solution - use shell=False
(then timeouts work) and use shlex.split
to parse the command into parameters in Python instead of asking the shell to do it. So:
string = shlex.split(string) # workaround for https://bugs.python.org/issue37424 (plus shell=False instead of True)
cp = subprocess.run(string, shell=False, capture_output=True, text=True, timeout=timeout)
In order to make it work also on Windows, I had to modify the string that contains the name of the executable to include the full path (seems like Windows doesn't search $PATH without shell==True).
Old: CHDKPTP_EXE = r'chdkptp'
New: CHDKPTP_EXE = r'c:\\bin\\chdkptp.bat'
Answered By - nerdfever.com