[merge] CountedLock class

John Arbash Meinel john at arbash-meinel.com
Thu May 3 20:00:42 BST 2007


John Arbash Meinel has voted +1.
Status is now: Approved
Comment:
+    def lock_read(self):
+        """Acquire the lock in read mode.
+
+        If the lock is already held in either read or write mode this
+        increments the count and succeeds.  If the lock is not already 
held,
+        it is taken in read mode.
+        """
+        if self._lock_mode:
+            assert self._lock_mode in ('r', 'w'), \
+                   "invalid lock mode %r" % self._lock_mode

Should this be an assert or a InvalidLockMode() error.

I'm not strict, since 'r', and 'w', are really all it could be. Because 
'_lock_mode' is a single character, you can also use "assert self._lock 
mode in 'rw'", but I think ('r', 'w') is probably a bigger win for 
readability.

For people following, I don't think it matters here, but "x in 'rw'" is 
slightly faster than "x in ('r', 'w')".

juju % python -m timeit "'r' in 'rw'"
1000000 loops, best of 3: 0.587 usec per loop
juju % python -m timeit "'w' in 'rw'"
1000000 loops, best of 3: 0.591 usec per loop
juju % python -m timeit "'r' in ('r', 'w')"
1000000 loops, best of 3: 0.588 usec per loop
juju % python -m timeit "'w' in ('r', 'w')"
1000000 loops, best of 3: 0.749 usec per loop

It doesn't matter much here (I think), but that is why WT4._iter_changes 
uses the "if minikind in 'ar'" sort of checks.


For details, see: 
http://bundlebuggy.aaronbentley.com/request/%3Ce01316480705022335od933d85yabaa2f4de11c5a3c%40mail.gmail.com%3E



More information about the bazaar mailing list