atexit functionality

Robin Munn rmunn at pobox.com
Thu Apr 10 13:34:31 EDT 2003


viszneki at telerama.com <viszneki at telerama.com> wrote:
> I am not a member of the Python mailing list, I dont' have the time to read any 
> mailing lists at the moment. So if someone could please respond to me 
> personally, that would be great.
> 
> Generally speaking I have a lot of objects that clean themselves up before 
> python actually exits, so I was wondering if a deregister function could be 
> provided.
> 
> Rather than a deregister function, I thought about making the following 
> modification to atexit.py after _exithandlers.append((func,targs,kargs))
> 
> return _exithandlers[len(_exithandlers)]
> 
> My intention is to be able to store the returned value as a member variable in 
> a class that registers its own cleanup functions. But if the class cleans 
> itself up, the class could "clean up" its cleanup registration with atexit by 
> saying something like del self.cleanup

You shouldn't need to return the just-inserted tuple. The caller already
has a reference to the function that was just registered -- it has to
have that reference, since it just passed that reference in as a
parameter! So you could write an "unregister" function like so:

    def unregister(func):
    """Unregister a function from the exit-handlers list.
    
    func - function to be unregistered.
    """
    for i in range(len(_exithandlers)):
        if _exithandlers[i][0] is func:
            del _exithandlers[i]
            break

Notice that I used the "is" operator to check for object identity.

If you think this is useful, feel free to prepare a patch and submit it
to the python-dev list for consideration. Be prepared to back it up with
real use cases demonstrating why this should be added, though. One of
the reasons Python is still so clean is that Guido won't make changes
without good, compelling reasons. This has (for the most part) avoided a
lot of cruft getting into the language.

Carefully-avoiding-mentioning-ternary-operators-ly y'rs, (oops, too late)

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list