[Python-Dev] Proposal: standard way of defining and executing "atexit" functions...

Skip Montanaro skip@mojam.com (Skip Montanaro)
Mon, 19 Jun 2000 15:17:59 -0500


Python's sys module defines an exitfunc variable that is settable from
Python scripts.  At exit, that function will be called with no arguments.
While this is a good start at supporting standard cleanup activities, it
defines no protocol to be used by modules that wish to use sys.exitfunc,
which leads to one of two extremes: 1, two modules wishing to define cleanup
functions both clobber sys.exitfunc or 2, to avoid collisions they don't use
the functionality provided.

At an application level this is okay.  Within a single application you can
define an application-specific protocol to handle the situation.
Unfortunately, this still leaves Python's core modules without a good way to
register exit functions.

I ran into this problem today.  I would really like the rlcompleter module
to read and write readline history files from Python.  Reading a history
file at module startup is no problem, but deciding where to write the
history file is a problem.  The logical place is at the time sys.exitfunc is
executed.

For my own applications I long ago wrote a very simple module (called
exit.py, appended to this message) that defines two functions:

    * exit.register_exitfunc takes a function object and a set of optional
      arguments and appends them to a list.

    * exit._run_exitfuncs is bound to sys.exitfunc and executes the
      registered exit functions in the order they appear in the list.

I propose exit.py as the starting point for a well-defined protocol for
modules to register exit functions without collisions.

-- 
Skip Montanaro, skip@mojam.com, http://www.mojam.com/, http://www.musi-cal.com/
On June 24th at 8AM, live your life for an hour as Ricky Byrdsong always lived
his - run/walk in the Ricky Byrdsong Memorial 5K or just make a donation:
    https://www.SignmeupSports.com/Events/Index_Events.asp?EventID=1395


"""
allow programmer to define multiple exit functions to be executed upon normal
program termination.
"""

_exithandlers = []
def _run_exitfuncs():
    while _exithandlers:
        func, targs, kargs = _exithandlers[0]
        apply(func, targs, kargs)
        _exithandlers.remove(_exithandlers[0])

def register_exitfunc(func, *targs, **kargs):
    """register a function to be executed upon normal program termination

    arguments are a function object, and zero or more arguments to pass to
    it.
    """
    _exithandlers.append((func, targs, kargs))

import sys
try:
    x = sys.exitfunc
except AttributeError:
    sys.exitfunc = _run_exitfuncs
del sys

if __name__ == "__main__":
    def x1():
        print "running x1"
    def x2(n):
        print "running x2(%s)" % `n`
    def x3(n, kwd=None):
        print "running x3(%s, kwd=%s)" % (`n`, `kwd`)

    register_exitfunc(x1)
    register_exitfunc(x2, 12)
    register_exitfunc(x3, 5, "bar")
    register_exitfunc(x3, "no kwd args")