Skip to content

Commit

Permalink
Better handling of file URLs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
blueyed authored and xavfernandez committed Dec 12, 2018
1 parent b7af5da commit eb02438
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ Changelog

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

* 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.


18.0 - 2018-09-26
~~~~~~~~~~~~~~~~~
Expand Down
5 changes: 4 additions & 1 deletion packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def __init__(self, requirement_string):
self.name = req.name
if req.url:
parsed_url = urlparse.urlparse(req.url)
if not (parsed_url.scheme and parsed_url.netloc) or (
if parsed_url.scheme == 'file':
if urlparse.urlunparse(parsed_url) != req.url:
raise InvalidRequirement("Invalid URL given")
elif not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc):
raise InvalidRequirement("Invalid URL: {0}".format(req.url))
self.url = req.url
Expand Down
12 changes: 12 additions & 0 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ def test_invalid_url(self):
assert "Invalid URL: " in str(e)
assert "gopher:/foo/com" in str(e)

def test_file_url(self):
req = Requirement("name @ file:///absolute/path")
self._assert_requirement(req, "name", "file:///absolute/path")
req = Requirement("name @ file://.")
self._assert_requirement(req, "name", "file://.")

def test_invalid_file_urls(self):
with pytest.raises(InvalidRequirement):
Requirement("name @ file:.")
with pytest.raises(InvalidRequirement):
Requirement("name @ file:/.")

def test_extras_and_url_and_marker(self):
req = Requirement(
"name [fred,bar] @ http://foo.com ; python_version=='2.7'")
Expand Down

0 comments on commit eb02438

Please sign in to comment.