def unlock(self): overlapped = pywintypes.OVERLAPPED() try: win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped) self._clear_f() except Exception, e: raise errors.LockContention(e)
def flock(fd, flags=0): hfile = win32file._get_osfhandle(fd) if flags & LOCK_UN != 0: # Unlock file descriptor try: win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) except pywintypes.error as exc_value: # error: (158, 'UnlockFileEx', 'The segment is already unlocked.') # To match the 'posix' implementation, silently ignore this error if exc_value[0] == 158: pass else: # Q: Are there exceptions/codes we should be dealing with here? raise elif flags & LOCK_EX != 0: # Lock file try: win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped) except pywintypes.error as exc_value: if exc_value[0] == 33: # error: (33, 'LockFileEx', # 'The process cannot access the file because another process has locked # a portion of the file.') raise IOError(33, exc_value[2]) else: # Q: Are there exceptions/codes we should be dealing with here? raise else: raise NotImplementedError("Unsupported set of bitflags {}".format( bin(flags)))
def unlock(file_): try: savepos = file_.tell() if savepos: file_.seek(0) try: msvcrt.locking(file_.fileno(), constants.LOCK_UN, lock_length) except IOError as exc_value: if exc_value.strerror == 'Permission denied': hfile = win32file._get_osfhandle(file_.fileno()) try: win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) except pywintypes.error as exc_value: if exc_value.winerror == winerror.ERROR_NOT_LOCKED: # error: (158, 'UnlockFileEx', # 'The segment is already unlocked.') # To match the 'posix' implementation, silently # ignore this error pass else: # Q: Are there exceptions/codes we should be # dealing with here? raise else: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror) finally: if savepos: file_.seek(savepos) except IOError as exc_value: raise exceptions.LockException( exceptions.LockException.LOCK_FAILED, exc_value.strerror)
def release(self): # 文件解锁 if fcntl: fcntl.flock(self.handle, fcntl.LOCK_UN) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, overlapped)
def unlock(file): try: hfile = win32file._get_osfhandle(file.fileno()) win32file.UnlockFileEx(hfile, 0, 0x7fffffff, __overlapped) except pywintypes.error, e: # err 120 is unimplemented call, happens on win9x: if e.args[0] != 120: raise e
def release(self): """Release lock.""" # Unlock file try: win32file.UnlockFileEx(self.handle, 0, 0x7fff0000, pywintypes.OVERLAPPED()) except Exception as ex: raise FileLockError(ex)
def unlock(self): if fcntl: fcntl.flock(self.handle, fcntl.LOCK_UN) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, _OVERLAPPED) self.is_locked = False LOG.vv(f'UNLOCK PID: {os.getpid()}')
def unlock_and_close(self): """Close and unlock the file using the win32 primitive.""" if self._locked: try: hfile = win32file._get_osfhandle(self._fh.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) except pywintypes.error, e: if e[0] != _Win32Opener.FILE_ALREADY_UNLOCKED_ERROR: raise
def _do_release(self, path): hfile = win32file._get_osfhandle(self.fd.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, pywintypes.OVERLAPPED()) self.fd.close() self.fd = None if self.delete_lock_file: try: os.unlink(path) except WindowsError: pass
def unlock(file): """Try UnlockFileEx first, then move to (Win95, Win98, WinME) solutions.""" hfile = win32file._get_osfhandle(file.fileno()) try: win32file.UnlockFileEx(hfile, 0, FFFF0000, __overlapped) except win32file.error, e: import winerror if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: raise e win32file.UnlockFile(hfile, 0, 0, FFFF0000, 0)
def unlock(self) : if fcntl : fcntl.flock(self.fp, fcntl.LOCK_UN) else : hfile = win32file._get_osfhandle(self.fp.fileno()) win32file.UnlockFileEx(hfile, 0, FileLock.highbits, overlapped) try : self.fp.close() self.fp = None except : pass
def release(self): # 文件解锁 if os.name == 'nt': import win32file import pywintypes overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, overlapped) elif os.name == 'posix': import fcntl fcntl.flock(self.handle, fcntl.LOCK_UN)
def unlock(self): if getattr(self, 'has_lock', False): try: hfile = win32file._get_osfhandle(self.fh.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, _overlapped) self.fh.close() del self.fh os.unlink(self.lock_fname) except: pass self.has_lock = False
def UnlockFileEx(handle, nbytesLow, nbytesHigh, overlapped): # warning - pywin32 expects dwords as signed integer, so that -1 <-> max_int nbytesLow = _utilities.unsigned_to_signed(nbytesLow) nbytesHigh = _utilities.unsigned_to_signed(nbytesHigh) result = win32file.UnlockFileEx( handle, # HANDLE hFile nbytesLow, # DWORD nNumberOfBytesToLockLow nbytesHigh, # DWORD nNumberOfBytesToLockHigh overlapped # lpOverlapped )
def unlock_file(f): while True: try: if platform.system() == PLATFORM_WINDOWS: hfile = win32file._get_osfhandle(f.fileno()) win32file.UnlockFileEx(hfile, 0, 0, 0xffff0000, pywintypes.OVERLAPPED()) else: fcntl.flock(f, fcntl.LOCK_UN) break except: time.sleep(0.1)
def release_lock(self): """ Make sure that we do not retain an exclusive lock on self.file. """ if self.has_lock: if os.name == 'nt': win32file.UnlockFileEx( win32file._get_osfhandle(self.file.fileno()), 0, -65536, pywintypes.OVERLAPPED()) else: fcntl.flock(self.file, fcntl.LOCK_UN) self.has_lock = False
def unlock(file): hfile = win32file._get_osfhandle(file.fileno()) try: win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) except pywintypes.error, exc_value: if exc_value[0] == 158: # error: (158, 'UnlockFileEx', 'The segment is already unlocked.') # To match the 'posix' implementation, silently ignore this error pass else: # Q: Are there exceptions/codes we should be dealing with here? raise
def lockf(fd, operation): file_handle = win32file._get_osfhandle(fd.fileno()) if operation == LOCK_UN: try: win32file.UnlockFileEx(file_handle, 0, -0x7fff0000, overlapped) except pywintypes.error, e: if e[0] == 158: # Ignore error that happens when you unlock an already # unlocked file. pass else: raise
def unlock(file): hfile = win32file._get_osfhandle(file.fileno()) try: win32file.UnlockFileEx(hfile, 0, nNumberOfBytesToLockHigh, __overlapped) except pywintypes.error as exc_value: if exc_value.winerror == 158: # error: (158, 'UnlockFileEx', 'The segment is already unlocked.') To match the 'posix' implementation, # silently ignore this error pass else: raise # Q: Are there exceptions/codes we should be dealing with here?
def lockf(fd, flags, length=0xFFFF0000, start=0, whence=0): overlapped = pywintypes.OVERLAPPED() hfile = msvcrt.get_osfhandle(fd.fileno()) if LOCK_UN & flags: ret = win32file.UnlockFileEx(hfile, 0, start, length, overlapped) else: try: ret = win32file.LockFileEx(hfile, flags, start, length, overlapped) except: raise IOError(errno.EAGAIN, "", "") return ret
def __exit__(self, type, value, trace): if self.f: if os.name == 'nt': __overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(self.f.fileno()) win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped) elif os.name == 'posix': fcntl.flock(self.file.fileno(), fcntl.LOCK_UN) self.f.close() try: os.remove(self.filepath) except: pass
def _unlock_file(f): import os if os.name == 'nt': import win32con import win32file import pywintypes __overlapped = pywintypes.OVERLAPPED() hfile = win32file._get_osfhandle(f.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped) elif os.name == 'posix': import fcntl fcntl.flock(f, fcntl.LOCK_UN) else: raise OSError("Unsupported OS")
def unlock(file): hfile = win32file._get_osfhandle(file.fileno()) # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98, # WinME). # If it's not supported, win32file will raise an api_error exception. try: win32file.UnlockFileEx(hfile, 0, FFFF0000, __overlapped) except win32file.error, e: import winerror # Propagate upwards all exceptions other than not-implemented. if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: raise e # UnlockFileEx is not supported. Use UnlockFile. # Care: the low/high length params are reversed compared to # UnLockFileEx. win32file.UnlockFile(hfile, 0, 0, FFFF0000, 0)
def unlock_file(file_descriptor): """ Cross-platform utility function for releasing the lock on the given open file. Based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203. @param file_descriptor: open file descriptor @type file_descriptor: file """ if on_posix(): import fcntl fcntl.lockf(file_descriptor, fcntl.LOCK_UN) else: import pywintypes, win32file # pylint: disable-msg=F0401 file_handle = win32file._get_osfhandle(file_descriptor.fileno()) # pylint: disable-msg=W0212 win32file.UnlockFileEx(file_handle, 0, -0x10000, pywintypes.OVERLAPPED()) # pylint: disable-msg=E1101
# -*- coding:utf-8 -*-
def unlock(f): hfile = win32file._get_osfhandle(f.fileno()) win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)
def unlock(file): hfile = win32file._get_osfhandle(file.fileno()) win32file.UnlockFileEx(hfile, 0, -65536, __overlapped)
def __unlock(self): hfile = win32file._get_osfhandle(self.__file.fileno()) win32file.UnlockFileEx(hfile, 0, 0xf0, FileLocker.__overlapped)
def unlockFile(file): hfile = win32file._get_osfhandle(file.fileno()) win32file.UnlockFileEx(hfile, 0, 0xffff, overlapped)
def unlock(file): """Remove file lock.""" hfile = win32file._get_osfhandle(file.fileno()) win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)