Estou tentando capturar a mensagem de erro de um programa chamado por subprocess.check_call
mas stderr
o objeto de erro é sempre None
.
Aqui está um pequeno script que mostra isso:
import subprocess
import sys
if '--fail' in sys.argv: # the script calls itself with this option, for the test
print('this is the error message', file=sys.stderr)
sys.exit(1)
try:
subprocess.check_call([sys.executable, sys.argv[0], '--fail'],
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
print('Not failed?') # this neither happens nor is expected
except subprocess.CalledProcessError as err:
if err.stderr:
print(err.stderr.decode()) # this is what I expect
else:
print('<No error message>') # this is what happens
Eu tenho Python 3.10.12.
Você não se comunicou com o subprocesso, então o conteúdo ainda está armazenado em buffer no canal.
Os documentos de
check_call
têm este aviso:Em vez disso:
Use a API de nível superior de run :