changeset 27797:054abf2377e8

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(): # ...
author Bryan O'Sullivan <bryano@fb.com>
date Fri, 15 Jan 2016 13:14:45 -0800
parents f7f3958d39c0
children 8953e963ce8c
files mercurial/lock.py
diffstat 1 files changed, 6 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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",