Skip to content

Commit

Permalink
Merge 0ebc631 into ee3e335
Browse files Browse the repository at this point in the history
  • Loading branch information
pombredanne committed Dec 21, 2017
2 parents ee3e335 + 0ebc631 commit a343e31
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 87 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
12 changes: 10 additions & 2 deletions fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,14 @@ def getsize(self, path):
size = self.getdetails(path).size
return size

def getsyspath(self, path):
def getsyspath(self, path, as_bytes=False):
"""Get the *system path* of a resource.
Parameters:
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
Loading

0 comments on commit a343e31

Please sign in to comment.