Mercurial > hg
diff mercurial/lock.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | ead71b15efd5 |
children | 687b865b95ad |
line wrap: on
line diff
--- a/mercurial/lock.py Sat Oct 05 10:29:34 2019 -0400 +++ b/mercurial/lock.py Sun Oct 06 09:45:02 2019 -0400 @@ -24,9 +24,8 @@ util, ) -from .utils import ( - procutil, -) +from .utils import procutil + def _getlockprefix(): """Return a string which is used to differentiate pid namespaces @@ -44,6 +43,7 @@ raise return result + @contextlib.contextmanager def _delayedinterrupt(): """Block signal interrupt while doing something critical @@ -60,11 +60,13 @@ orighandlers = {} def raiseinterrupt(num): - if (num == getattr(signal, 'SIGINT', None) or - num == getattr(signal, 'CTRL_C_EVENT', None)): + if num == getattr(signal, 'SIGINT', None) or num == getattr( + signal, 'CTRL_C_EVENT', None + ): raise KeyboardInterrupt else: raise error.SignalInterrupt + def catchterm(num, frame): if blocked: assertedsigs.append(num) @@ -82,7 +84,7 @@ for num in orighandlers: signal.signal(num, catchterm) except ValueError: - pass # in a thread? no luck + pass # in a thread? no luck blocked = True yield @@ -95,13 +97,14 @@ for num, handler in orighandlers.items(): signal.signal(num, handler) except ValueError: - pass # in a thread? + pass # in a thread? # re-raise interrupt exception if any, which may be shadowed by a new # interrupt occurred while re-raising the first one if assertedsigs: raiseinterrupt(assertedsigs[0]) + def trylock(ui, vfs, lockname, timeout, warntimeout, *args, **kwargs): """return an acquired lock or raise an a LockHeld exception @@ -113,12 +116,18 @@ # show more details for new-style locks if ':' in locker: host, pid = locker.split(":", 1) - msg = (_("waiting for lock on %s held by process %r on host %r\n") - % (pycompat.bytestr(l.desc), pycompat.bytestr(pid), - pycompat.bytestr(host))) + msg = _( + "waiting for lock on %s held by process %r on host %r\n" + ) % ( + pycompat.bytestr(l.desc), + pycompat.bytestr(pid), + pycompat.bytestr(host), + ) else: - msg = (_("waiting for lock on %s held by %r\n") - % (l.desc, pycompat.bytestr(locker))) + msg = _("waiting for lock on %s held by %r\n") % ( + l.desc, + pycompat.bytestr(locker), + ) printer(msg) l = lock(vfs, lockname, 0, *args, dolock=False, **kwargs) @@ -141,8 +150,9 @@ if delay == warningidx: printwarning(ui.warn, inst.locker) if timeout <= delay: - raise error.LockHeld(errno.ETIMEDOUT, inst.filename, - l.desc, inst.locker) + raise error.LockHeld( + errno.ETIMEDOUT, inst.filename, l.desc, inst.locker + ) time.sleep(1) delay += 1 @@ -156,6 +166,7 @@ l.acquirefn() return l + class lock(object): '''An advisory lock held by one process to control access to a set of files. Non-cooperating processes or incorrectly written scripts @@ -176,9 +187,19 @@ _host = None - def __init__(self, vfs, fname, timeout=-1, releasefn=None, acquirefn=None, - desc=None, inheritchecker=None, parentlock=None, - signalsafe=True, dolock=True): + def __init__( + self, + vfs, + fname, + timeout=-1, + releasefn=None, + acquirefn=None, + desc=None, + inheritchecker=None, + parentlock=None, + signalsafe=True, + dolock=True, + ): self.vfs = vfs self.f = fname self.held = 0 @@ -194,7 +215,7 @@ self._maybedelayedinterrupt = _delayedinterrupt else: self._maybedelayedinterrupt = util.nullcontextmanager - self.postrelease = [] + self.postrelease = [] self.pid = self._getpid() if dolock: self.delay = self.lock() @@ -209,9 +230,11 @@ def __del__(self): if self.held: - warnings.warn(r"use lock.release instead of del lock", - category=DeprecationWarning, - stacklevel=2) + warnings.warn( + r"use lock.release instead of del lock", + category=DeprecationWarning, + stacklevel=2, + ) # ensure the lock will be removed # even if recursive locking did occur @@ -235,8 +258,9 @@ if timeout > 0: timeout -= 1 continue - raise error.LockHeld(errno.ETIMEDOUT, inst.filename, self.desc, - inst.locker) + raise error.LockHeld( + errno.ETIMEDOUT, inst.filename, self.desc, inst.locker + ) def _trylock(self): if self.held: @@ -268,18 +292,23 @@ return locker = self._testlock(locker) if locker is not None: - raise error.LockHeld(errno.EAGAIN, - self.vfs.join(self.f), self.desc, - locker) + raise error.LockHeld( + errno.EAGAIN, + self.vfs.join(self.f), + self.desc, + locker, + ) else: - raise error.LockUnavailable(why.errno, why.strerror, - why.filename, self.desc) + raise error.LockUnavailable( + why.errno, why.strerror, why.filename, self.desc + ) if not self.held: # use empty locker to mean "busy for frequent lock/unlock # by many processes" - raise error.LockHeld(errno.EAGAIN, - self.vfs.join(self.f), self.desc, "") + raise error.LockHeld( + errno.EAGAIN, self.vfs.join(self.f), self.desc, "" + ) def _readlock(self): """read lock and return its value @@ -342,10 +371,12 @@ """ if not self.held: raise error.LockInheritanceContractViolation( - 'inherit can only be called while lock is held') + 'inherit can only be called while lock is held' + ) if self._inherited: raise error.LockInheritanceContractViolation( - 'inherit cannot be called while lock is already inherited') + 'inherit cannot be called while lock is already inherited' + ) if self._inheritchecker is not None: self._inheritchecker() if self.releasefn: @@ -391,6 +422,7 @@ # Prevent double usage and help clear cycles. self.postrelease = None + def release(*locks): for lock in locks: if lock is not None: