Setting exit code in atexit callback

ぐ巨炮叔叔 提交于 2021-01-27 12:52:52

问题


Is there any way to set exit code in the function registered in atexit module and called on exit? The call to sys.exit(code) produces an error and does not set exit code to the desired value.

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

The contents of atexit_test.py:

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

import atexit
atexit.register(myexit)

回答1:


I can prove the test code works in Python 2.7.x (2.7.6 in my case), as reported by @Łukasz Rogalski. So I’ve assumed this to be a bug of Python 3.x and filed a bug report.



来源:https://stackoverflow.com/questions/37178636/setting-exit-code-in-atexit-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!