~bzr-pqm/bzr/bzr.dev

2353.3.8 by John Arbash Meinel
Fix Copyright in lock.py
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
577 by Martin Pool
- merge portable lock module from John
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
577 by Martin Pool
- merge portable lock module from John
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
577 by Martin Pool
- merge portable lock module from John
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
1553.5.39 by Martin Pool
More lock docs
18
"""Locking using OS file locks or file existence.
577 by Martin Pool
- merge portable lock module from John
19
1553.5.46 by Martin Pool
doc
20
Note: This method of locking is generally deprecated in favour of LockDir, but
21
is used to lock local WorkingTrees, and by some old formats.  It's accessed
22
through Transport.lock_read(), etc.
577 by Martin Pool
- merge portable lock module from John
23
24
This module causes two methods, lock() and unlock() to be defined in
25
any way that works on the current platform.
26
27
It is not specified whether these locks are reentrant (i.e. can be
28
taken repeatedly by a single process) or whether they exclude
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
29
different threads in a single process.  That reentrancy is provided by
1553.5.39 by Martin Pool
More lock docs
30
LockableFiles.
615 by Martin Pool
Major rework of locking code:
31
32
This defines two classes: ReadLock and WriteLock, which can be
33
implemented in different ways on different platforms.  Both have an
34
unlock() method.
614 by Martin Pool
- unify two defintions of LockError
35
"""
577 by Martin Pool
- merge portable lock module from John
36
1185.65.29 by Robert Collins
Implement final review suggestions.
37
import errno
577 by Martin Pool
- merge portable lock module from John
38
2353.3.11 by John Arbash Meinel
Code cleanup
39
from bzrlib import (
40
    errors,
41
    osutils,
42
    trace,
43
    )
1711.8.1 by John Arbash Meinel
Branch.lock_read/lock_write/unlock should handle failures
44
577 by Martin Pool
- merge portable lock module from John
45
615 by Martin Pool
Major rework of locking code:
46
class _base_Lock(object):
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
47
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
48
    def __init__(self):
49
        self.f = None
2353.3.10 by John Arbash Meinel
Cleanup errors, and change ReadOnlyLockError to pass around more details.
50
        self.filename = None
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
51
615 by Martin Pool
Major rework of locking code:
52
    def _open(self, filename, filemode):
2353.3.11 by John Arbash Meinel
Code cleanup
53
        self.filename = osutils.realpath(filename)
656 by Martin Pool
- create branch lock files if they don't exist
54
        try:
2353.3.10 by John Arbash Meinel
Cleanup errors, and change ReadOnlyLockError to pass around more details.
55
            self.f = open(self.filename, filemode)
656 by Martin Pool
- create branch lock files if they don't exist
56
            return self.f
57
        except IOError, e:
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
58
            if e.errno in (errno.EACCES, errno.EPERM):
2353.3.10 by John Arbash Meinel
Cleanup errors, and change ReadOnlyLockError to pass around more details.
59
                raise errors.ReadOnlyLockError(self.filename, str(e))
656 by Martin Pool
- create branch lock files if they don't exist
60
            if e.errno != errno.ENOENT:
61
                raise
62
63
            # maybe this is an old branch (before may 2005)
2353.3.11 by John Arbash Meinel
Code cleanup
64
            trace.mutter("trying to create missing lock %r", self.filename)
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
65
2353.3.10 by John Arbash Meinel
Cleanup errors, and change ReadOnlyLockError to pass around more details.
66
            self.f = open(self.filename, 'wb+')
656 by Martin Pool
- create branch lock files if they don't exist
67
            return self.f
68
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
69
    def _clear_f(self):
70
        """Clear the self.f attribute cleanly."""
71
        if self.f:
72
            self.f.close()
73
            self.f = None
74
615 by Martin Pool
Major rework of locking code:
75
    def __del__(self):
76
        if self.f:
77
            from warnings import warn
78
            warn("lock on %r not released" % self.f)
