如何绕过python中sys.exit()的0-255范围限制?

发布于 2024-10-07 16:09:30 字数 424 浏览 6 评论 0原文

在 python 中(在 Linux 系统上),我使用 os.system() 启动命令并检索返回码。如果该返回代码不同于 0,我想让程序以相同的返回代码退出。所以我写道:

ret = os.system(cmd)
if ret != 0:
   print "exit with status %s" % ret
   sys.exit(ret)

当返回代码低于256时,它工作正常,但是当它大于255时,使用的退出代码是0。如何使sys.exit()接受大于255的代码?

编辑:限制实际上是 255

事实上,ret 变量接收 256,但 sys.exit() 无法使用它,因此程序返回 0。当我手动启动 cmd 时,我看到它返回 1,而不是 256。

In python (on a Linux system), I'm launching a command using os.system() and retrieving the return code. If that return code is different from 0, I would like to make the program exit with that same return code. So I wrote:

ret = os.system(cmd)
if ret != 0:
   print "exit with status %s" % ret
   sys.exit(ret)

When the return code is lower than 256, it works fine, but when it's greater than 255, the exit code used is 0. How can I make sys.exit() accept codes greater than 255?

Edit: the limit is actually 255

In fact, the ret variable receives 256, but sys.exit() fails to use it, so the program returns 0 instead. When I launch cmd manually, I see that it returns 1, not 256.

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

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

发布评论

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

评论(3

我最亲爱的 2024-10-14 16:09:30

实际上,文档表明

退出状态指示:一个16位数字,低字节是杀死进程的信号号,高字节是退出状态(如果信号号为零);如果生成了核心文件,则设置低字节的高位。

所以我需要处理 sys.exit() 返回的数字,以便检索真正的退出状态,如下所示:

ret = os.system(cmd)
# Ignore the first byte, use the second one only
ret = ret >> 8
if ret != 0:
   print "exit with status %s" % ret
   sys.exit(ret)

现在它可以工作了: ret 包含 1 而不是 256。

Actually, the documentation indicates that

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.

So I need to process the number returned by sys.exit() in order to retrieve the real exit status, like so:

ret = os.system(cmd)
# Ignore the first byte, use the second one only
ret = ret >> 8
if ret != 0:
   print "exit with status %s" % ret
   sys.exit(ret)

And now it works: ret contains 1 instead of 256.

呆° 2024-10-14 16:09:30

不能不应该,因为系统为自己保留了128以上的退出代码。如果返回码是从0到127,则表示程序自行退出。如果返回码高于128,则意味着程序被系统信号(例如SIGKILL或SIGTERM)终止。信号编号是结果代码减 128。

我认为您需要检测它并进行其他类型的输出指示。也许将返回代码打印到 STDOUT,然后返回 0(正常退出,正常输出)或 1(cmd 有一个非零返回值,输出意味着什么)。

编辑:显然(来自 此评论),Python 比 shell 脚本更聪明...要将其放回常规 Linux 退出代码,请尝试以下操作:

subprocess_exit_code = (ret >> 8) & 0x7f
subprocess_signal_number = ret & 0xff

if subpprocess_signal_number == 0:
  exit_code = subprocess_exit_code
else:
  exit_code = 128 + subprocess_signal_number

尽管这会忽略核心转储信息。

You can't shouldn't, because the system reserves exit codes above 128 for itself. If the return code is from 0 to 127, it means that the program exited by itself. If the return code is higher than 128, it means that the program was terminated by a system signal (e.g. SIGKILL or SIGTERM). The signal number is the result code minus 128.

You'll need to detect it and make some other kind of output indication, I think. Perhaps print the return code to STDOUT and then return with either 0 (normal exit, normal output) or 1 (cmd had a non-zero return value, output means something).

edit: Apparently (from this comment), Python is more clever than a shell script... To put it back into a regular Linux exit code, try this:

subprocess_exit_code = (ret >> 8) & 0x7f
subprocess_signal_number = ret & 0xff

if subpprocess_signal_number == 0:
  exit_code = subprocess_exit_code
else:
  exit_code = 128 + subprocess_signal_number

Although this ignores the core-dump information.

神爱温柔 2024-10-14 16:09:30

对我有用(CentOS 5.5,Python 2.6.5):

$ python
>>> import sys
>>> sys.exit(245)
$ echo $?
245

Works for me (CentOS 5.5, Python 2.6.5):

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