Ejemplo n.º 1
0
 def unlock(self):
     overlapped = pywintypes.OVERLAPPED()
     try:
         win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
         self._clear_f()
     except Exception, e:
         raise errors.LockContention(e)
Ejemplo n.º 2
0
    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)))
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 def release(self):
     """Release lock."""
     # Unlock file
     try:
         win32file.UnlockFileEx(self.handle, 0, 0x7fff0000,
                                pywintypes.OVERLAPPED())
     except Exception as ex:
         raise FileLockError(ex)
Ejemplo n.º 7
0
 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()}')
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
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
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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
Ejemplo n.º 14
0
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
    )
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
0
Archivo: file.py Proyecto: cfobel/durus
 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
Ejemplo n.º 17
0
 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
Ejemplo n.º 18
0
 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
Ejemplo n.º 19
0
 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?
Ejemplo n.º 20
0
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
Ejemplo n.º 21
0
 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
Ejemplo n.º 22
0
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")
Ejemplo n.º 23
0
 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)
Ejemplo n.º 24
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
Ejemplo n.º 25
0
# -*- coding:utf-8 -*-
Ejemplo n.º 26
0
 def unlock(f):
     hfile = win32file._get_osfhandle(f.fileno())
     win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)
Ejemplo n.º 27
0
 def unlock(file):
     hfile = win32file._get_osfhandle(file.fileno())
     win32file.UnlockFileEx(hfile, 0, -65536, __overlapped)
Ejemplo n.º 28
0
 def __unlock(self):
     hfile = win32file._get_osfhandle(self.__file.fileno())
     win32file.UnlockFileEx(hfile, 0, 0xf0, FileLocker.__overlapped)
Ejemplo n.º 29
0
def unlockFile(file):
    hfile = win32file._get_osfhandle(file.fileno())
    win32file.UnlockFileEx(hfile, 0, 0xffff, overlapped)
Ejemplo n.º 30
0
 def unlock(file):
     """Remove file lock."""
     hfile = win32file._get_osfhandle(file.fileno())
     win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped)