[Python-Dev] atexit missing an unregister method

Skip Montanaro skip at pobox.com
Tue Apr 26 22:06:50 CEST 2005


    Nick> But while you can mark functions to be called with the 'register'
    Nick> method, there's no 'unregister' method to remove them from the
    Nick> stack of functions to be called.  Nor is there any way to view
    Nick> this stack and e.g. call 'del' on a registered function.

    Nick> This would be useful in the following scenario, in which x and y
    Nick> are resources that need to be cleaned up, even in the event of a
    Nick> program exit:

    Nick> import atexit

    Nick> def free_resource(resource):
    Nick>     ...

    Nick> atexit.register(free_resource, x)
    Nick> atexit.register(free_resource, y)
    Nick> # do operations with x and y, potentially causing the program to exit
    Nick> ...
    Nick> # if nothing caused the program to unexpectedly quit, close the resources
    Nick> free_resource(x)
    Nick> free_resource(y)
    Nick> #unregister the functions, so that you don't try to free the resources 
    Nick> twice!
    Nick> atexit.unregisterall()

This seems like a poor argument for unregistering exit handlers.  If you've
registered an exit handler, why then explicitly do what you've already asked
the system to do?  Also, your proposed unregisterall() function would be
dangerous.  As an application writer you don't know what other parts of the
system (libraries you use, for example) might have registered exit
functions.

Skip


More information about the Python-Dev mailing list