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

Andrew Bennetts andrew.bennetts at canonical.com
Wed Oct 8 07:45:59 BST 2008


Michael Hudson wrote:
> Andrew Bennetts wrote:
> 
> > So, feedback sought.
> 
> I guess it's not the most practical thought, but it does seem to me that
> this is a neat use case for Python 2.5's 'with' blocks -- you ought to
> be able to define an object such that:
> 
>    with write_locked(branch):
>        ...
> 
> DTRT.  A thought for the future, perhaps?

Yes.  It looks pretty easy to write a context manager that DTRT in this
case.  At a glance its __exit__ hook would simply be:

   def __exit__(exc_type, exc_val, exc_tb):
       try:
           self.thing.unlock()
       finally:
           return False

Unfortunately we're still supporting Python 2.4.  AFAIK there are no
plans to change that.

That said, it's not immediately obvious that 'with' blocks would make
the fix to commit.py any neater, although it would certainly help
simpler cases like bzrlib/tag.py.

-Andrew.




More information about the bazaar mailing list