Mercurial > public > mercurial-scm > hg-stable
diff mercurial/lock.py @ 26473:5f94e64f182c
lock: turn prepinherit/reacquire into a single context manager
Review feedback from Pierre-Yves David. This makes the overall code cleaner and
less error-prone, and makes a previously explicitly checked error state
impossible.
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Sun, 04 Oct 2015 20:02:50 -0700 |
parents | e16f80f89a29 |
children | 431094a3b21f |
line wrap: on
line diff
--- a/mercurial/lock.py Sun Oct 04 19:28:43 2015 -0700 +++ b/mercurial/lock.py Sun Oct 04 20:02:50 2015 -0700 @@ -7,6 +7,7 @@ from __future__ import absolute_import +import contextlib import errno import os import socket @@ -171,19 +172,20 @@ locker = self._readlock() return self._testlock(locker) - def prepinherit(self): - """prepare for the lock to be inherited by a Mercurial subprocess + @contextlib.contextmanager + def inherit(self): + """context for the lock to be inherited by a Mercurial subprocess. - Returns a string that will be recognized by the lock in the - subprocess. Communicating this string to the subprocess needs to be done - separately -- typically by an environment variable. + Yields a string that will be recognized by the lock in the subprocess. + Communicating this string to the subprocess needs to be done separately + -- typically by an environment variable. """ if not self.held: raise error.LockInheritanceContractViolation( - 'prepinherit can only be called while lock is held') + 'inherit can only be called while lock is held') if self._inherited: raise error.LockInheritanceContractViolation( - 'prepinherit cannot be called while lock is already inherited') + 'inherit cannot be called while lock is already inherited') if self.releasefn: self.releasefn() if self._parentheld: @@ -191,15 +193,12 @@ else: lockname = '%s:%s' % (lock._host, self.pid) self._inherited = True - return lockname - - def reacquire(self): - if not self._inherited: - raise error.LockInheritanceContractViolation( - 'reacquire can only be called after prepinherit') - if self.acquirefn: - self.acquirefn() - self._inherited = False + try: + yield lockname + finally: + if self.acquirefn: + self.acquirefn() + self._inherited = False def release(self): """release the lock and execute callback function if any