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():
# ...
--- 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",