Skip to content

Commit eb02438

Browse files
blueyedxavfernandez
authored andcommitted
Better handling of file URLs
This allows for using `file:///absolute/path`, which was previously prevented due to the missing `netloc`. This allows for all file URLs that `urlunparse` turns back into the original URL to be valid.
1 parent b7af5da commit eb02438

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.rst

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Changelog
77

88
* Fix string representation of PEP 508 direct URL requirements with markers.
99

10+
* Better handling of file URLs
11+
12+
This allows for using ``file:///absolute/path``, which was previously
13+
prevented due to the missing ``netloc``.
14+
15+
This allows for all file URLs that ``urlunparse`` turns back into the
16+
original URL to be valid.
17+
1018

1119
18.0 - 2018-09-26
1220
~~~~~~~~~~~~~~~~~

packaging/requirements.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def __init__(self, requirement_string):
9999
self.name = req.name
100100
if req.url:
101101
parsed_url = urlparse.urlparse(req.url)
102-
if not (parsed_url.scheme and parsed_url.netloc) or (
102+
if parsed_url.scheme == 'file':
103+
if urlparse.urlunparse(parsed_url) != req.url:
104+
raise InvalidRequirement("Invalid URL given")
105+
elif not (parsed_url.scheme and parsed_url.netloc) or (
103106
not parsed_url.scheme and not parsed_url.netloc):
104107
raise InvalidRequirement("Invalid URL: {0}".format(req.url))
105108
self.url = req.url

tests/test_requirements.py

+12
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ def test_invalid_url(self):
107107
assert "Invalid URL: " in str(e)
108108
assert "gopher:/foo/com" in str(e)
109109

110+
def test_file_url(self):
111+
req = Requirement("name @ file:///absolute/path")
112+
self._assert_requirement(req, "name", "file:///absolute/path")
113+
req = Requirement("name @ file://.")
114+
self._assert_requirement(req, "name", "file://.")
115+
116+
def test_invalid_file_urls(self):
117+
with pytest.raises(InvalidRequirement):
118+
Requirement("name @ file:.")
119+
with pytest.raises(InvalidRequirement):
120+
Requirement("name @ file:/.")
121+
110122
def test_extras_and_url_and_marker(self):
111123
req = Requirement(
112124
"name [fred,bar] @ http://foo.com ; python_version=='2.7'")

0 commit comments

Comments
 (0)