diff --git a/CHANGES.txt b/CHANGES.txt index 440a750a98a..27211ac8d8e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -51,6 +51,9 @@ * Fixed :issue:`1319`. Allow use of Zip64 extension in Wheels and other zip files. (:pull:`1868`) +* Fixed :issue:`1101`. Properly handle an index or --find-links target which + has a without a href attribute. (:pull:`1869`) + **1.5.7** diff --git a/pip/index.py b/pip/index.py index 903865f4fc5..9b5b4145131 100644 --- a/pip/index.py +++ b/pip/index.py @@ -820,9 +820,12 @@ def api_version(self): @property def base_url(self): if not hasattr(self, "_base_url"): - base = self.parsed.find(".//base") - if base is not None and base.get("href"): - self._base_url = base.get("href") + bases = [ + x for x in self.parsed.findall(".//base") + if x.get("href") is not None + ] + if bases and bases[0].get("href"): + self._base_url = bases[0].get("href") else: self._base_url = self.url return self._base_url diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index ef4b8b8ebd0..7c2f8ca2a9e 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -1,3 +1,5 @@ +import pytest + from pip.download import PipSession from pip.index import package_to_requirement, HTMLPage from pip.index import PackageFinder, Link, INSTALLED_VERSION @@ -80,3 +82,27 @@ def test_ext_fragment(self): def test_ext_query(self): assert '.whl' == Link('http://yo/wheel.whl?a=b').ext + + +@pytest.mark.parametrize( + ("html", "url", "expected"), + [ + ("", "https://example.com/", "https://example.com/"), + ( + "" + "" + "", + "https://example.com/", + "https://foo.example.com/", + ), + ( + "" + "" + "", + "https://example.com/", + "https://foo.example.com/", + ), + ], +) +def test_base_url(html, url, expected): + assert HTMLPage(html, url).base_url == expected