Mercurial > public > mercurial-scm > hg
comparison mercurial/lock.py @ 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 | e8564e04382d |
children | 14033c5dd261 |
comparison
equal
deleted
inserted
replaced
27796:f7f3958d39c0 | 27797:054abf2377e8 |
---|---|
55 self.postrelease = [] | 55 self.postrelease = [] |
56 self.pid = self._getpid() | 56 self.pid = self._getpid() |
57 self.delay = self.lock() | 57 self.delay = self.lock() |
58 if self.acquirefn: | 58 if self.acquirefn: |
59 self.acquirefn() | 59 self.acquirefn() |
60 | |
61 def __enter__(self): | |
62 return self | |
63 | |
64 def __exit__(self, exc_type, exc_value, exc_tb): | |
65 self.release() | |
60 | 66 |
61 def __del__(self): | 67 def __del__(self): |
62 if self.held: | 68 if self.held: |
63 warnings.warn("use lock.release instead of del lock", | 69 warnings.warn("use lock.release instead of del lock", |
64 category=DeprecationWarning, | 70 category=DeprecationWarning, |