【问题标题】:Setting exit code in atexit callback在 atexit 回调中设置退出代码
【发布时间】:2016-05-12 06:14:00
【问题描述】:

有没有办法在atexit 模块中注册并在退出时调用的函数中设置退出代码?对sys.exit(code) 的调用会产生错误,并且不会将退出代码设置为所需的值。

d:\>python atexit_test.py
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "atexit_test.py", line 3, in myexit
    sys.exit(2)
SystemExit: 2

d:\>echo %ERRORLEVEL%
0

atexit_test.py的内容:

def myexit():
  import sys
  sys.exit(2)

import atexit
atexit.register(myexit)

【问题讨论】:

  • 使用的是什么 Python 版本?我可以在 Python 3.5.1 上重现它,但它在 Python 2.7.10 中运行良好(sys.exit 和提升 SystemExit)。你能使用正确的标签(python-2.x 或 python-3.x)吗?
  • @Rogalski 我正在使用 Python 3.5.1。

标签: python python-3.x python-3.5 atexit


【解决方案1】:

我可以证明测试代码在 Python 2.7.x(在我的情况下为 2.7.6)中有效,如 reported by @Łukasz Rogalski。所以我认为这是 Python 3.x 的一个错误,并提交了bug report

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多