Skip to content

Commit

Permalink
improve handling of file URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-pierre committed Oct 18, 2018
1 parent c30b10a commit fe7584b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/5892.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve handling of file URIs: correctly handle `file://localhost/...` and don't try to use UNC paths on Unix.
13 changes: 11 additions & 2 deletions src/pip/_internal/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,18 @@ def url_to_path(url):

_, netloc, path, _, _ = urllib_parse.urlsplit(url)

# if we have a UNC path, prepend UNC share notation
if netloc:
netloc = '\\\\' + netloc
if netloc == 'localhost':
# According to RFC 8089, same as empty authority.
netloc = ''
elif sys.platform == 'win32':
# If we have a UNC path, prepend UNC share notation.
netloc = '\\\\' + netloc
else:
raise ValueError(
'non-local file URIs are not supported on this platform: %r'
% url
)

path = urllib_request.url2pathname(netloc + path)
return path
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ def test_path_to_url_unix():

@pytest.mark.skipif("sys.platform == 'win32'")
def test_url_to_path_unix():
assert url_to_path('file:tmp') == 'tmp'
assert url_to_path('file:///tmp/file') == '/tmp/file'
assert url_to_path('file:/path/to/file') == '/path/to/file'
assert url_to_path('file://localhost/tmp/file') == '/tmp/file'
with pytest.raises(ValueError):
url_to_path('file://somehost/tmp/file')


@pytest.mark.skipif("sys.platform != 'win32'")
Expand All @@ -141,8 +146,11 @@ def test_path_to_url_win():

@pytest.mark.skipif("sys.platform != 'win32'")
def test_url_to_path_win():
assert url_to_path('file:///c:/tmp/file') == 'C:\\tmp\\file'
assert url_to_path('file:tmp') == 'tmp'
assert url_to_path('file:///c:/tmp/file') == r'C:\tmp\file'
assert url_to_path('file://unc/as/path') == r'\\unc\as\path'
assert url_to_path('file:c:/path/to/file') == r'C:\path\to\file'
assert url_to_path('file://localhost/c:/tmp/file') == r'C:\tmp\file'


@pytest.mark.skipif("sys.platform != 'win32'")
Expand Down

0 comments on commit fe7584b

Please sign in to comment.