1

I would like to print the exit code in a function registered by atexit.

import atexit, sys, os

@atexit.register
def goodbye():
    print('Exit code', sys.???)

My script loads some not-standard modules which may raise an exception or call exit() and in all cases I would like to write the exit code to a log file. I checked the sys and os modules, but I found no such attribute of method.

UPDATE

The issue is more complicated than I thought. For quit(), exit() and sys_exit() atexit function is called. Actually they can be called practically with anything! Their argument can be empty, number, text or any class, object and anything else and this value is in the args argument of an SystemExit exception. But not for os._exit(), os.abort(). They behaves more like the C exit() and abort(), but should not be used as the housekeeping activities would be skipped!

TrueY
  • 7,360
  • 1
  • 41
  • 46
  • 1
    check [this](https://stackoverflow.com/questions/9741351/how-to-find-exit-code-or-reason-when-atexit-callback-is-called-in-python) question, it may be helpful. – George Imerlishvili Feb 12 '22 at 19:48
  • @GiorgiImerlishvili Thanks. I have seen this solution, but it is a bit more complex, then it supposed to be. In `Perl` it is like `END { print "Exit code: $?\n";}`. It is a bit strange to me at the `atexit` callback I can not even read the `SystemExit` exception, which was thrown as `sys.exc_info()` returns `(None, None, None)`. – TrueY Feb 13 '22 at 12:37

0 Answers0