Skip to content

Commit

Permalink
fsn2bytes and bytes2fsn now default to utf-8/wtf-8
Browse files Browse the repository at this point in the history
I've never needed something else, let's make it the default.
  • Loading branch information
lazka committed Jan 23, 2018
1 parent 07b617e commit 00ffcd6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.3.4 - 2018-01-23
------------------

* :func:`fsn2bytes` and :func:`bytes2fsn` now default to "wtf-8" for the
Windows path encoding instead of having no default.


1.3.3 - 2018-01-03
------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ There are various ways to create fsnative instances:
>>> senf.fsnative(u"foo")
'foo'
# create form some serialized format
# create from some serialized format
>>> senf.bytes2fsn(b"foo", "utf-8")
'foo'
Expand Down
21 changes: 11 additions & 10 deletions senf/_fsnative.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,22 +473,21 @@ def text2fsn(text):
return fsnative(text)


def fsn2bytes(path, encoding):
def fsn2bytes(path, encoding="utf-8"):
"""
Args:
path (fsnative): The path to convert
encoding (`str` or `None`): `None` if you don't care about Windows
encoding (`str`): encoding used for Windows
Returns:
`bytes`
Raises:
TypeError: If no `fsnative` path is passed
ValueError: If encoding fails or no encoding is given
ValueError: If encoding fails or the encoding is invalid
Converts a `fsnative` path to `bytes`.
The passed *encoding* is only used on platforms where paths are not
associated with an encoding (Windows for example). If you don't care about
Windows you can pass `None`.
associated with an encoding (Windows for example).
For Windows paths, lone surrogates will be encoded like normal code points
and surrogate pairs will be merged before encoding. In case of ``utf-8``
Expand All @@ -510,22 +509,24 @@ def fsn2bytes(path, encoding):
return path


def bytes2fsn(data, encoding):
def bytes2fsn(data, encoding="utf-8"):
"""
Args:
data (bytes): The data to convert
encoding (`str` or `None`): `None` if you don't care about Windows
encoding (`str`): encoding used for Windows
Returns:
`fsnative`
Raises:
TypeError: If no `bytes` path is passed
ValueError: If decoding fails or no encoding is given
ValueError: If decoding fails or the encoding is invalid
Turns `bytes` to a `fsnative` path.
The passed *encoding* is only used on platforms where paths are not
associated with an encoding (Windows for example). If you don't care about
Windows you can pass `None`.
associated with an encoding (Windows for example).
For Windows paths ``WTF-8`` is accepted if ``utf-8`` is used and
``WTF-16`` accepted if ``utf-16-le`` is used.
"""

if not isinstance(data, bytes):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ def test_fsn2bytes():
with pytest.raises(TypeError):
fsn2bytes(path)

assert fsn2bytes(fsnative(u"foo"), "utf-8") == fsn2bytes(fsnative(u"foo"))


@pytest.mark.skipif(os.name != "nt", reason="win only")
def test_fsn2bytes_surrogate_pairs():
Expand Down Expand Up @@ -737,6 +739,8 @@ def test_bytes2fsn():
with pytest.raises(TypeError):
bytes2fsn(b"data", object())

assert bytes2fsn(b"foo", "utf-8") == bytes2fsn(b"foo")


def test_constants():
assert isinstance(sep, fsnative)
Expand Down

0 comments on commit 00ffcd6

Please sign in to comment.