From a9fd762b814d344e4ee3c27582e5b1bce4f54ebd Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 6 Sep 2014 17:06:07 +0300 Subject: [PATCH] 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. --- os/os/__init__.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/os/os/__init__.py b/os/os/__init__.py index 6d9d5ded..58b750fa 100644 --- a/os/os/__init__.py +++ b/os/os/__init__.py @@ -115,13 +115,13 @@ def ilistdir_ex(path="."): yield dirent def listdir(path="."): - is_bytes = type(path) is bytes + is_str = type(path) is not bytes res = [] for dirent in ilistdir_ex(path): - fname = str(dirent[4].split('\0', 1)[0], "ascii") - if fname != "." and fname != "..": - if is_bytes: - fname = fsencode(fname) + fname = dirent[4].split(b'\0', 1)[0] + if fname != b"." and fname != b"..": + if is_str: + fname = fsdecode(fname) res.append(fname) return res @@ -130,12 +130,12 @@ def walk(top, topdown=True): dirs = [] for dirent in ilistdir_ex(top): 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 fname != "." and fname != "..": - dirs.append(fname) + if fname != b"." and fname != b"..": + dirs.append(fsdecode(fname)) else: - files.append(fname) + files.append(fsdecode(fname)) if topdown: yield top, dirs, files for d in dirs: