# HG changeset patch # User Bryan O'Sullivan # Date 2016-01-15 21:14:45 # Node ID 054abf2377e806d9ba51130dd3f9db2670d8bdf5 # Parent f7f3958d39c037be5f3a13e8037aebd481c15c50 lock: turn a lock into a Python context manager This lets us greatly simply acquire/release cycles. Code pattern before: try: lock = repo.lock() # zillions of lines of code finally: lock.release() And after: with repo.lock(): # ... diff --git a/mercurial/lock.py b/mercurial/lock.py --- a/mercurial/lock.py +++ b/mercurial/lock.py @@ -58,6 +58,12 @@ class lock(object): if self.acquirefn: self.acquirefn() + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_tb): + self.release() + def __del__(self): if self.held: warnings.warn("use lock.release instead of del lock",