bugfix: InterProcessLock.locked() was broken cause InterProcessLock.aquire() no longer raises SingleInstanceError

- fixed InterProcessLock.locked()
- fixed doctests

notes:

- doctests run ok on linux with InterProcessLockFcntl and InterprocessLockSocket
- doctests fail on linux/wine with InterprocessLockWin32 when trying to aquire the lock held by the process created on the fly.
   just a guess ..maybe the mutex is not released automatically when terminating the process. does native win32 guarantee
   this?
This commit is contained in:
fpdb-mme 2009-11-01 09:51:00 +01:00
parent bcbafc160d
commit 6fcf81e566

View File

@ -60,14 +60,10 @@ class InterProcessLockBase:
self._has_lock = False
def locked(self):
if self._has_lock:
return True
try:
self.acquire()
if self.acquire():
self.release()
return False
except SingleInstanceError:
return True
return True
LOCK_FILE_DIRECTORY = '/tmp'
@ -162,38 +158,33 @@ def test_construct():
>>> lock1 = InterProcessLock(name=test_name)
>>> lock1.acquire()
True
>>> lock2 = InterProcessLock(name=test_name)
>>> lock3 = InterProcessLock(name=test_name)
# Since lock1 is locked, other attempts to acquire it fail.
>>> lock2.acquire()
Traceback (most recent call last):
...
SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck
False
>>> lock3.acquire()
Traceback (most recent call last):
...
SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck
False
# Release the lock and let lock2 have it.
>>> lock1.release()
>>> lock2.acquire()
True
>>> lock3.acquire()
Traceback (most recent call last):
...
SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck
False
# Release it and give it back to lock1
>>> lock2.release()
>>> lock1.acquire()
True
>>> lock2.acquire()
Traceback (most recent call last):
...
SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck
False
# Test lock status
>>> lock2.locked()
@ -237,15 +228,14 @@ def test_construct():
>>> pid = execute('import interlocks;a=interlocks.InterProcessLock(name=\\''+test_name+ '\\');a.acquire();')
>>> lock1.acquire()
Traceback (most recent call last):
...
SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck
False
>>> os_independent_kill(pid)
>>> time.sleep(1)
>>> lock1.acquire()
True
>>> lock1.release()
# Testing wait
@ -253,13 +243,12 @@ def test_construct():
>>> pid = execute('import interlocks;a=interlocks.InterProcessLock(name=\\''+test_name+ '\\');a.acquire();')
>>> lock1.acquire()
Traceback (most recent call last):
...
SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck
False
>>> os_independent_kill(pid)
>>> lock1.acquire(True)
True
>>> lock1.release()
"""