gpt4 book ai didi

Python 错误代码被调高

转载 作者:太空狗 更新时间:2023-10-29 22:14:05 26 4
gpt4 key购买 nike

考虑一个 python 脚本 error.py

import sys
sys.exit(3)

调用

python error.py; echo $?

产生预期的“3”。但是,请考虑 runner.py

import os
result = os.system("python error.py")
print result

yields 768。python代码的结果好像不知何故左移了8位,但这两种情况有何不同就不清楚了。怎么回事?

这发生在 python 2.5 和 2.6 中。

最佳答案

来自docs :

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

os.wait()

Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.

在您的例子中,二进制返回值 768 是 00000011 00000000。高字节为3。

关于Python 错误代码被调高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com