79
            self.unlock()
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
80
615 by Martin Pool
Major rework of locking code:
81
    def unlock(self):
82
        raise NotImplementedError()
83
84
577 by Martin Pool
- merge portable lock module from John
85
try:
86
    import fcntl
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
87
    have_fcntl = True
88
except ImportError:
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
89
    have_fcntl = False
90
try:
91
    import win32con, win32file, pywintypes, winerror, msvcrt
92
    have_pywin32 = True
93
except ImportError:
94
    have_pywin32 = False
95
try:
96
    import ctypes, msvcrt
97
    have_ctypes = True
98
except ImportError:
99
    have_ctypes = False
100
101
102
_lock_classes = []
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
103
104
105
if have_fcntl:
106
    LOCK_SH = fcntl.LOCK_SH
107
    LOCK_NB = fcntl.LOCK_NB
108
    lock_EX = fcntl.LOCK_EX
109
615 by Martin Pool
Major rework of locking code:
110
111
    class _fcntl_FileLock(_base_Lock):
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
112
113
        def _unlock(self):
1185.9.2 by Harald Meland
Use fcntl.lockf() rather than fcntl.flock() to support NFS file systems.
114
            fcntl.lockf(self.f, fcntl.LOCK_UN)
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
115
            self._clear_f()
116
117
615 by Martin Pool
Major rework of locking code:
118
    class _fcntl_WriteLock(_fcntl_FileLock):
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
119
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
120
        _open_locks = set()
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
121
615 by Martin Pool
Major rework of locking code:
122
        def __init__(self, filename):
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
123
            super(_fcntl_WriteLock, self).__init__()
2353.4.2 by John Arbash Meinel
[merge] LockCleanup changes.
124
            # Check we can grab a lock before we actually open the file.
125
            self.filename = osutils.realpath(filename)
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
126
            if (self.filename in _fcntl_WriteLock._open_locks
127
                or self.filename in _fcntl_ReadLock._open_locks):
128
                self._clear_f()
129
                raise errors.LockContention(self.filename)
130
2353.4.2 by John Arbash Meinel
[merge] LockCleanup changes.
131
            self._open(self.filename, 'rb+')
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
132
            # reserve a slot for this lock - even if the lockf call fails,
133
            # at thisi point unlock() will be called, because self.f is set.
134
            # TODO: make this fully threadsafe, if we decide we care.
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
135
            _fcntl_WriteLock._open_locks.add(self.filename)
615 by Martin Pool
Major rework of locking code:
136
            try:
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
137
                # LOCK_NB will cause IOError to be raised if we can't grab a
138
                # lock right away.
139
                fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
1185.65.29 by Robert Collins
Implement final review suggestions.
140
            except IOError, e:
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
141
                if e.errno in (errno.EAGAIN, errno.EACCES):
142
                    # We couldn't grab the lock
143
                    self.unlock()
1185.65.29 by Robert Collins
Implement final review suggestions.
144
                # we should be more precise about whats a locking
145
                # error and whats a random-other error
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
146
                raise errors.LockError(e)
615 by Martin Pool
Major rework of locking code:
147
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
148
        def unlock(self):
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
149
            _fcntl_WriteLock._open_locks.remove(self.filename)
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
150
            self._unlock()
151
152
615 by Martin Pool
Major rework of locking code:
153
    class _fcntl_ReadLock(_fcntl_FileLock):
1185.65.29 by Robert Collins
Implement final review suggestions.
154
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
155
        _open_locks = {}
2353.3.3 by John Arbash Meinel
Define an explicit error when trying to grab a write lock on a readonly file.
156
2353.4.3 by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process.
157
        def __init__(self, filename, _ignore_write_lock=False):
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
158
            super(_fcntl_ReadLock, self).__init__()
2353.4.2 by John Arbash Meinel
[merge] LockCleanup changes.
159
            self.filename = osutils.realpath(filename)
