Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Use sys.getfilesystemencoding() on Linux/BSD instead of hardcoded utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoyt committed Nov 27, 2012
1 parent 54c882c commit 723bb93
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions betterwalk.py
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 723bb93

Please sign in to comment.