Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1013 / extensions / extScripting / scripts / jython / Lib / atexit.py @ 13521

History | View | Annotate | Download (1.34 KB)

1 5782 jmvivo
"""
2
atexit.py - allow programmer to define multiple exit functions to be executed
3
upon normal program termination.
4

5
One public function, register, is defined.
6
"""
7
8
__all__ = ["register"]
9
10
_exithandlers = []
11
def _run_exitfuncs():
12
    """run any registered exit functions
13

14
    _exithandlers is traversed in reverse order so functions are executed
15
    last in, first out.
16
    """
17
18
    while _exithandlers:
19
        func, targs, kargs = _exithandlers.pop()
20
        apply(func, targs, kargs)
21
22
def register(func, *targs, **kargs):
23
    """register a function to be executed upon normal program termination
24

25
    func - function to be called at exit
26
    targs - optional arguments to pass to func
27
    kargs - optional keyword arguments to pass to func
28
    """
29
    _exithandlers.append((func, targs, kargs))
30
31
import sys
32
try:
33
    x = sys.exitfunc
34
except AttributeError:
35
    sys.exitfunc = _run_exitfuncs
36
else:
37
    # if x isn't our own exit func executive, assume it's another
38
    # registered exit function - append it to our list...
39
    if x != _run_exitfuncs:
40
        register(x)
41
del sys
42
43
if __name__ == "__main__":
44
    def x1():
45
        print "running x1"
46
    def x2(n):
47
        print "running x2(%s)" % `n`
48
    def x3(n, kwd=None):
49
        print "running x3(%s, kwd=%s)" % (`n`, `kwd`)
50
51
    register(x1)
52
    register(x2, 12)
53
    register(x3, 5, "bar")
54
    register(x3, "no kwd args")