2353.4.3 by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process.
160
            if not _ignore_write_lock and self.filename in _fcntl_WriteLock._open_locks:
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
161
                raise errors.LockContention(self.filename)
162
            _fcntl_ReadLock._open_locks.setdefault(self.filename, 0)
163
            _fcntl_ReadLock._open_locks[self.filename] += 1
1185.65.29 by Robert Collins
Implement final review suggestions.
164
            self._open(filename, 'rb')
615 by Martin Pool
Major rework of locking code:
165
            try:
2255.10.2 by John Arbash Meinel
Update to dirstate locking.
166
                # LOCK_NB will cause IOError to be raised if we can't grab a
167
                # lock right away.
168
                fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
1185.65.29 by Robert Collins
Implement final review suggestions.
169
            except IOError, e:
170
                # we should be more precise about whats a locking
171
                # error and whats a random-other error
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
172
                raise errors.LockError(e)
615 by Martin Pool
Major rework of locking code:
173
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
174
        def unlock(self):
2353.4.1 by John Arbash Meinel
(broken) Change fcntl locks to be properly exclusive within the same process.
175
            count = _fcntl_ReadLock._open_locks[self.filename]
176
            if count == 1:
177
                del _fcntl_ReadLock._open_locks[self.filename]
178
            else:
179
                _fcntl_ReadLock._open_locks[self.filename] = count - 1
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
180
            self._unlock()
181
2353.4.3 by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process.
182
        def temporary_write_lock(self):
183
            """Try to grab a write lock on the file.
184
185
            On platforms that support it, this will upgrade to a write lock
186
            without unlocking the file.
187
            Otherwise, this will release the read lock, and try to acquire a
188
            write lock.
189
190
            :return: A token which can be used to switch back to a read lock.
191
            """
192
            assert self.filename not in _fcntl_WriteLock._open_locks
193
            return _fcntl_TemporaryWriteLock(self)
194
195
196
    class _fcntl_TemporaryWriteLock(_base_Lock):
197
        """A token used when grabbing a temporary_write_lock.
198
199
        Call restore_read_lock() when you are done with the write lock.
200
        """
201
202
        def __init__(self, read_lock):
203
            super(_fcntl_TemporaryWriteLock, self).__init__()
204
            self._read_lock = read_lock
205
            self.filename = read_lock.filename
206
207
            count = _fcntl_ReadLock._open_locks[self.filename]
208
            if count > 1:
209
                # Something else also has a read-lock, so we cannot grab a
210
                # write lock.
211
                raise errors.LockContention(self.filename)
212
213
            assert self.filename not in _fcntl_WriteLock._open_locks
214
215
            # See if we can open the file for writing. Another process might
216
            # have a read lock. We don't use self._open() because we don't want
217
            # to create the file if it exists. That would have already been
218
            # done by _fcntl_ReadLock
219
            try:
220
                new_f = open(self.filename, 'rb+')
221
            except IOError, e:
222
                if e.errno in (errno.EACCES, errno.EPERM):
223
                    raise errors.ReadOnlyLockError(self.filename, str(e))
224
                raise
225
            try:
226
                # LOCK_NB will cause IOError to be raised if we can't grab a
227
                # lock right away.
228
                fcntl.lockf(new_f, fcntl.LOCK_SH | fcntl.LOCK_NB)
229
            except IOError, e:
230
                # TODO: Raise a more specific error based on the type of error
231
                raise errors.LockError(e)
232
            _fcntl_WriteLock._open_locks.add(self.filename)
233
234
            self.f = new_f
235
236
        def restore_read_lock(self):
237
            """Restore the original ReadLock.
238
239
            For fcntl, since we never released the read lock, just release the
240
            write lock, and return the original lock.
241
            """
242
            fcntl.lockf(self.f, fcntl.LOCK_UN)
243
            self._clear_f()
244
            _fcntl_WriteLock._open_locks.remove(self.filename)
245
            # Avoid reference cycles
246
            read_lock = self._read_lock
247
            self._read_lock = None
