From 1135ac041d253122cf9791a0978332737f449195 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 24 Sep 2020 15:51:44 +0800 Subject: [PATCH] Move lru_cache to utils for reuse --- src/pip/_internal/index/collector.py | 26 ++------------------------ src/pip/_internal/utils/compat.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/pip/_internal/index/collector.py b/src/pip/_internal/index/collector.py index e6230c767..7b9abbf69 100644 --- a/src/pip/_internal/index/collector.py +++ b/src/pip/_internal/index/collector.py @@ -21,6 +21,7 @@ from pip._internal.exceptions import NetworkConnectionError from pip._internal.models.link import Link from pip._internal.models.search_scope import SearchScope from pip._internal.network.utils import raise_for_status +from pip._internal.utils.compat import lru_cache from pip._internal.utils.filetypes import ARCHIVE_EXTENSIONS from pip._internal.utils.misc import pairwise, redact_auth_from_url from pip._internal.utils.typing import MYPY_CHECK_RUNNING @@ -36,10 +37,8 @@ if MYPY_CHECK_RUNNING: List, MutableMapping, Optional, - Protocol, Sequence, Tuple, - TypeVar, Union, ) @@ -50,31 +49,10 @@ if MYPY_CHECK_RUNNING: HTMLElement = xml.etree.ElementTree.Element ResponseHeaders = MutableMapping[str, str] - # Used in the @lru_cache polyfill. - F = TypeVar('F') - - class LruCache(Protocol): - def __call__(self, maxsize=None): - # type: (Optional[int]) -> Callable[[F], F] - raise NotImplementedError - logger = logging.getLogger(__name__) -# Fallback to noop_lru_cache in Python 2 -# TODO: this can be removed when python 2 support is dropped! -def noop_lru_cache(maxsize=None): - # type: (Optional[int]) -> Callable[[F], F] - def _wrapper(f): - # type: (F) -> F - return f - return _wrapper - - -_lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache - - def _match_vcs_scheme(url): # type: (str) -> Optional[str] """Look for VCS schemes in the URL. @@ -344,7 +322,7 @@ def with_cached_html_pages( `page` has `page.cache_link_parsing == False`. """ - @_lru_cache(maxsize=None) + @lru_cache(maxsize=None) def wrapper(cacheable_page): # type: (CacheablePageContent) -> List[Link] return list(fn(cacheable_page.page)) diff --git a/src/pip/_internal/utils/compat.py b/src/pip/_internal/utils/compat.py index cc6353678..2196e6e0a 100644 --- a/src/pip/_internal/utils/compat.py +++ b/src/pip/_internal/utils/compat.py @@ -7,6 +7,7 @@ distributions.""" from __future__ import absolute_import, division import codecs +import functools import locale import logging import os @@ -18,7 +19,15 @@ from pip._vendor.six import PY2, text_type from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: - from typing import Optional, Text, Tuple, Union + from typing import Callable, Optional, Protocol, Text, Tuple, TypeVar, Union + + # Used in the @lru_cache polyfill. + F = TypeVar('F') + + class LruCache(Protocol): + def __call__(self, maxsize=None): + # type: (Optional[int]) -> Callable[[F], F] + raise NotImplementedError try: import ipaddress @@ -269,3 +278,16 @@ else: if not cr: cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80)) return int(cr[1]), int(cr[0]) + + +# Fallback to noop_lru_cache in Python 2 +# TODO: this can be removed when python 2 support is dropped! +def noop_lru_cache(maxsize=None): + # type: (Optional[int]) -> Callable[[F], F] + def _wrapper(f): + # type: (F) -> F + return f + return _wrapper + + +lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache