97
97
def __init__(self, filename):
98
98
# standard IO errors get exposed directly.
99
99
self._open(filename, 'rb+')
100
self.filename = realpath(filename)
101
if self.filename in self.open_locks:
103
raise LockContention("Lock already held.")
104
# reserve a slot for this lock - even if the lockf call fails,
105
# at thisi point unlock() will be called, because self.f is set.
106
# TODO: make this fully threadsafe, if we decide we care.
107
self.open_locks[self.filename] = self.filename
101
self.filename = realpath(filename)
102
if self.filename in self.open_locks:
104
raise LockContention("Lock already held.")
105
# reserve a slot for this lock - even if the lockf call fails,
106
# at thisi point unlock() will be called, because self.f is set.
107
# TODO: make this fully threadsafe, if we decide we care.
108
self.open_locks[self.filename] = self.filename
109
fcntl.lockf(self.f, fcntl.LOCK_EX)
109
# LOCK_NB will cause IOError to be raised if we can't grab a
111
fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
110
112
except IOError, e:
113
if e.errno in (errno.EAGAIN, errno.EACCES):
114
# We couldn't grab the lock
111
116
# we should be more precise about whats a locking
112
117
# error and whats a random-other error
113
118
raise LockError(e)
123
128
# standard IO errors get exposed directly.
124
129
self._open(filename, 'rb')
126
fcntl.lockf(self.f, fcntl.LOCK_SH)
131
# LOCK_NB will cause IOError to be raised if we can't grab a
133
fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
127
134
except IOError, e:
128
135
# we should be more precise about whats a locking
129
136
# error and whats a random-other error
207
215
class _msvc_ReadLock(_msvc_FileLock):
208
216
def __init__(self, filename):
209
_msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
217
_msvc_lock(self._open(filename, 'rb'),
218
self.LOCK_SH | self.LOCK_NB)
212
221
class _msvc_WriteLock(_msvc_FileLock):
213
222
def __init__(self, filename):
214
_msvc_lock(self._open(filename, 'rb+'), self.LOCK_EX)
223
_msvc_lock(self._open(filename, 'rb+'),
224
self.LOCK_EX | self.LOCK_NB)
217
227
def _msvc_lock(f, flags):