File: app_atexit.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (45 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
atexit_callbacks = []

def register(func, *args, **kwargs):
    """Register a function to be executed upon normal program termination.

    func - function to be called at exit
    args - optional arguments to pass to func
    kwargs - optional keyword arguments to pass to func

    func is returned to facilitate usage as a decorator."""

    if not callable(func):
        raise TypeError("func must be callable")

    atexit_callbacks.append((func, args, kwargs))
    return func

def run_exitfuncs():
    "Run all registered exit functions."
    # Maintain the last exception
    for (func, args, kwargs) in reversed(atexit_callbacks):
        if func is None:
            # unregistered slot
            continue
        try:
            func(*args, **kwargs)
        except BaseException as e:
            import __pypy__
            __pypy__.write_unraisable("in atexit callback", e, func)

    clear()

def clear():
    "Clear the list of previously registered exit functions."
    del atexit_callbacks[:]

def unregister(func):
    """Unregister a exit function which was previously registered using
    atexit.register"""
    for i, (f, _, _) in enumerate(atexit_callbacks):
        if f == func:
            atexit_callbacks[i] = (None, None, None)

def ncallbacks():
    return len(atexit_callbacks)