我正在寻找一种便携式来检查 subprocess
带有指示成功的出口代码的。
我发现某些系统不遵循 0
的标准惯例,意思是“成功”(例如 OpenVMS )因此,该解决方案不像 Process.ReturnCode == 0
那样微不足道。
I'm looking for a portable way to check whether a subprocess
exited with an exit code indicating a success.
I found out that some systems don't follow the standard convention of 0
meaning "success" (e.g. OpenVMS) so the solution isn't as trivial as just process.returncode == 0
.
发布评论
评论(1)
subprocess
的文档要求0
表示成功,例如subprocess.run
(全级高级同步API),当check = true
,文档指定:OpenVMS实际上并不是CPYTHON的支持目标,并且所有支持的目标都遵守我所知的规则。
大概是第三方OpenVMS端口必须包括移植适当的错误检查(即使它不更改退出代码,
check = true
应该更改以使其仅在当时提高异常孩子失败)。因此,最简单的解决方案是将您的代码编写为:如果OpenVMS没有使
Check = true
在其端口中以逻辑上正确的方式工作,请对其进行错误,以使其在逻辑上匹配预期行为(即使文档说“非零”时,在子过程失败时提出了例外)。除此之外,您唯一的选项正在检查sys.platform
并在OpenVM上手动检查错误代码(我不知道
sys.platform
的OpenVMS报告是什么,您必须自己检查它)。The docs for
subprocess
effectively require that0
means success, e.g. forsubprocess.run
(the all-in-one high level synchronous API), whencheck=True
, the docs specify:OpenVMS isn't actually a supported target for CPython, and all supported targets obey that rule to my knowledge.
Presumably the third-party OpenVMS port would have to include porting proper error-checking (even if it doesn't change the exit codes,
check=True
should be changed to make it only raise the exception when the child fails). So the simplest solution would be to write your code as:and if OpenVMS didn't make
check=True
work the logically correct way in their port, file a bug against them to make it logically match the intended behavior (exception raised when subprocess fails, even if docs say "non-zero"). Aside from that, your only option is checkingsys.platform
and manually checking the error code a different way on OpenVMS (I have no idea what OpenVMS reports forsys.platform
, you'd have to check it yourself).