[Zope-Checkins] CVS: Zope/lib/python/Zope/Startup/misc - lock_file.py:1.2.2.1

Fred L. Drake, Jr. fred@zope.com
Tue, 25 Feb 2003 11:30:39 -0500


Update of /cvs-repository/Zope/lib/python/Zope/Startup/misc
In directory cvs.zope.org:/tmp/cvs-serv2191

Modified Files:
      Tag: new-install-branch
	lock_file.py 
Log Message:
- minimize the code in the try: clauses
- name the exceptions we're checking for
- simplify the Unix version of lock_file(): fcntl.flock() can take
  file objects directly in Python 2.2 and newer


=== Zope/lib/python/Zope/Startup/misc/lock_file.py 1.2 => 1.2.2.1 ===
--- Zope/lib/python/Zope/Startup/misc/lock_file.py:1.2	Wed Jan 29 15:25:17 2003
+++ Zope/lib/python/Zope/Startup/misc/lock_file.py	Tue Feb 25 11:30:39 2003
@@ -8,23 +8,33 @@
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
+# FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-""" Utilities for file locking """
+
+"""Utility function for file locking.
+
+This module provides a platform-specific function which uses the
+best-available strategy for locking a file object.
+"""
+
 try:
     import fcntl
-    def lock_file(file):
-        un=file.fileno()
-        fcntl.flock(un, fcntl.LOCK_EX | fcntl.LOCK_NB)
-except:
+except ImportError:
     # Try windows-specific code:
     try:
+        # We expect this module to exist, but the LockFile function may not.
         from ZODB.winlock import LockFile
-        def lock_file(file):
-            un=file.fileno()
-            LockFile(un,0,0,1,0) # just lock the first byte, who cares
-    except:
+    except ImportError:
         # we don't understand any kind of locking, forget it
         def lock_file(file):
             pass
+    else:
+        # Windows
+        def lock_file(file):
+            un = file.fileno()
+            LockFile(un, 0, 0, 1, 0) # just lock the first byte, who cares
+else:
+    # Unix-specific locking:
+    def lock_file(file):
+        fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)