-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pathlike support * tests for fscompat * version bump * str fix * official release * docstring fix
- Loading branch information
1 parent
6b78709
commit 9489fdc
Showing
5 changed files
with
148 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import sys | ||
|
||
import six | ||
|
||
try: | ||
from os import fsencode, fsdecode | ||
except ImportError: | ||
def _fscodec(): | ||
encoding = sys.getfilesystemencoding() | ||
errors = 'strict' if encoding == 'mbcs' else 'surrogateescape' | ||
|
||
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 | ||
except ImportError: | ||
def fspath(path): | ||
"""Return the path representation of a path-like object. | ||
If str or bytes is passed in, it is returned unchanged. Otherwise the | ||
os.PathLike interface is used to get the path representation. If the | ||
path representation is not str or bytes, TypeError is raised. If the | ||
provided path is not str, bytes, or os.PathLike, TypeError is raised. | ||
""" | ||
if isinstance(path, (six.text_type, bytes)): | ||
return path | ||
|
||
# Work from the object's type to match method resolution of other magic | ||
# methods. | ||
path_type = type(path) | ||
try: | ||
path_repr = path_type.__fspath__(path) | ||
except AttributeError: | ||
if hasattr(path_type, '__fspath__'): | ||
raise | ||
else: | ||
raise TypeError("expected string type or os.PathLike object, " | ||
"not " + path_type.__name__) | ||
if isinstance(path_repr, (six.text_type, bytes)): | ||
return path_repr | ||
else: | ||
raise TypeError("expected {}.__fspath__() to return string type " | ||
"not {}".format(path_type.__name__, | ||
type(path_repr).__name__)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "2.0.4a2" | ||
__version__ = "2.0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
|
||
import six | ||
|
||
from fs._fscompat import fsencode, fsdecode, fspath | ||
|
||
|
||
class PathMock(object): | ||
def __init__(self, path): | ||
self._path = path | ||
def __fspath__(self): | ||
return self._path | ||
|
||
|
||
class BrokenPathMock(object): | ||
def __init__(self, path): | ||
self._path = path | ||
def __fspath__(self): | ||
return self.broken | ||
|
||
|
||
class TestFSCompact(unittest.TestCase): | ||
|
||
def test_fspath(self): | ||
path = PathMock('foo') | ||
self.assertEqual(fspath(path), 'foo') | ||
path = PathMock(b'foo') | ||
self.assertEqual(fspath(path), b'foo') | ||
path = 'foo' | ||
assert path is fspath(path) | ||
|
||
with self.assertRaises(TypeError): | ||
fspath(100) | ||
|
||
with self.assertRaises(TypeError): | ||
fspath(PathMock(5)) | ||
|
||
with self.assertRaises(AttributeError): | ||
fspath(BrokenPathMock('foo')) | ||
|
||
def test_fsencode(self): | ||
encode_bytes = fsencode(b'foo') | ||
assert isinstance(encode_bytes, bytes) | ||
self.assertEqual(encode_bytes, b'foo') | ||
|
||
encode_bytes = fsencode('foo') | ||
assert isinstance(encode_bytes, bytes) | ||
self.assertEqual(encode_bytes, b'foo') | ||
|
||
with self.assertRaises(TypeError): | ||
fsencode(5) | ||
|
||
def test_fsdecode(self): | ||
decode_text = fsdecode(b'foo') | ||
assert isinstance(decode_text, six.text_type) | ||
decode_text = fsdecode('foo') | ||
assert isinstance(decode_text, six.text_type) | ||
with self.assertRaises(TypeError): | ||
fsdecode(5) |