[Checkins] SVN: zc.lockfile/trunk/ Fixed: the fix included in 1.0.1 caused multiple pids to be written

jim cvs-admin at zope.org
Sun Dec 2 17:50:19 UTC 2012


Log message for revision 128475:
  Fixed: the fix included in 1.0.1 caused multiple pids to be written
  to the lock file
  
  Also, fixed the pid_in_lockfile test to work on windows, I hope...
  
  

Changed:
  U   zc.lockfile/trunk/CHANGES.txt
  U   zc.lockfile/trunk/buildout.cfg
  U   zc.lockfile/trunk/src/zc/lockfile/__init__.py
  U   zc.lockfile/trunk/src/zc/lockfile/tests.py

-=-
Modified: zc.lockfile/trunk/CHANGES.txt
===================================================================
--- zc.lockfile/trunk/CHANGES.txt	2012-12-02 17:29:04 UTC (rev 128474)
+++ zc.lockfile/trunk/CHANGES.txt	2012-12-02 17:50:19 UTC (rev 128475)
@@ -1,6 +1,12 @@
 Change History
 ***************
 
+1.0.2 (2012-12-02)
+==================
+
+- Fixed: the fix included in 1.0.1 caused multiple pids to be written
+  to the lock file
+
 1.0.1 (2012-11-30)
 ==================
 

Modified: zc.lockfile/trunk/buildout.cfg
===================================================================
--- zc.lockfile/trunk/buildout.cfg	2012-12-02 17:29:04 UTC (rev 128474)
+++ zc.lockfile/trunk/buildout.cfg	2012-12-02 17:50:19 UTC (rev 128475)
@@ -3,7 +3,7 @@
 parts = py
 
 [test]
-recipe = zc.recipe.testrunner
+recipe = zc.recipe.testrunner ==1.3.0
 eggs = zc.lockfile [test]
 
 [py]

Modified: zc.lockfile/trunk/src/zc/lockfile/__init__.py
===================================================================
--- zc.lockfile/trunk/src/zc/lockfile/__init__.py	2012-12-02 17:29:04 UTC (rev 128474)
+++ zc.lockfile/trunk/src/zc/lockfile/__init__.py	2012-12-02 17:50:19 UTC (rev 128475)
@@ -47,7 +47,7 @@
                 msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, 1)
             except IOError:
                 raise LockError("Couldn't unlock %r" % file.name)
-                
+
 else:
     # Unix
     _flags = fcntl.LOCK_EX | fcntl.LOCK_NB
@@ -57,8 +57,8 @@
             fcntl.flock(file.fileno(), _flags)
         except IOError:
             raise LockError("Couldn't lock %r" % file.name)
-            
 
+
     def _unlock_file(file):
         # File is automatically unlocked on close
         pass
@@ -70,9 +70,15 @@
 
     def __init__(self, path):
         self._path = path
-        # XXX this overwrites the pid info.  Should probably be r+.
-        # Need a test.
-        fp = open(path, 'a+')
+        try:
+            # Try to open for writing without truncation:
+            fp = open(path, 'r+')
+        except IOError:
+            # If the file doesn't exist, we'll get an IO error, try a+
+            # Note that there may be a race here. Multiple processes
+            # could fail on the r+ open and open the file a+, but only
+            # one will get the the lock and write a pid.
+            fp = open(path, 'a+')
 
         try:
             _lock_file(fp)

Modified: zc.lockfile/trunk/src/zc/lockfile/tests.py
===================================================================
--- zc.lockfile/trunk/src/zc/lockfile/tests.py	2012-12-02 17:29:04 UTC (rev 128474)
+++ zc.lockfile/trunk/src/zc/lockfile/tests.py	2012-12-02 17:50:19 UTC (rev 128475)
@@ -47,7 +47,15 @@
     True
 
     >>> os.remove('f')
+
+    We should only have one pid in the lock file:
+
+    >>> f = open('f.lock')
+    >>> len(f.read().strip().split())
+    1
+
     >>> os.remove('f.lock')
+
     """
 
 def pid_in_lockfile():
@@ -55,17 +63,26 @@
     >>> import os, zc.lockfile
     >>> pid = os.getpid()
     >>> lock = zc.lockfile.LockFile("f.lock")
-    >>> open("f.lock").read().strip() == str(pid)
+    >>> f = open("f.lock")
+    >>> f.seek(1)
+    >>> f.read().strip() == str(pid)
     True
+    >>> f.close()
 
     Make sure that locking twice does not overwrite the old pid:
-    
+
     >>> lock = zc.lockfile.LockFile("f.lock")
     Traceback (most recent call last):
       ...
     LockError: Couldn't lock 'f.lock'
-    >>> open("f.lock").read().strip() == str(pid)
+
+    >>> f = open("f.lock")
+    >>> f.seek(1)
+    >>> f.read().strip() == str(pid)
     True
+    >>> f.close()
+
+    >>> lock.close()
     """
 
 def test_suite():



More information about the checkins mailing list