|
|
|
@ -2122,22 +2122,22 @@ if sys.platform == 'win32':
|
|
|
|
|
whole_low = 0xffffffff
|
|
|
|
|
whole_high = 0x7fffffff
|
|
|
|
|
|
|
|
|
|
def _lock_file(f, exclusive, block): # todo: block unused on win32
|
|
|
|
|
def _lock_file(f, exclusive, block):
|
|
|
|
|
overlapped = OVERLAPPED()
|
|
|
|
|
overlapped.Offset = 0
|
|
|
|
|
overlapped.OffsetHigh = 0
|
|
|
|
|
overlapped.hEvent = 0
|
|
|
|
|
f._lock_file_overlapped_p = ctypes.pointer(overlapped)
|
|
|
|
|
handle = msvcrt.get_osfhandle(f.fileno())
|
|
|
|
|
if not LockFileEx(handle, 0x2 if exclusive else 0x0, 0,
|
|
|
|
|
whole_low, whole_high, f._lock_file_overlapped_p):
|
|
|
|
|
raise OSError('Locking file failed: %r' % ctypes.FormatError())
|
|
|
|
|
|
|
|
|
|
if not LockFileEx(msvcrt.get_osfhandle(f.fileno()),
|
|
|
|
|
(0x2 if exclusive else 0x0) | (0x0 if block else 0x1),
|
|
|
|
|
0, whole_low, whole_high, f._lock_file_overlapped_p):
|
|
|
|
|
raise BlockingIOError('Locking file failed: %r' % ctypes.FormatError())
|
|
|
|
|
|
|
|
|
|
def _unlock_file(f):
|
|
|
|
|
assert f._lock_file_overlapped_p
|
|
|
|
|
handle = msvcrt.get_osfhandle(f.fileno())
|
|
|
|
|
if not UnlockFileEx(handle, 0,
|
|
|
|
|
whole_low, whole_high, f._lock_file_overlapped_p):
|
|
|
|
|
if not UnlockFileEx(handle, 0, whole_low, whole_high, f._lock_file_overlapped_p):
|
|
|
|
|
raise OSError('Unlocking file failed: %r' % ctypes.FormatError())
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
@ -2175,6 +2175,8 @@ else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class locked_file(object):
|
|
|
|
|
_closed = False
|
|
|
|
|
|
|
|
|
|
def __init__(self, filename, mode, block=True, encoding=None):
|
|
|
|
|
assert mode in ['r', 'rb', 'a', 'ab', 'w', 'wb']
|
|
|
|
|
self.f = io.open(filename, mode, encoding=encoding)
|
|
|
|
@ -2192,9 +2194,11 @@ class locked_file(object):
|
|
|
|
|
|
|
|
|
|
def __exit__(self, etype, value, traceback):
|
|
|
|
|
try:
|
|
|
|
|
if not self._closed:
|
|
|
|
|
_unlock_file(self.f)
|
|
|
|
|
finally:
|
|
|
|
|
self.f.close()
|
|
|
|
|
self._closed = True
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
return iter(self.f)
|
|
|
|
|