便携式方法检查子过程退出代码是否表示成功?

发布于 2025-02-09 13:13:42 字数 296 浏览 1 评论 0 原文

我正在寻找一种便携式来检查 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2025-02-16 13:13:42

subprocess 的文档要求 0 表示成功,例如 subprocess.run (全级高级同步API),当 check = true ,文档指定:

如果 check 是正确的,并且该过程使用非零退出代码退出,则将提高 naterprocesserror 例外。

OpenVMS实际上并不是CPYTHON的支持目标,并且所有支持的目标都遵守我所知的规则。

大概是第三方OpenVMS端口必须包括移植适当的错误检查(即使它不更改退出代码, check = true 应该更改以使其仅在当时提高异常孩子失败)。因此,最简单的解决方案是将您的代码编写为:

try:
    proc = subprocess.run(..., check=True)
except subprocess.CalledProcessError:
    # Handle failure here

如果OpenVMS没有使 Check = true 在其端口中以逻辑上正确的方式工作,请对其进行错误,以使其在逻辑上匹配预期行为(即使文档说“非零”时,在子过程失败时提出了例外)。除此之外,您唯一的选项正在检查 sys.platform 并在OpenVM上手动检查错误代码(我不知道 sys.platform 的OpenVMS报告是什么,您必须自己检查它)。

The docs for subprocess effectively require that 0 means success, e.g. for subprocess.run (the all-in-one high level synchronous API), when check=True, the docs specify:

If check is true, and the process exits with a non-zero exit code, a CalledProcessError exception will be raised.

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:

try:
    proc = subprocess.run(..., check=True)
except subprocess.CalledProcessError:
    # Handle failure here

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 checking sys.platform and manually checking the error code a different way on OpenVMS (I have no idea what OpenVMS reports for sys.platform, you'd have to check it yourself).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文