[Pkg-bazaar-commits] ./bzr/unstable r610: - replace Branch.lock(mode) with separate lock_read and lock_write

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:20:17 UTC 2009


------------------------------------------------------------
revno: 610
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Tue 2005-05-31 18:10:44 +1000
message:
  - replace Branch.lock(mode) with separate lock_read and lock_write 
    methods
modified:
  bzrlib/branch.py
  bzrlib/commands.py
  bzrlib/commit.py
  bzrlib/errors.py
  bzrlib/status.py
-------------- next part --------------
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2005-05-30 06:45:30 +0000
+++ b/bzrlib/branch.py	2005-05-31 08:10:44 +0000
@@ -50,7 +50,7 @@
     """Method decorator for functions run with the branch locked."""
     def d(self, *a, **k):
         # called with self set to the branch
-        self.lock('w')
+        self.lock_write()
         try:
             return method(self, *a, **k)
         finally:
@@ -60,7 +60,7 @@
 
 def with_readlock(method):
     def d(self, *a, **k):
-        self.lock('r')
+        self.lock_read()
         try:
             return method(self, *a, **k)
         finally:
@@ -200,30 +200,41 @@
             self.unlock()
 
 
-    def lock(self, mode):
-        if self._lock_mode:
-            if mode == 'w' and cur_lm == 'r':
-                raise BzrError("can't upgrade to a write lock")
+
+    def lock_write(self):
+        if self._lock_mode:
+            if self._lock_mode != 'w':
+                from errors import LockError
+                raise LockError("can't upgrade to a write lock from %r" %
+                                self._lock_mode)
+            self._lock_count += 1
+        else:
+            from bzrlib.lock import lock, LOCK_EX
+
+            lock(self._lockfile, LOCK_EX)
+            self._lock_mode = 'w'
+            self._lock_count = 1
+
+
+
+    def lock_read(self):
+        if self._lock_mode:
+            assert self._lock_mode in ('r', 'w'), \
+                   "invalid lock mode %r" % self._lock_mode
+            self._lock_count += 1
+        else:
+            from bzrlib.lock import lock, LOCK_SH
+
+            lock(self._lockfile, LOCK_SH)
+            self._lock_mode = 'r'
+            self._lock_count = 1
+                        
+
             
-            assert self._lock_count >= 1
-            self._lock_count += 1
-        else:
-            from bzrlib.lock import lock, LOCK_SH, LOCK_EX
-            if mode == 'r':
-                m = LOCK_SH
-            elif mode == 'w':
-                m = LOCK_EX
-            else:
-                raise ValueError('invalid lock mode %r' % mode)
-
-            lock(self._lockfile, m)
-            self._lock_mode = mode
-            self._lock_count = 1
-
-
     def unlock(self):
         if not self._lock_mode:
-            raise BzrError('branch %r is not locked' % (self))
+            from errors import LockError
+            raise LockError('branch %r is not locked' % (self))
 
         if self._lock_count > 1:
             self._lock_count -= 1
@@ -794,8 +805,9 @@
     def destroy(self):
         """Destroy the test branch, removing the scratch directory."""
         try:
-            mutter("delete ScratchBranch %s" % self.base)
-            shutil.rmtree(self.base)
+            if self.base:
+                mutter("delete ScratchBranch %s" % self.base)
+                shutil.rmtree(self.base)
         except OSError, e:
             # Work around for shutil.rmtree failing on Windows when
             # readonly files are encountered

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-05-31 07:42:44 +0000
+++ b/bzrlib/commands.py	2005-05-31 08:10:44 +0000
@@ -675,7 +675,10 @@
         assert len(revision) == 2
 
         mutter('encoding log as %r' % bzrlib.user_encoding)
-        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout)
+
+        # use 'replace' so that we don't abort if trying to write out
+        # in e.g. the default C locale.
+        outf = codecs.getwriter(bzrlib.user_encoding)(sys.stdout, errors='replace')
 
         show_log(b, file_id,
                  show_timezone=timezone,

=== modified file 'bzrlib/commit.py'
--- a/bzrlib/commit.py	2005-05-30 01:37:52 +0000
+++ b/bzrlib/commit.py	2005-05-31 08:10:44 +0000
@@ -64,7 +64,7 @@
     from revision import Revision
     from trace import mutter, note
 
-    branch.lock('w')
+    branch.lock_write()
 
     try:
         # First walk over the working inventory; and both update that

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2005-05-30 05:10:41 +0000
+++ b/bzrlib/errors.py	2005-05-31 08:10:44 +0000
@@ -51,6 +51,10 @@
     pass
 
 
+class LockError(BzrError):
+    pass
+
+
 def bailout(msg, explanation=[]):
     ex = BzrError(msg, explanation)
     import trace

=== modified file 'bzrlib/status.py'
--- a/bzrlib/status.py	2005-05-30 01:37:52 +0000
+++ b/bzrlib/status.py	2005-05-31 08:10:44 +0000
@@ -30,7 +30,7 @@
     import sys
     from bzrlib.diff import compare_trees
     
-    branch.lock('r')
+    branch.lock_read()
     try:
 
         old = branch.basis_tree()



More information about the Pkg-bazaar-commits mailing list