Patchwork [2,of,4] lock: factor code to read lock into a separate function

login
register
mail settings
Submitter Siddharth Agarwal
Date Sept. 22, 2015, 4:33 a.m.
Message ID <9a99a53a7f7c818dd413.1442896395@dev6666.prn1.facebook.com>
Download mbox | patch
Permalink /patch/10567/
State Accepted
Headers show

Comments

Siddharth Agarwal - Sept. 22, 2015, 4:33 a.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1442456819 25200
#      Wed Sep 16 19:26:59 2015 -0700
# Node ID 9a99a53a7f7c818dd41370dcbd86d4e237f98552
# Parent  33a63670e1c5e4bb431ac1fe66839654eb5b3b06
lock: factor code to read lock into a separate function

This is going to be needed for upcoming work with lock inheritance.

Patch

diff --git a/mercurial/lock.py b/mercurial/lock.py
--- a/mercurial/lock.py
+++ b/mercurial/lock.py
@@ -100,6 +100,19 @@  class lock(object):
                     raise error.LockUnavailable(why.errno, why.strerror,
                                                 why.filename, self.desc)
 
+    def _readlock(self):
+        """read lock and return its value
+
+        Returns None if no lock exists, pid for old-style locks, and host:pid
+        for new-style locks.
+        """
+        try:
+            return self.vfs.readlock(self.f)
+        except (OSError, IOError) as why:
+            if why.errno == errno.ENOENT:
+                return None
+            raise
+
     def testlock(self):
         """return id of locker if lock is valid, else None.
 
@@ -111,12 +124,9 @@  class lock(object):
         The lock file is only deleted when None is returned.
 
         """
-        try:
-            locker = self.vfs.readlock(self.f)
-        except (OSError, IOError) as why:
-            if why.errno == errno.ENOENT:
-                return None
-            raise
+        locker = self._readlock()
+        if locker is None:
+            return None
         try:
             host, pid = locker.split(":", 1)
         except ValueError: