Jason R. Coombs avatar Jason R. Coombs committed 4c8cef4

Move error message handling into the LockError and UnlockError classes.

Comments (0)

Files changed (1)

cherrypy/lib/lockfile.py

 except ImportError:
     pass
 
+
 class LockError(Exception):
     "Could not obtain a lock"
 
+    msg = "Unable to lock %r"
+
+    def __init__(self, path):
+        super(LockError, self).__init__(self.msg % path)
+
+
+class UnlockError(LockError):
+    "Could not release a lock"
+
+    msg = "Unable to unlock %r"
+
+
 # first, a default, naive locking implementation
 class LockFile(object):
     """
     A default, naive locking implementation. Always fails if the file
     already exists.
     """
+
     def __init__(self, path):
         self.path = path
         try:
             fd = os.open(path, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
         except OSError:
-            raise LockError("Unable to lock %s" % self.path)
+            raise LockError(self.path)
         os.close(fd)
 
     def release(self):
         try:
             msvcrt.locking(self.fp.fileno(), msvcrt.LK_NBLCK, 1)
         except IOError:
-            raise LockError("Unable to lock %r" % self.fp.name)
+            raise LockError(self.fp.name)
 
     def _unlock_file(self):
         try:
             self.fp.seek(0)
             msvcrt.locking(self.fp.fileno(), msvcrt.LK_UNLCK, 1)
         except IOError:
-            raise LockError("Unable to unlock %r" % self.fp.name)
+            raise UnlockError(self.fp.name)
 
 if 'msvcrt' in globals():
     LockFile = WindowsLockFile
         try:
             fcntl.flock(self.fp.fileno(), flags)
         except IOError:
-            raise LockError("Unable to lock %r" % self.fp.name)
+            raise LockError(self.fp.name)
 
     # no need to implement _unlock_file, it will be unlocked on close()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.