本文整理汇总了Python中subprocess.CalledProcessError.result方法的典型用法代码示例。如果您正苦于以下问题:Python CalledProcessError.result方法的具体用法?Python CalledProcessError.result怎么用?Python CalledProcessError.result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类subprocess.CalledProcessError
的用法示例。
在下文中一共展示了CalledProcessError.result方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: capture_subprocess
# 需要导入模块: from subprocess import CalledProcessError [as 别名]
# 或者: from subprocess.CalledProcessError import result [as 别名]
def capture_subprocess(cmd, encoding='UTF-8', **popen_kwargs):
"""Run a command, showing its usual outputs in real time,
and return its stdout, stderr output as strings.
No temporary files are used.
"""
stdout = Pty() # libc uses full buffering for stdout if it doesn't see a tty
stderr = Pipe()
# deadlocks occur if we have any write-end of a pipe open more than once
# best practice: close any used write pipes just after spawn
outputter = Popen(
cmd,
stdout=stdout.write,
stderr=stderr.write,
**popen_kwargs
)
stdout.readonly() # deadlock otherwise
stderr.readonly() # deadlock otherwise
# start one tee each on the original stdout and stderr
# writing each to three places:
# 1. the original destination
# 2. a pipe just for that one stream
stdout_tee = Tee(stdout.read, STDOUT)
stderr_tee = Tee(stderr.read, STDERR)
# clean up left-over processes and pipes:
exit_code = outputter.wait()
result = (stdout_tee.join(), stderr_tee.join())
if encoding is not None:
result = tuple(
bytestring.decode(encoding)
for bytestring in result
)
if exit_code == 0:
return result
else:
error = CalledProcessError(exit_code, cmd)
error.result = result
raise error