Rev 5206: Make lock methods return objects rather than None-or-string. in http://bazaar.launchpad.net/~lifeless/bzr/lock_return

Robert Collins robertc at robertcollins.net
Tue May 11 09:35:49 BST 2010


At http://bazaar.launchpad.net/~lifeless/bzr/lock_return

------------------------------------------------------------
revno: 5206
revision-id: robertc at robertcollins.net-20100511083548-1dqx64ox360sd41d
parent: robertc at robertcollins.net-20100506235405-wii4elupfhzl3jvy
committer: Robert Collins <robertc at robertcollins.net>
branch nick: lock_return
timestamp: Tue 2010-05-11 20:35:48 +1200
message:
  Make lock methods return objects rather than None-or-string.
=== modified file 'NEWS'
--- a/NEWS	2010-05-06 23:49:39 +0000
+++ b/NEWS	2010-05-11 08:35:48 +0000
@@ -17,6 +17,8 @@
   ``Repository`` objects; they now return ``branch.BranchWriteLockResult``
   and ``repository.RepositoryWriteLockResult`` objects. This makes
   changing the API in future easier and permits some cleaner calling code.
+  The lock_read method has also changed from having no defined return
+  value to returning ``LogicalLockResult`` objects.
   (Robert Collins)
 
 New Features

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2010-05-06 23:54:05 +0000
+++ b/bzrlib/branch.py	2010-05-11 08:35:48 +0000
@@ -49,7 +49,7 @@
 from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
 from bzrlib.hooks import HookPoint, Hooks
 from bzrlib.inter import InterObject
-from bzrlib.lock import _RelockDebugMixin
+from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
 from bzrlib import registry
 from bzrlib.symbol_versioning import (
     deprecated_in,
@@ -295,8 +295,7 @@
     def lock_read(self):
         """Lock the branch for read operations.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         raise NotImplementedError(self.lock_read)
 
@@ -2276,7 +2275,7 @@
     _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
 
 
-class BranchWriteLockResult(object):
+class BranchWriteLockResult(LogicalLockResult):
     """The result of write locking a branch.
 
     :ivar branch_token: The token obtained from the underlying branch lock, or
@@ -2285,10 +2284,10 @@
     """
 
     def __init__(self, unlock, branch_token):
+        LogicalLockResult.__init__(self, unlock)
         self.branch_token = branch_token
-        self.unlock = unlock
 
-    def __str__(self):
+    def __repr__(self):
         return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
             self.unlock)
 
@@ -2379,8 +2378,7 @@
     def lock_read(self):
         """Lock the branch for read operations.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         if not self.is_locked():
             self._note_lock('r')
@@ -2394,7 +2392,7 @@
             took_lock = False
         try:
             self.control_files.lock_read()
-            return self
+            return LogicalLockResult(self.unlock)
         except:
             if took_lock:
                 self.repository.unlock()

=== modified file 'bzrlib/lock.py'
--- a/bzrlib/lock.py	2010-04-16 07:56:51 +0000
+++ b/bzrlib/lock.py	2010-05-11 08:35:48 +0000
@@ -88,6 +88,20 @@
                              self.lock_url, self.details)
 
 
+class LogicalLockResult(object):
+    """The result of a lock_read/lock_write/lock_tree_write call on lockables.
+
+    :ivar unlock: A callable which will unlock the lock.
+    """
+
+    def __init__(self, unlock):
+        self.unlock = unlock
+
+    def __repr__(self):
+        return "LogicalLockResult(%s)" % (self.unlock)
+
+
+
 def cant_unlock_not_held(locked_object):
     """An attempt to unlock failed because the object was not locked.
 

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2010-05-06 23:41:35 +0000
+++ b/bzrlib/remote.py	2010-05-11 08:35:48 +0000
@@ -1000,8 +1000,7 @@
     def lock_read(self):
         """Lock the repository for read operations.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         # wrong eventually - want a local lock cache context
         if not self._lock_mode:
@@ -1015,7 +1014,7 @@
                 repo.lock_read()
         else:
             self._lock_count += 1
-        return self
+        return lock.LogicalLockResult(self.unlock)
 
     def _remote_lock_write(self, token):
         path = self.bzrdir._path_for_remote_call(self._client)
@@ -2396,8 +2395,7 @@
     def lock_read(self):
         """Lock the branch for read operations.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         self.repository.lock_read()
         if not self._lock_mode:
@@ -2408,7 +2406,7 @@
                 self._real_branch.lock_read()
         else:
             self._lock_count += 1
-        return self
+        return lock.LogicalLockResult(self.unlock)
 
     def _remote_lock_write(self, token):
         if token is None:

=== modified file 'bzrlib/repofmt/pack_repo.py'
--- a/bzrlib/repofmt/pack_repo.py	2010-05-06 23:41:35 +0000
+++ b/bzrlib/repofmt/pack_repo.py	2010-05-11 08:35:48 +0000
@@ -64,6 +64,7 @@
     GraphIndex,
     InMemoryGraphIndex,
     )
+from bzrlib.lock import LogicalLockResult
 from bzrlib.repofmt.knitrepo import KnitRepository
 from bzrlib.repository import (
     CommitBuilder,
@@ -2341,6 +2342,10 @@
         return self._write_lock_count
 
     def lock_write(self, token=None):
+        """Lock the repository for writes.
+
+        :return: A bzrlib.repository.RepositoryWriteLockResult.
+        """
         locked = self.is_locked()
         if not self._write_lock_count and locked:
             raise errors.ReadOnlyError(self)
@@ -2358,6 +2363,10 @@
         return RepositoryWriteLockResult(self.unlock, None)
 
     def lock_read(self):
+        """Lock the repository for reads.
+
+        :return: A bzrlib.lock.LogicalLockResult.
+        """
         locked = self.is_locked()
         if self._write_lock_count:
             self._write_lock_count += 1
@@ -2370,7 +2379,7 @@
             for repo in self._fallback_repositories:
                 repo.lock_read()
             self._refresh_data()
-        return self
+        return LogicalLockResult(self.unlock)
 
     def leave_lock_in_place(self):
         # not supported - raise an error

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2010-05-06 23:54:05 +0000
+++ b/bzrlib/repository.py	2010-05-11 08:35:48 +0000
@@ -61,7 +61,7 @@
     ROOT_ID,
     entry_factory,
     )
-from bzrlib.lock import _RelockDebugMixin
+from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
 from bzrlib import registry
 from bzrlib.trace import (
     log_exception_quietly, note, mutter, mutter_callsite, warning)
@@ -860,7 +860,7 @@
         # versioned roots do not change unless the tree found a change.
 
 
-class RepositoryWriteLockResult(object):
+class RepositoryWriteLockResult(LogicalLockResult):
     """The result of write locking a repository.
 
     :ivar repository_token: The token obtained from the underlying lock, or
