Skip to content

Commit

Permalink
Merge 3504187 into ee3e335
Browse files Browse the repository at this point in the history
  • Loading branch information
pombredanne committed Dec 21, 2017
2 parents ee3e335 + 3504187 commit 25bd1af
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ ENV/
#PyCharm

.idea/
/tmp/
34 changes: 1 addition & 33 deletions fs/_fscompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,8 @@
try:
from os import fsencode, fsdecode
except ImportError:
def _fscodec():
encoding = sys.getfilesystemencoding()
errors = 'strict' if encoding == 'mbcs' else 'surrogateescape'
from backports.os import fsencode, fsdecode

def fsencode(filename):
"""
Encode filename to the filesystem encoding with 'surrogateescape' error
handler, return bytes unchanged. On Windows, use 'strict' error handler if
the file system encoding is 'mbcs' (which is the default encoding).
"""
if isinstance(filename, bytes):
return filename
elif isinstance(filename, six.text_type):
return filename.encode(encoding, errors)
else:
raise TypeError("expect string type, not %s" % type(filename).__name__)

def fsdecode(filename):
"""
Decode filename from the filesystem encoding with 'surrogateescape' error
handler, return str unchanged. On Windows, use 'strict' error handler if
the file system encoding is 'mbcs' (which is the default encoding).
"""
if isinstance(filename, six.text_type):
return filename
elif isinstance(filename, bytes):
return filename.decode(encoding, errors)
else:
raise TypeError("expect string type, not %s" % type(filename).__name__)

return fsencode, fsdecode

fsencode, fsdecode = _fscodec()
del _fscodec

try:
from os import fspath
Expand Down
10 changes: 9 additions & 1 deletion fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ def getsyspath(self, path):
path (str): A path on the filesystem.
Returns:
str: the *system path* of the resource, if any.
str: the *system path* of the resource, if any, always as Unicode.
Raises:
fs.errors.NoSysPath: If there is no corresponding system path.
Expand All @@ -629,6 +629,14 @@ def getsyspath(self, path):
resource is referenced by that path -- as long as it can
be certain what that system path would be.
Note:
The returned value is always Unicode. Some filesystems
such as on Linux only deal with bytes. If you need to use
a path outside of Python, or if you are running Python 2
on Linux, you can get a proper byte value from a Unicode
path using the os.fsencode function on Python 3 or its
Pythyon 2 backport available here.
"""
raise errors.NoSysPath(path=path)

Expand Down

0 comments on commit 25bd1af

Please sign in to comment.