예제 #1
0
파일: ll_win32file.py 프로젝트: njues/Sypy
 def listdir_llimpl(path):
     mask = make_listdir_mask(path)
     filedata = lltype.malloc(win32traits.WIN32_FIND_DATA, flavor='raw')
     try:
         result = []
         hFindFile = win32traits.FindFirstFile(mask, filedata)
         if hFindFile == rwin32.INVALID_HANDLE_VALUE:
             error = rwin32.GetLastError()
             if error == win32traits.ERROR_FILE_NOT_FOUND:
                 return result
             else:
                 raise WindowsError(error, "FindFirstFile failed")
         while True:
             name = traits.charp2str(
                 rffi.cast(traits.CCHARP, filedata.c_cFileName))
             if not skip_listdir(name):
                 result.append(name)
             if not win32traits.FindNextFile(hFindFile, filedata):
                 break
         # FindNextFile sets error to ERROR_NO_MORE_FILES if
         # it got to the end of the directory
         error = rwin32.GetLastError()
         win32traits.FindClose(hFindFile)
         if error == win32traits.ERROR_NO_MORE_FILES:
             return result
         else:
             raise WindowsError(error, "FindNextFile failed")
     finally:
         lltype.free(filedata, flavor='raw')
예제 #2
0
    def win32_fstat_llimpl(fd):
        handle = rwin32.get_osfhandle(fd)
        filetype = win32traits.GetFileType(handle)
        if filetype == win32traits.FILE_TYPE_CHAR:
            # console or LPT device
            return make_stat_result(
                (win32traits._S_IFCHR, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        elif filetype == win32traits.FILE_TYPE_PIPE:
            # socket or named pipe
            return make_stat_result(
                (win32traits._S_IFIFO, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        elif filetype == win32traits.FILE_TYPE_UNKNOWN:
            error = rwin32.GetLastError()
            if error != 0:
                raise WindowsError(error, "os_fstat failed")
            # else: unknown but valid file

        # normal disk file (FILE_TYPE_DISK)
        info = lltype.malloc(win32traits.BY_HANDLE_FILE_INFORMATION,
                             flavor='raw',
                             zero=True)
        try:
            res = win32traits.GetFileInformationByHandle(handle, info)
            if res == 0:
                raise WindowsError(rwin32.GetLastError(), "os_fstat failed")
            return by_handle_info_to_stat(info)
        finally:
            lltype.free(info, flavor='raw')
예제 #3
0
 def dlopen(name, mode=-1):
     # mode is unused on windows, but a consistant signature
     res = rwin32.LoadLibrary(name)
     if not res:
         err = rwin32.GetLastError()
         raise DLOpenError(rwin32.FormatError(err))
     return res
예제 #4
0
 def win32_stat_llimpl(path):
     data = lltype.malloc(WIN32_FILE_ATTRIBUTE_DATA, flavor='raw')
     try:
         l_path = rffi.str2charp(path)
         res = GetFileAttributesEx(l_path, GetFileExInfoStandard, data)
         errcode = rwin32.GetLastError()
         if res == 0:
             if errcode == ERROR_SHARING_VIOLATION:
                 res = attributes_from_dir(l_path, data)
                 errcode = rwin32.GetLastError()
         rffi.free_charp(l_path)
         if res == 0:
             raise WindowsError(errcode, "os_stat failed")
         return attribute_data_to_stat(data)
     finally:
         lltype.free(data, flavor='raw')
예제 #5
0
 def create_semaphore(space, name, val, max):
     rwin32.SetLastError(0)
     handle = _CreateSemaphore(rffi.NULL, val, max, rffi.NULL)
     # On Windows we should fail on ERROR_ALREADY_EXISTS
     err = rwin32.GetLastError()
     if err != 0:
         raise WindowsError(err, "CreateSemaphore")
     return handle
예제 #6
0
 def win32_fstat_llimpl(fd):
     info = lltype.malloc(BY_HANDLE_FILE_INFORMATION, flavor='raw')
     try:
         handle = rwin32._get_osfhandle(fd)
         res = GetFileInformationByHandle(handle, info)
         if res == 0:
             raise WindowsError(rwin32.GetLastError(), "os_fstat failed")
         return by_handle_info_to_stat(info)
     finally:
         lltype.free(info, flavor='raw')
예제 #7
0
 def semlock_release(self, space):
     if not _ReleaseSemaphore(self.handle, 1, lltype.nullptr(
             rffi.LONGP.TO)):
         err = rwin32.GetLastError()
         if err == 0x0000012a:  # ERROR_TOO_MANY_POSTS
             raise OperationError(
                 space.w_ValueError,
                 space.wrap("semaphore or lock released too many times"))
         else:
             raise WindowsError(err, "ReleaseSemaphore")
예제 #8
0
    def do_recv_string(self, space, buflength, maxlength):
        from pypy.module._multiprocessing.interp_win32 import (
            _ReadFile, _PeekNamedPipe, ERROR_BROKEN_PIPE, ERROR_MORE_DATA)
        from pypy.rlib import rwin32
        from pypy.interpreter.error import wrap_windowserror

        read_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO,
                                 1,
                                 flavor='raw')
        left_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO,
                                 1,
                                 flavor='raw')
        try:
            result = _ReadFile(self.handle, self.buffer,
                               min(self.BUFFER_SIZE, buflength), read_ptr,
                               rffi.NULL)
            if result:
                return intmask(read_ptr[0]), lltype.nullptr(rffi.CCHARP.TO)

            err = rwin32.GetLastError()
            if err == ERROR_BROKEN_PIPE:
                raise OperationError(space.w_EOFError, space.w_None)
            elif err != ERROR_MORE_DATA:
                raise wrap_windowserror(space, WindowsError(err, "_ReadFile"))

            # More data...
            if not _PeekNamedPipe(self.handle, rffi.NULL, 0,
                                  lltype.nullptr(rwin32.LPDWORD.TO),
                                  lltype.nullptr(rwin32.LPDWORD.TO), left_ptr):
                raise wrap_windowserror(space, rwin32.lastWindowsError())

            length = intmask(read_ptr[0] + left_ptr[0])
            if length > maxlength:  # bad message, close connection
                self.flags &= ~READABLE
                if self.flags == 0:
                    self.close()
                raise OperationError(space.w_IOError,
                                     space.wrap("bad message length"))

            newbuf = lltype.malloc(rffi.CCHARP.TO, length + 1, flavor='raw')
            for i in range(read_ptr[0]):
                newbuf[i] = self.buffer[i]

            result = _ReadFile(self.handle, rffi.ptradd(newbuf, read_ptr[0]),
                               left_ptr[0], read_ptr, rffi.NULL)
            if not result:
                rffi.free_charp(newbuf)
                raise wrap_windowserror(space, rwin32.lastWindowsError())

            assert read_ptr[0] == left_ptr[0]
            return length, newbuf
        finally:
            lltype.free(read_ptr, flavor='raw')
            lltype.free(left_ptr, flavor='raw')
예제 #9
0
def load_resource_pystr(py, filename, resname, resid, reslang):
    """Load the named resource from the given file as a python-level string

    The filename and resource name must be ascii strings, and the resid and
    reslang must be integers.

    This uses the given python dll object to load the data directly into
    a python string, saving a lot of copying and carrying on.
    """
    l_handle = k32_LoadLibraryExA(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
    if not l_handle:
        raise WindowsError(rwin32.GetLastError(), "LoadLibraryExW failed")
    try:
        r_handle = k32_FindResourceExA(l_handle, resname, resid, reslang)
        if not r_handle:
            raise WindowsError(rwin32.GetLastError(), "FindResourceExA failed")
        r_size = k32_SizeofResource(l_handle, r_handle)
        if not r_size:
            raise WindowsError(rwin32.GetLastError(), "SizeofResource failed")
        r_info = k32_LoadResource(l_handle, r_handle)
        if not r_info:
            raise WindowsError(rwin32.GetLastError(), "LoadResource failed")
        r_ptr = k32_LockResource(r_info)
        if not r_ptr:
            raise WindowsError(rwin32.GetLastError(), "LockResource failed")
        s = py.String_FromStringAndSize(None, r_size)
        buf = py.String_AsString(s)
        memcpy(buf, r_ptr, r_size)
        return s
    finally:
        if not k32_FreeLibrary(l_handle):
            raise WindowsError(rwin32.GetLastError(), "FreeLibrary failed")
예제 #10
0
def load_resource(filename, resname, resid, reslang):
    """Load the named resource from the given file.

    The filename and resource name must be ascii strings, and the resid and
    reslang must be integers.
    """
    l_handle = k32_LoadLibraryExA(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
    if not l_handle:
        raise WindowsError(rwin32.GetLastError(), "LoadLibraryExW failed")
    try:
        r_handle = k32_FindResourceExA(l_handle, resname, resid, reslang)
        if not r_handle:
            raise WindowsError(rwin32.GetLastError(), "FindResourceExA failed")
        r_size = k32_SizeofResource(l_handle, r_handle)
        if not r_size:
            raise WindowsError(rwin32.GetLastError(), "SizeofResource failed")
        r_info = k32_LoadResource(l_handle, r_handle)
        if not r_info:
            raise WindowsError(rwin32.GetLastError(), "LoadResource failed")
        r_ptr = k32_LockResource(r_info)
        if not r_ptr:
            raise WindowsError(rwin32.GetLastError(), "LockResource failed")
        return rffi.charpsize2str(r_ptr, r_size)
    finally:
        if not k32_FreeLibrary(l_handle):
            raise WindowsError(rwin32.GetLastError(), "FreeLibrary failed")
예제 #11
0
 def ftruncate_win32(fd, size):
     curpos = os.lseek(fd, 0, 1)
     try:
         # move to the position to be truncated
         os.lseek(fd, size, 0)
         # Truncate.  Note that this may grow the file!
         handle = _get_osfhandle(fd)
         if handle == -1:
             raise OSError(errno.EBADF, "Invalid file handle")
         if not SetEndOfFile(handle):
             raise WindowsError(rwin32.GetLastError(),
                                "Could not truncate file")
     finally:
         os.lseek(fd, curpos, 0)
예제 #12
0
파일: rmmap.py 프로젝트: purepython/pypy
 def _get_file_size(handle):
     # XXX use native Windows types like WORD
     high_ref = lltype.malloc(LPDWORD.TO, 1, flavor='raw')
     try:
         low = GetFileSize(handle, high_ref)
         low = rffi.cast(lltype.Signed, low)
         # XXX should be propagate the real type, allowing
         # for 2*sys.maxint?
         high = high_ref[0]
         # low might just happen to have the value INVALID_FILE_SIZE
         # so we need to check the last error also
         INVALID_FILE_SIZE = -1
         if low == INVALID_FILE_SIZE:
             err = rwin32.GetLastError()
             if err:
                 raise WindowsError(err, "mmap")
         return low, high
     finally:
         lltype.free(high_ref, flavor='raw')
예제 #13
0
    def do_send_string(self, space, buffer, offset, size):
        from pypy.module._multiprocessing.interp_win32 import (
            _WriteFile, ERROR_NO_SYSTEM_RESOURCES)
        from pypy.rlib import rwin32

        charp = rffi.str2charp(buffer)
        written_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO,
                                    1,
                                    flavor='raw')
        try:
            result = _WriteFile(self.handle, rffi.ptradd(charp, offset), size,
                                written_ptr, rffi.NULL)

            if (result == 0
                    and rwin32.GetLastError() == ERROR_NO_SYSTEM_RESOURCES):
                raise operationerrfmt(space.w_ValueError,
                                      "Cannot send %d bytes over connection",
                                      size)
        finally:
            rffi.free_charp(charp)
            lltype.free(written_ptr, flavor='raw')
예제 #14
0
 def dlopen(name):
     res = rwin32.LoadLibrary(name)
     if not res:
         # XXX format error message
         raise WindowsError(2, rwin32.GetLastError())
     return res
예제 #15
0
 def delete_semaphore(handle):
     if not _CloseHandle(handle):
         err = rwin32.GetLastError()
         raise WindowsError(err, "CloseHandle")
예제 #16
0
파일: interp_win32.py 프로젝트: njues/Sypy
def GetLastError(space):
    return space.wrap(rwin32.GetLastError())