@@ -869,10 +869,10 @@
     """
 
     def __init__(self, unlock, repository_token):
+        LogicalLockResult.__init__(self, unlock)
         self.repository_token = repository_token
-        self.unlock = unlock
 
-    def __str__(self):
+    def __repr__(self):
         return "RepositoryWriteLockResult(%s, %s)" % (self.repository_token,
             self.unlock)
 
@@ -1434,7 +1434,7 @@
             for repo in self._fallback_repositories:
                 repo.lock_read()
             self._refresh_data()
-        return self
+        return LogicalLockResult(self.unlock)
 
     def get_physical_lock_status(self):
         return self.control_files.get_physical_lock_status()

=== modified file 'bzrlib/tree.py'
--- a/bzrlib/tree.py	2010-04-30 11:03:59 +0000
+++ b/bzrlib/tree.py	2010-05-11 08:35:48 +0000
@@ -582,6 +582,10 @@
             yield child.file_id
 
     def lock_read(self):
+        """Lock this tree for multiple read only operations.
+        
+        :return: A bzrlib.lock.LogicalLockResult.
+        """
         pass
 
     def revision_tree(self, revision_id):

=== modified file 'bzrlib/workingtree.py'
--- a/bzrlib/workingtree.py	2010-05-06 23:41:35 +0000
+++ b/bzrlib/workingtree.py	2010-05-11 08:35:48 +0000
@@ -77,6 +77,7 @@
 
 from bzrlib import symbol_versioning
 from bzrlib.decorators import needs_read_lock, needs_write_lock
+from bzrlib.lock import LogicalLockResult
 from bzrlib.lockable_files import LockableFiles
 from bzrlib.lockdir import LockDir
 import bzrlib.mutabletree
@@ -1802,15 +1803,14 @@
 
         This also locks the branch, and can be unlocked via self.unlock().
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         if not self.is_locked():
             self._reset_data()
         self.branch.lock_read()
         try:
             self._control_files.lock_read()
-            return self
+            return LogicalLockResult(self.unlock)
         except:
             self.branch.unlock()
             raise
@@ -1818,15 +1818,14 @@
     def lock_tree_write(self):
         """See MutableTree.lock_tree_write, and WorkingTree.unlock.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         if not self.is_locked():
             self._reset_data()
         self.branch.lock_read()
         try:
             self._control_files.lock_write()
-            return self
+            return LogicalLockResult(self.unlock)
         except:
             self.branch.unlock()
             raise
@@ -1834,15 +1833,14 @@
     def lock_write(self):
         """See MutableTree.lock_write, and WorkingTree.unlock.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         if not self.is_locked():
             self._reset_data()
         self.branch.lock_write()
         try:
             self._control_files.lock_write()
-            return self
+            return LogicalLockResult(self.unlock)
         except:
             self.branch.unlock()
             raise

=== modified file 'bzrlib/workingtree_4.py'
--- a/bzrlib/workingtree_4.py	2010-05-06 23:41:35 +0000
+++ b/bzrlib/workingtree_4.py	2010-05-11 08:35:48 +0000
@@ -53,6 +53,7 @@
 from bzrlib.decorators import needs_read_lock, needs_write_lock
 from bzrlib.filters import filtered_input_file, internal_size_sha_file_byname
 from bzrlib.inventory import Inventory, ROOT_ID, entry_factory
+from bzrlib.lock import LogicalLockResult
 from bzrlib.mutabletree import needs_tree_write_lock
 from bzrlib.osutils import (
     file_kind,
@@ -569,8 +570,7 @@
     def lock_read(self):
         """See Branch.lock_read, and WorkingTree.unlock.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         self.branch.lock_read()
         try:
@@ -590,7 +590,7 @@
         except:
             self.branch.unlock()
             raise
-        return self
+        return LogicalLockResult(self.unlock)
 
     def _lock_self_write(self):
         """This should be called after the branch is locked."""
@@ -611,13 +611,12 @@
         except:
             self.branch.unlock()
             raise
-        return self
+        return LogicalLockResult(self.unlock)
 
     def lock_tree_write(self):
         """See MutableTree.lock_tree_write, and WorkingTree.unlock.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         self.branch.lock_read()
         return self._lock_self_write()
@@ -625,8 +624,7 @@
     def lock_write(self):
         """See MutableTree.lock_write, and WorkingTree.unlock.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         self.branch.lock_write()
         return self._lock_self_write()
@@ -1896,8 +1894,7 @@
     def lock_read(self):
         """Lock the tree for a set of operations.
 
-        :return: An object with an unlock method which will release the lock
-            obtained.
+        :return: A bzrlib.lock.LogicalLockResult.
         """
         if not self._locked:
             self._repository.lock_read()
@@ -1905,7 +1902,7 @@
                 self._dirstate.lock_read()
                 self._dirstate_locked = True
         self._locked += 1
-        return self
+        return LogicalLockResult(self.unlock)
 
     def _must_be_locked(self):
         if not self._locked:




More information about the bazaar-commits mailing list