Skip to content

Commit

Permalink
uri2fsn: support surrogates. Fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
lazka committed Jul 26, 2017
1 parent 2a0e095 commit 7878b6c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
6 changes: 2 additions & 4 deletions senf/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
if PY2:
from urlparse import urlparse, urlunparse
urlparse, urlunparse
from urllib import pathname2url, url2pathname, quote, unquote
pathname2url, url2pathname, quote, unquote
from urllib import quote, unquote
quote, unquote

from StringIO import StringIO
BytesIO = StringIO
Expand All @@ -45,8 +45,6 @@
elif PY3:
from urllib.parse import urlparse, quote, unquote, urlunparse
urlparse, quote, unquote, urlunparse
from urllib.request import pathname2url, url2pathname
pathname2url, url2pathname

from io import StringIO
StringIO = StringIO
Expand Down
23 changes: 17 additions & 6 deletions senf/_fsnative.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import codecs

from . import _winapi as winapi
from ._compat import text_type, PY3, PY2, url2pathname, urlparse, quote, \
unquote, urlunparse
from ._compat import text_type, PY3, PY2, urlparse, quote, unquote, urlunparse


is_win = os.name == "nt"
Expand Down Expand Up @@ -551,7 +550,18 @@ def uri2fsn(uri):
uri = urlunparse(parsed)[7:]

if is_win:
path = url2pathname(uri)
try:
drive, rest = uri.split(":", 1)
except ValueError:
path = ""
rest = uri.replace("/", "\\")
else:
path = drive[-1] + ":"
rest = rest.replace("/", "\\")
if PY2:
path += unquote(rest)
else:
path += unquote(rest, errors="surrogatepass")
if netloc:
path = "\\\\" + path
if PY2:
Expand All @@ -560,11 +570,12 @@ def uri2fsn(uri):
raise ValueError("embedded null")
return path
else:
path = unquote(uri)
if PY2:
path = unquote(uri)
else:
path = unquote(uri, errors="surrogateescape")
if "\x00" in path:
raise ValueError("embedded null")
if PY3:
path = fsnative(path)
return path


Expand Down
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,7 @@ def test_uri2fsn():
fsnative(u"/bla:foo@NOPE.com")
assert uri2fsn("file:///bla?x#b") == fsnative(u"/bla?x#b")
else:
# FIXME
# assert uri2fsn("file:///C:/%ED%A0%80") == fsnative(u"C:\\\ud800")
assert uri2fsn("file:///C:/%ED%A0%80") == fsnative(u"C:\\\ud800")
assert uri2fsn("file:///C:/%20") == "C:\\ "
assert uri2fsn("file:NOPE") == "\\NOPE"
assert uri2fsn("file:/NOPE") == "\\NOPE"
Expand Down Expand Up @@ -967,6 +966,7 @@ def test_uri_roundtrip():
else:
path = path2fsn(b"/foo-\xe1\x88\xb4")

assert uri2fsn(fsn2uri(path2fsn(b"/\x80"))) == path2fsn(b"/\x80")
assert uri2fsn(fsn2uri(fsnative(u"/foo"))) == "/foo"
assert uri2fsn(fsn2uri(path)) == path
assert isinstance(uri2fsn(fsn2uri(path)), fsnative)
Expand Down
10 changes: 1 addition & 9 deletions tests/test_hypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,7 @@ def test_anything(pathlike):
abspath = os.path.abspath(
fsn.replace(sep, fsnative()).replace(altsep or sep, fsnative()))
if os.path.isabs(abspath):
try:
if isinstance(abspath, text_type):
abspath.encode("utf-8")
except UnicodeEncodeError:
# FIXME: url2pathname can't handle surrogates in Python 3,
# get rid of it
pass
else:
assert uri2fsn(fsn2uri(abspath)) == abspath
assert uri2fsn(fsn2uri(abspath)) == abspath

try:
# never raises ValueError/TypError
Expand Down

0 comments on commit 7878b6c

Please sign in to comment.