[Zodb-checkins] SVN: ZODB/branches/jim-zeo-blob/src/ZODB/lock_file.py Updated to provide a catchable exception.

Jim Fulton jim at zope.com
Wed May 16 13:30:49 EDT 2007


Log message for revision 75808:
  Updated to provide a catchable exception.
  

Changed:
  U   ZODB/branches/jim-zeo-blob/src/ZODB/lock_file.py

-=-
Modified: ZODB/branches/jim-zeo-blob/src/ZODB/lock_file.py
===================================================================
--- ZODB/branches/jim-zeo-blob/src/ZODB/lock_file.py	2007-05-16 17:12:54 UTC (rev 75807)
+++ ZODB/branches/jim-zeo-blob/src/ZODB/lock_file.py	2007-05-16 17:30:48 UTC (rev 75808)
@@ -17,31 +17,49 @@
 import logging
 logger = logging.getLogger("ZODB.lock_file")
 
+class LockError(Exception):
+    """Couldn't lock a file
+    """
+
 try:
     import fcntl
 except ImportError:
     try:
-        from winlock import LockFile as _LockFile
-        from winlock import UnlockFile as _UnlockFile
+        import ZODB.winlock
     except ImportError:
-        def lock_file(file):
-            logger.info('No file-locking support on this platform')
+        def _lock_file(file):
+            raise TypeError('No file-locking support on this platform')
+        def _unlock_file(file):
+            raise TypeError('No file-locking support on this platform')
 
-    # Windows
-    def lock_file(file):
-        # Lock just the first byte
-        _LockFile(file.fileno(), 0, 0, 1, 0)
+    else:
+        # Windows
+        def _lock_file(file):
+            # Lock just the first byte
+            try:
+                ZODB.winlock.LockFile(file.fileno())
+            except ZODB.winlock.LockError:
+                raise LockError("Couldn't lock %r", file.name)
+            
 
-    def unlock_file(file):
-        _UnlockFile(file.fileno(), 0, 0, 1, 0)
+        def _unlock_file(file):
+            try:
+                ZODB.winlock.UnlockFile(file.fileno())
+            except ZODB.winlock.LockError:
+                raise LockError("Couldn't unlock %r", file.name)
+                
 else:
     # Unix
     _flags = fcntl.LOCK_EX | fcntl.LOCK_NB
 
-    def lock_file(file):
-        fcntl.flock(file.fileno(), _flags)
+    def _lock_file(file):
+        try:
+            fcntl.flock(file.fileno(), _flags)
+        except IOError:
+            raise LockError("Couldn't lock %r", file.name)
+            
 
-    def unlock_file(file):
+    def _unlock_file(file):
         # File is automatically unlocked on close
         pass
 
@@ -51,6 +69,7 @@
 # Creating the instance acquires the lock.  The file remains open.  Calling
 # close both closes and unlocks the lock file.
 class LockFile:
+
     def __init__(self, path):
         self._path = path
         try:
@@ -60,7 +79,7 @@
             self._fp = open(path, 'w+')
         # Acquire the lock and piss on the hydrant
         try:
-            lock_file(self._fp)
+            _lock_file(self._fp)
         except:
             logger.exception("Error locking file %s", path)
             raise
@@ -69,7 +88,7 @@
 
     def close(self):
         if self._fp is not None:
-            unlock_file(self._fp)
+            _unlock_file(self._fp)
             self._fp.close()
             os.unlink(self._path)
             self._fp = None



More information about the Zodb-checkins mailing list