[issue27035] Cannot set exit code in atexit callback

R. David Murray report at bugs.python.org
Mon May 16 10:28:23 EDT 2016


R. David Murray added the comment:

Calling sys.exit in an atexit function strikes me as a really bad idea.  It feels to me like it breaks the contract of what atexit is designed for, which is to run multiple handlers at exit.  If you call sys.exit you are in some sense restarting the exit processing, which feels broken to me.  It would feel equally broken, however, to call _exit, since that would mean the rest of the handlers in the chain would not be run.

In any case, I personally would *expect* an exit called in an atexit handler to have no effect, since an exit is already in progress and we've just taken a detour to run the handlers.  I can see the argument for the reverse, however, so it isn't obvious to me what the correct answer is.

I do note that the OP in #1257 prefers to use the sys.exit return code if an atexit handler raises an error, which argues for not replacing it if the atexit handler raises SystemExit with a different RC.  It would be more consistent with the proposed handling of atexit errors.

----------
nosy: +r.david.murray

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27035>
_______________________________________


More information about the Python-bugs-list mailing list