问题
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