[RFC/MERGE][#243391] _run_and_cleanup helper (alternative to try/finally)

John Arbash Meinel john at arbash-meinel.com
Wed Oct 8 14:13:39 BST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aaron Bentley wrote:
> Andrew Bennetts wrote:
>> Hi all,
> 
>> This patch is an extension of my fix for bug 230902 at
>> <http://bundlebuggy.aaronbentley.com/request/%3C20081008054310.GC19754@steerpike.home.puzzling.org%3E>.
> 
>> I'm looking for opinions on the API defined by the attached patch.
> 
> It looks sensible, but I wonder whether it goes far enough.
> 
> There are many situations where I want to run a method with multiple
> cleanups, i.e. two tree.unlocks.  Either I wind up with deep-nested
> try/finally blocks, or I define a stack of cleanups to run.
> 
> So I'm thinking about an object like:
> 
> class CleanupHandler(object):
>     @classmethod
>     def run_and_cleanup(klass, cleanup, kallable, *args, **kwargs):
>         handler = klass()
>         handler.add_cleanup(cleanup)
>         handler.run(kallable, *args, **kwargs)
> 
>     def __init__(self):
>         self.cleanups = []
> 
>     def add_cleanup(self, cleanup):
>         self.cleanups.append(cleanup)
> 
>     def cleanup(self):
>         for cleanup in reversed(self.cleanups):
>             cleanup()

^- Wouldn't you want something here around the cleanup() functions so
that if one fails it still calls the others?

Another thing to consider, why are our cleanup functions raising
exceptions? I believe in C++ if you raise an exception while an
exception is being propagated, you get a segfault. And I believe ~Foo()
is defined as unable to raise an exception.

Perhaps we should consider pushing harder on preventing a second
exception. As reliably handling the various ways that you can get
multiple exceptions seems difficult at best.

Anyway, the CleanupHandler class seems fine, though I don't know if I
would make it a classmethod. Mostly because of scope. But I suppose if
we want to have people creating custom cleanup classes, it may make it
easier for inheritance.

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkjssgMACgkQJdeBCYSNAAOt9gCfWphX3XJdPDR0JQB+B7vsDss3
xyUAoJRXtUw7RElprA6a++8ajsIr2IWK
=fSgJ
-----END PGP SIGNATURE-----



More information about the bazaar mailing list