def atsuccess(func, *args, **kwds):

    def wrapper():
        if _success:
            func(*args, **kwds)
atexit(wrapper)

def set_success():
    global _success
_success = True
How can I register a function to be called only on *successful* exit of my Python program?