os: listdir(), walk(): Handle unicode strings properly.

CPython lib reference specifies that listdir() should accept both bytes and
str argument, and return value type should match the argument. But no such
stipulation is made for walk(), so we just return strings.
pull/7/head
Paul Sokolovsky 2014-09-06 17:06:07 +03:00
rodzic a3495c40bf
commit a9fd762b81
1 zmienionych plików z 9 dodań i 9 usunięć

Wyświetl plik

@ -115,13 +115,13 @@ def ilistdir_ex(path="."):
yield dirent yield dirent
def listdir(path="."): def listdir(path="."):
is_bytes = type(path) is bytes is_str = type(path) is not bytes
res = [] res = []
for dirent in ilistdir_ex(path): for dirent in ilistdir_ex(path):
fname = str(dirent[4].split('\0', 1)[0], "ascii") fname = dirent[4].split(b'\0', 1)[0]
if fname != "." and fname != "..": if fname != b"." and fname != b"..":
if is_bytes: if is_str:
fname = fsencode(fname) fname = fsdecode(fname)
res.append(fname) res.append(fname)
return res return res
@ -130,12 +130,12 @@ def walk(top, topdown=True):
dirs = [] dirs = []
for dirent in ilistdir_ex(top): for dirent in ilistdir_ex(top):
mode = dirent[3] << 12 mode = dirent[3] << 12
fname = str(dirent[4].split('\0', 1)[0], "ascii") fname = dirent[4].split(b'\0', 1)[0]
if stat_.S_ISDIR(mode): if stat_.S_ISDIR(mode):
if fname != "." and fname != "..": if fname != b"." and fname != b"..":
dirs.append(fname) dirs.append(fsdecode(fname))
else: else:
files.append(fname) files.append(fsdecode(fname))
if topdown: if topdown:
yield top, dirs, files yield top, dirs, files
for d in dirs: for d in dirs: