Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting exit code in atexit callback

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)
like image 360
Melebius Avatar asked Sep 05 '25 03:09

Melebius


1 Answers

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.

like image 141
Melebius Avatar answered Sep 07 '25 15:09

Melebius