LockSet: invert try/for nesting in acquire()
authorGuido Trotter <ultrotter@google.com>
Tue, 4 Mar 2008 13:16:44 +0000 (13:16 +0000)
committerGuido Trotter <ultrotter@google.com>
Tue, 4 Mar 2008 13:16:44 +0000 (13:16 +0000)
This patch changes nothing to the functionality of a LockSet. Rather than
trying to do the whole for loop we try each of its steps. This opens the way to
handle differently a single failure.

Reviewed-by: imsnah

lib/locking.py

index 41519eb..6795379 100644 (file)
@@ -382,19 +382,19 @@ class LockSet:
     # First we look the locks up on __lockdict. We have no way of being sure
     # they will still be there after, but this makes it a lot faster should
     # just one of them be the already wrong
-    try:
-      for lname in names:
+    for lname in names:
+      try:
         lock = self.__lockdict[lname] # raises KeyError if the lock is not there
         acquire_list.append((lname, lock))
-    except (KeyError):
-      raise errors.LockError('non-existing lock in set (%s)' % lname)
+      except (KeyError):
+        raise errors.LockError('non-existing lock in set (%s)' % lname)
 
     # Now acquire_list contains a sorted list of resources and locks we want.
     # In order to get them we loop on this (private) list and acquire() them.
     # We gave no real guarantee they will still exist till this is done but
     # .acquire() itself is safe and will alert us if the lock gets deleted.
-    try:
-      for (lname, lock) in acquire_list:
+    for (lname, lock) in acquire_list:
+      try:
         lock.acquire(shared=shared) # raises LockError if the lock is deleted
         try:
           # now the lock cannot be deleted, we have it!
@@ -406,12 +406,12 @@ class LockSet:
           lock.release()
           raise
 
-    except (errors.LockError):
-      name_fail = lname
-      for lname in self._list_owned():
-        self.__lockdict[lname].release()
-        self._del_owned(lname)
-      raise errors.LockError('non-existing lock in set (%s)' % name_fail)
+      except (errors.LockError):
+        name_fail = lname
+        for lname in self._list_owned():
+          self.__lockdict[lname].release()
+          self._del_owned(lname)
+        raise errors.LockError('non-existing lock in set (%s)' % name_fail)
 
     return True