lock: turn a lock into a Python context manager
authorBryan O'Sullivan <bryano@fb.com>
Fri, 15 Jan 2016 13:14:45 -0800
changeset 27797 054abf2377e8
parent 27796 f7f3958d39c0
child 27798 8953e963ce8c
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(): # ...
mercurial/lock.py
--- a/mercurial/lock.py	Fri Jan 15 13:14:50 2016 -0800
+++ b/mercurial/lock.py	Fri Jan 15 13:14:45 2016 -0800
@@ -58,6 +58,12 @@
         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",