248
            return read_lock
249
1852.4.3 by Robert Collins
Alter working tree lock and unlock tests to test via the interface rather than via unpublished internals.
250
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
251
    _lock_classes.append(('fcntl', _fcntl_WriteLock, _fcntl_ReadLock))
577 by Martin Pool
- merge portable lock module from John
252
2353.3.11 by John Arbash Meinel
Code cleanup
253
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
254
if have_pywin32:
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
255
    LOCK_SH = 0 # the default
256
    LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
257
    LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
258
259
260
    class _w32c_FileLock(_base_Lock):
261
262
        def _lock(self, filename, openmode, lockmode):
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
263
            self._open(filename, openmode)
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
264
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
265
            self.hfile = msvcrt.get_osfhandle(self.f.fileno())
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
266
            overlapped = pywintypes.OVERLAPPED()
267
            try:
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
268
                win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000,
269
                                     overlapped)
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
270
            except pywintypes.error, e:
271
                self._clear_f()
272
                if e.args[0] in (winerror.ERROR_LOCK_VIOLATION,):
273
                    raise errors.LockContention(filename)
274
                ## import pdb; pdb.set_trace()
275
                raise
276
            except Exception, e:
277
                self._clear_f()
2353.3.11 by John Arbash Meinel
Code cleanup
278
                raise errors.LockError(e)
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
279
280
        def unlock(self):
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
281
            overlapped = pywintypes.OVERLAPPED()
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
282
            try:
283
                win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
284
                self._clear_f()
285
            except Exception, e:
2353.3.11 by John Arbash Meinel
Code cleanup
286
                raise errors.LockError(e)
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
287
288
289
    class _w32c_ReadLock(_w32c_FileLock):
290
        def __init__(self, filename):
291
            super(_w32c_ReadLock, self).__init__()
292
            self._lock(filename, 'rb', LOCK_SH + LOCK_NB)
293
2353.3.11 by John Arbash Meinel
Code cleanup
294
2353.3.4 by John Arbash Meinel
Clean up the lock.py code to use less indenting, and conform to better coding practise.
295
    class _w32c_WriteLock(_w32c_FileLock):
296
        def __init__(self, filename):
297
            super(_w32c_WriteLock, self).__init__()
298
            self._lock(filename, 'rb+', LOCK_EX + LOCK_NB)
299
2353.3.11 by John Arbash Meinel
Code cleanup
300
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
301
    _lock_classes.append(('pywin32', _w32c_WriteLock, _w32c_ReadLock))
302
2353.3.11 by John Arbash Meinel
Code cleanup
303
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
304
if have_ctypes:
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
305
    # These constants were copied from the win32con.py module.
306
    LOCKFILE_FAIL_IMMEDIATELY = 1
307
    LOCKFILE_EXCLUSIVE_LOCK = 2
308
    # Constant taken from winerror.py module
309
    ERROR_LOCK_VIOLATION = 33
310
311
    LOCK_SH = 0
312
    LOCK_EX = LOCKFILE_EXCLUSIVE_LOCK
313
    LOCK_NB = LOCKFILE_FAIL_IMMEDIATELY
314
    _LockFileEx = ctypes.windll.kernel32.LockFileEx
315
    _UnlockFileEx = ctypes.windll.kernel32.UnlockFileEx
316
    _GetLastError = ctypes.windll.kernel32.GetLastError
317
318
    ### Define the OVERLAPPED structure.
319
    #   http://msdn2.microsoft.com/en-us/library/ms684342.aspx
320
    # typedef struct _OVERLAPPED {
321
    #   ULONG_PTR Internal;
322
    #   ULONG_PTR InternalHigh;
323
    #   union {
324
    #     struct {
325
    #       DWORD Offset;
326
    #       DWORD OffsetHigh;
327
    #     };
328
    #     PVOID Pointer;
329
    #   };
330
    #   HANDLE hEvent;
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
331
    # } OVERLAPPED,
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
332
333
    class _inner_struct(ctypes.Structure):
