Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative file url repr #705

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/648.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix relative file url representation.
17 changes: 17 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def test_str():
assert str(url) == "http://example.com:8888/path/to?a=1&b=2"


def test_str_relative_file():
url1 = URL("file:foo/bar")
assert str(url1) == "file:foo/bar"
url2 = URL("file:///foo/bar")
assert str(url2) == "file:///foo/bar"


def test_repr():
url = URL("http://example.com")
assert "URL('http://example.com')" == repr(url)
Expand Down Expand Up @@ -1546,6 +1553,16 @@ def test_human_repr_non_printable():
)


def test_human_repr_file():
url = URL.build(
scheme="file",
path="foo/bar",
)
s = url.human_repr()
assert URL(s) == url
assert s == "file:foo/bar"


# relative


Expand Down
11 changes: 9 additions & 2 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def __str__(self):
val = self._val
if not val.path and self.is_absolute() and (val.query or val.fragment):
val = val._replace(path="/")
return urlunsplit(val)
return _urlunsplit(val)

def __repr__(self):
return f"{self.__class__.__name__}('{str(self)}')"
Expand Down Expand Up @@ -1090,7 +1090,7 @@ def human_repr(self):
for k, v in self.query.items()
)
fragment = _human_quote(self.fragment, "")
return urlunsplit(
return _urlunsplit(
SplitResult(
self.scheme,
self._make_netloc(
Expand All @@ -1107,6 +1107,13 @@ def human_repr(self):
)


def _urlunsplit(s):
url = urlunsplit(s)
if s.scheme == "file" and not s.path.startswith("/"):
url = url.replace("///", "")
return url


def _human_quote(s, unsafe):
if not s:
return s
Expand Down