Import LockTimeout before trying to catch it.

If lock.acquire raised any sort of exception, a NameError would
trump it because the __enter__ method was trying to catch
lockfile.LockTimeout, which wasn't imported.

Signed-off-by: Tomaz Muraus <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/5aed65dd
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/5aed65dd
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/5aed65dd

Branch: refs/heads/trunk
Commit: 5aed65ddbe186333080829e1e6f3c9d634112613
Parents: 0e094bf
Author: briancurtin <[email protected]>
Authored: Thu Oct 24 23:39:16 2013 -0500
Committer: Tomaz Muraus <[email protected]>
Committed: Fri Oct 25 18:46:29 2013 +0200

----------------------------------------------------------------------
 libcloud/storage/drivers/local.py   |  4 ++--
 libcloud/test/storage/test_local.py | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/5aed65dd/libcloud/storage/drivers/local.py
----------------------------------------------------------------------
diff --git a/libcloud/storage/drivers/local.py 
b/libcloud/storage/drivers/local.py
index b8c20a1..fee53f7 100644
--- a/libcloud/storage/drivers/local.py
+++ b/libcloud/storage/drivers/local.py
@@ -25,7 +25,7 @@ import shutil
 import sys
 
 try:
-    from lockfile import mkdirlockfile
+    from lockfile import LockTimeout, mkdirlockfile
 except ImportError:
     raise ImportError('Missing lockfile dependency, you can install it ' \
                       'using pip: pip install lockfile')
@@ -57,7 +57,7 @@ class LockLocalStorage(object):
     def __enter__(self):
         try:
             self.lock.acquire(timeout=0.1)
-        except lockfile.LockTimeout:
+        except LockTimeout:
             raise LibcloudError('Lock timeout')
 
     def __exit__(self, type, value, traceback):

http://git-wip-us.apache.org/repos/asf/libcloud/blob/5aed65dd/libcloud/test/storage/test_local.py
----------------------------------------------------------------------
diff --git a/libcloud/test/storage/test_local.py 
b/libcloud/test/storage/test_local.py
index bb70f38..dfd0cde 100644
--- a/libcloud/test/storage/test_local.py
+++ b/libcloud/test/storage/test_local.py
@@ -21,6 +21,8 @@ import shutil
 import unittest
 import tempfile
 
+import mock
+
 from libcloud.utils.py3 import httplib
 
 from libcloud.common.types import InvalidCredsError
@@ -35,6 +37,8 @@ from libcloud.storage.types import ObjectHashMismatchError
 
 try:
     from libcloud.storage.drivers.local import LocalStorageDriver
+    from libcloud.storage.drivers.local import LockLocalStorage
+    from lockfile import LockTimeout
 except ImportError:
     print('lockfile library is not available, skipping local_storage tests...')
     LocalStorageDriver = None
@@ -318,6 +322,14 @@ class LocalTests(unittest.TestCase):
         container.delete()
         self.remove_tmp_file(tmppath)
 
+    @mock.patch("lockfile.mkdirlockfile.MkdirLockFile.acquire",
+                mock.MagicMock(side_effect=LockTimeout))
+    def test_proper_lockfile_imports(self):
+        # LockLocalStorage was previously using an un-imported exception
+        # in its __enter__ method, so the following would raise a NameError.
+        lls = LockLocalStorage("blah")
+        self.assertRaises(LibcloudError, lls.__enter__)
+
 
 if not LocalStorageDriver:
     class LocalTests(unittest.TestCase):

Reply via email to