diff --git a/README.md b/README.md index 2419a90..3c5c243 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,6 @@ To-do ----- * Windows FindFirst/Next wildcard matching is quirky (compared to fnmatch). From Random832 on python-ideas: it matches short filenames, the behavior you noted of "?" at the end of patterns also applies to the end of the 'filename portion' (e.g. foo?.txt can match foo.txt), and the behavior of patterns ending in ".*" or "." isn't like fnmatch. [This](http://digital.ni.com/public.nsf/allkb/0DBE16907A17717B86256F7800169797) and [this](http://blogs.msdn.com/b/oldnewthing/archive/2007/12/17/6785519.aspx) might be helpful. -* Use sys.getfilesystemencoding() instead of hard-coded utf-8 in POSIX version. * From John Mulligan on python-ideas: there is a potential race condition between calling the readdir and the stat, like if the object is removed between calls. Consider what to do here, maybe return None for all st_* fields on stat() error? * Test performance of pattern param on Windows versus fnmatch filtering afterwards. * Add tests, especially for [reparse points / Win32 symbolic links](http://mail.python.org/pipermail/python-ideas/2012-November/017794.html) diff --git a/betterwalk.py b/betterwalk.py index e010e6a..ea56791 100644 --- a/betterwalk.py +++ b/betterwalk.py @@ -191,6 +191,8 @@ class dirent(ctypes.Structure): closedir.argtypes = [DIR_p] closedir.restype = ctypes.c_int + file_system_encoding = sys.getfilesystemencoding() + def type_to_stat(d_type): """Convert dirent.d_type value to stat_result.""" st_mode = d_type << 12 @@ -208,7 +210,7 @@ def iterdir_stat(path='.', pattern='*', fields=None): # call stat() on each file need_stat = fields is not None and set(fields) != set(['st_mode_type']) - dir_p = opendir(path.encode('utf-8')) + dir_p = opendir(path.encode(file_system_encoding)) if not dir_p: raise posix_error(path) try: @@ -219,7 +221,7 @@ def iterdir_stat(path='.', pattern='*', fields=None): raise posix_error(path) if not result: break - name = entry.d_name.decode('utf-8') + name = entry.d_name.decode(file_system_encoding) if name not in ('.', '..'): if pattern == '*' or fnmatch.fnmatch(name, pattern): if need_stat or entry.d_type == DT_UNKNOWN: