Skip to content

Commit acdd06e

Browse files
finally listen to the maintainer and cache parse_links() by url :)
1 parent 09e7cd9 commit acdd06e

File tree

2 files changed

+10
-40
lines changed

2 files changed

+10
-40
lines changed

src/pip/_internal/index/collector.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,11 @@ def __init__(self, page):
281281
def __eq__(self, other):
282282
# type: (object) -> bool
283283
return (isinstance(other, type(self)) and
284-
self.page.content == other.page.content and
285-
self.page.encoding == other.page.encoding)
284+
self.page.url == other.page.url)
286285

287286
def __hash__(self):
288287
# type: () -> int
289-
return hash((self.page.content, self.page.encoding))
288+
return hash(self.page.url)
290289

291290

292291
def with_cached_html_pages(
@@ -371,20 +370,6 @@ def _make_html_page(response):
371370
return HTMLPage(response.content, encoding=encoding, url=response.url)
372371

373372

374-
def with_cached_link_fetch(
375-
fn, # type: Callable[..., Optional[HTMLPage]]
376-
):
377-
# type: (...) -> Callable[..., Optional[HTMLPage]]
378-
379-
@_lru_cache(maxsize=None)
380-
def wrapper(link, session=None):
381-
# type: (Link, Optional[PipSession]) -> Optional[HTMLPage]
382-
return fn(link, session=session)
383-
384-
return wrapper
385-
386-
387-
@with_cached_link_fetch
388373
def _get_html_page(link, session=None):
389374
# type: (Link, Optional[PipSession]) -> Optional[HTMLPage]
390375
if session is None:

tests/unit/test_collector.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os.path
3+
import uuid
34
from textwrap import dedent
45

56
import mock
@@ -10,7 +11,6 @@
1011
from pip._vendor.six.moves.urllib import request as urllib_request
1112

1213
from pip._internal.index.collector import (
13-
CacheablePageContent,
1414
HTMLPage,
1515
_clean_link,
1616
_determine_base_url,
@@ -270,15 +270,17 @@ def test_parse_links__yanked_reason(anchor_html, expected):
270270
page = HTMLPage(
271271
html_bytes,
272272
encoding=None,
273-
url='https://example.com/simple/',
273+
# parse_links() is cached by url, so we inject a random uuid to ensure
274+
# the page content isn't cached.
275+
url='https://example.com/simple-{}/'.format(uuid.uuid4()),
274276
)
275277
links = list(parse_links(page))
276278
link, = links
277279
actual = link.yanked_reason
278280
assert actual == expected
279281

280282

281-
def test_parse_links_caches_same_page():
283+
def test_parse_links_caches_same_page_by_url():
282284
html = (
283285
# Mark this as a unicode string for Python 2 since anchor_html
284286
# can contain non-ascii.
@@ -292,8 +294,10 @@ def test_parse_links_caches_same_page():
292294
encoding=None,
293295
url='https://example.com/simple/',
294296
)
297+
# Make a second page with zero content, to ensure that it's not accessed,
298+
# because the page was cached by url.
295299
page_2 = HTMLPage(
296-
html_bytes,
300+
b'',
297301
encoding=None,
298302
url='https://example.com/simple/',
299303
)
@@ -378,25 +382,6 @@ def test_get_html_page_invalid_scheme(caplog, url, vcs_scheme):
378382
]
379383

380384

381-
def test_get_html_page_caches_same_link():
382-
link = Link('https://example.com/link-1/')
383-
session = mock.Mock(PipSession)
384-
385-
fake_response = make_fake_html_response(link.url)
386-
mock_func = mock.patch("pip._internal.index.collector._get_html_response")
387-
with mock_func as mock_func:
388-
mock_func.return_value = fake_response
389-
page_1 = _get_html_page(link, session=session)
390-
mock_func.assert_called_once()
391-
392-
with mock_func as mock_func:
393-
page_2 = _get_html_page(link, session=session)
394-
# Assert that the result of the cached html page fetch will also then
395-
# be cached by parse_links() and @with_cached_html_pages.
396-
assert CacheablePageContent(page_1) == CacheablePageContent(page_2)
397-
mock_func.assert_not_called()
398-
399-
400385
def make_fake_html_response(url):
401386
"""
402387
Create a fake requests.Response object.

0 commit comments

Comments
 (0)