Beispiel #1
0
 def test_name_bytes(self):
     scan = rposix_scandir.opendir(basedir)
     found = []
     while True:
         p = rposix_scandir.nextentry(scan)
         if not p:
             break
         found.append(func(p))
     rposix_scandir.closedir(scan)
     found.remove('.')
     found.remove('..')
     assert sorted(found) == sorted(os.listdir(basedir))
Beispiel #2
0
 def test_name_bytes(self):
     scan = rposix_scandir.opendir('/')
     found = []
     while True:
         p = rposix_scandir.nextentry(scan)
         if not p:
             break
         assert rposix_scandir.has_name_bytes(p)
         found.append(rposix_scandir.get_name_bytes(p))
     rposix_scandir.closedir(scan)
     found.remove('.')
     found.remove('..')
     assert sorted(found) == sorted(os.listdir('/'))
Beispiel #3
0
 def test_name_bytes(self):
     for func in funcs:
         scan = rposix_scandir.opendir(basedir, len(basedir))
         found = []
         while True:
             p = rposix_scandir.nextentry(scan)
             if not p:
                 break
             found.append(func(p))
         rposix_scandir.closedir(scan)
         found.remove('.')
         found.remove('..')
     # win32 listdir must use unicode
     assert sorted(found) == sorted(os.listdir(basedir))
def scandir(space, w_path=None):
    "scandir(path='.') -> iterator of DirEntry objects for given path"
    if space.is_none(w_path):
        w_path = space.newunicode(u".")

    if not _WIN32:
        if space.isinstance_w(w_path, space.w_bytes):
            path = space.bytes0_w(w_path)
            result_is_bytes = True
        else:
            path = space.fsencode_w(w_path)
            result_is_bytes = False
    else:
        if space.isinstance_w(w_path, space.w_bytes):
            raise oefmt(
                space.w_TypeError, "os.scandir() doesn't support bytes path"
                " on Windows, use Unicode instead")
        path = space.unicode_w(w_path)
        result_is_bytes = False

    # 'path' is always bytes on posix and always unicode on windows
    try:
        dirp = rposix_scandir.opendir(path)
    except OSError as e:
        raise wrap_oserror2(space, e, w_path, eintr_retry=False)
    path_prefix = path
    if not _WIN32:
        if len(path_prefix) > 0 and path_prefix[-1] != '/':
            path_prefix += '/'
        w_path_prefix = space.newbytes(path_prefix)
        if not result_is_bytes:
            w_path_prefix = space.fsdecode(w_path_prefix)
    else:
        if len(path_prefix) > 0 and path_prefix[-1] not in (u'\\', u'/', u':'):
            path_prefix += u'\\'
        w_path_prefix = space.newunicode(path_prefix)
    if rposix.HAVE_FSTATAT:
        dirfd = rposix.c_dirfd(dirp)
    else:
        dirfd = -1
    return W_ScandirIterator(space, dirp, dirfd, w_path_prefix,
                             result_is_bytes)
Beispiel #5
0
def scandir(space, path=None):
    "scandir(path='.') -> iterator of DirEntry objects for given path"
    try:
        space._try_buffer_w(path.w_path, space.BUF_FULL_RO)
    except BufferInterfaceNotFound:
        as_bytes = (path.as_unicode is None)
        result_is_bytes = False
    else:
        as_bytes = True
        result_is_bytes = True
    if path.as_fd != -1:
        if not rposix.HAVE_FDOPENDIR:
            # needed for translation, in practice this is dead code
            raise oefmt(space.w_TypeError,
                        "scandir: illegal type for path argument")
        try:
            dirfd = rposix.dup(path.as_fd, inheritable=False)
        except OSError as e:
            raise wrap_oserror(space, e, eintr_retry=False)
        dirp = rposix.c_fdopendir(dirfd)
        if not dirp:
            rposix.c_close(dirfd)
            e = rposix.get_saved_errno()
            if e == ENOTDIR:
                w_type = space.w_NotADirectoryError
            else:
                w_type = space.w_ValueError
            raise oefmt(w_type, "invalid fd %d", path.as_fd)
        path_prefix = ''
    elif as_bytes:
        path_prefix = path.as_bytes
        try:
            name = path.as_bytes
            dirp = rposix_scandir.opendir(name, len(name))
        except OSError as e:
            raise wrap_oserror2(space,
                                e,
                                space.newbytes(path.as_bytes),
                                eintr_retry=False)
    else:
        w_path = path.w_path
        path_prefix = space.utf8_w(w_path)
        lgt = len(path_prefix)
        try:
            dirp = rposix_scandir.opendir(path_prefix, lgt)
        except OSError as e:
            raise wrap_oserror2(space, e, w_path, eintr_retry=False)
    if not _WIN32:
        if len(path_prefix) > 0 and path_prefix[-1] != '/':
            path_prefix += '/'
        w_path_prefix = space.newbytes(path_prefix)
        if not result_is_bytes:
            w_path_prefix = space.fsdecode(w_path_prefix)
    else:
        if len(path_prefix) > 0 and path_prefix[-1] not in ('\\', '/', ':'):
            path_prefix += '\\'
        if result_is_bytes:
            w_path_prefix = space.newbytes(path_prefix)
        else:
            w_path_prefix = space.newtext(path_prefix)
    if rposix.HAVE_FSTATAT:
        dirfd = rposix.c_dirfd(dirp)
    else:
        dirfd = -1
    return W_ScandirIterator(space, dirp, dirfd, w_path_prefix,
                             result_is_bytes, path.as_fd)