334
        _fields_ = [('Offset', ctypes.c_uint), # DWORD
335
                    ('OffsetHigh', ctypes.c_uint), # DWORD
336
                   ]
337
338
    class _inner_union(ctypes.Union):
339
        _fields_  = [('anon_struct', _inner_struct), # struct
340
                     ('Pointer', ctypes.c_void_p), # PVOID
341
                    ]
342
343
    class OVERLAPPED(ctypes.Structure):
344
        _fields_ = [('Internal', ctypes.c_void_p), # ULONG_PTR
345
                    ('InternalHigh', ctypes.c_void_p), # ULONG_PTR
346
                    ('_inner_union', _inner_union),
347
                    ('hEvent', ctypes.c_void_p), # HANDLE
348
                   ]
349
350
    class _ctypes_FileLock(_base_Lock):
351
352
        def _lock(self, filename, openmode, lockmode):
353
            self._open(filename, openmode)
354
355
            self.hfile = msvcrt.get_osfhandle(self.f.fileno())
356
            overlapped = OVERLAPPED()
357
            p_overlapped = ctypes.pointer(overlapped)
358
            result = _LockFileEx(self.hfile, # HANDLE hFile
359
                                 lockmode,   # DWORD dwFlags
360
                                 0,          # DWORD dwReserved
361
                                 0x7fffffff, # DWORD nNumberOfBytesToLockLow
362
                                 0x00000000, # DWORD nNumberOfBytesToLockHigh
363
                                 p_overlapped, # lpOverlapped
364
                                )
365
            if result == 0:
366
                self._clear_f()
367
                last_err = _GetLastError()
368
                if last_err in (ERROR_LOCK_VIOLATION,):
369
                    raise errors.LockContention(filename)
370
                raise errors.LockError('Unknown locking error: %s'
371
                                       % (last_err,))
372
373
        def unlock(self):
374
            overlapped = OVERLAPPED()
375
            p_overlapped = ctypes.pointer(overlapped)
376
            result = _UnlockFileEx(self.hfile, # HANDLE hFile
377
                                   0,          # DWORD dwReserved
378
                                   0x7fffffff, # DWORD nNumberOfBytesToLockLow
379
                                   0x00000000, # DWORD nNumberOfBytesToLockHigh
380
                                   p_overlapped, # lpOverlapped
381
                                  )
382
            self._clear_f()
383
            if result == 0:
384
                self._clear_f()
385
                last_err = _GetLastError()
386
                raise errors.LockError('Unknown unlocking error: %s'
387
                                       % (last_err,))
388
389
390
    class _ctypes_ReadLock(_ctypes_FileLock):
391
        def __init__(self, filename):
392
            super(_ctypes_ReadLock, self).__init__()
393
            self._lock(filename, 'rb', LOCK_SH + LOCK_NB)
394
2353.3.11 by John Arbash Meinel
Code cleanup
395
2353.3.5 by John Arbash Meinel
Fall back on ctypes to get LockFileEx, because msvcrt.locking()
396
    class _ctypes_WriteLock(_ctypes_FileLock):
397
        def __init__(self, filename):
398
            super(_ctypes_WriteLock, self).__init__()
399
            self._lock(filename, 'rb+', LOCK_EX + LOCK_NB)
400
2353.3.11 by John Arbash Meinel
Code cleanup
401
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
402
    _lock_classes.append(('ctypes', _ctypes_WriteLock, _ctypes_ReadLock))
403
404
405
if len(_lock_classes) == 0:
2353.3.11 by John Arbash Meinel
Code cleanup
406
    raise NotImplementedError(
407
        "We must have one of fcntl, pywin32, or ctypes available"
408
        " to support OS locking."
409
        )
410
2353.3.9 by John Arbash Meinel
Update the lock code and test code so that if more than one
411
412
# We default to using the first available lock class.
413
_lock_type, WriteLock, ReadLock = _lock_classes[0]
414