diff options
author | Ryan Shepherd <ryansh@microsoft.com> | 2023-03-17 13:36:02 -0700 |
---|---|---|
committer | Stéphane Bidoul <stephane.bidoul@gmail.com> | 2023-04-10 16:45:34 +0200 |
commit | 58882a1642129df8cbdf8ed8fff75d458967afe3 (patch) | |
tree | 59d856b375422fb4c558e65a7c765c45b0853cfd /src | |
parent | 1bb849682dbc0b16770b1b059fb5bccf8ee96f52 (diff) | |
download | pip-58882a1642129df8cbdf8ed8fff75d458967afe3.tar.gz |
Move normalize_path caching to an instance on UninstallPathSet
Diffstat (limited to 'src')
-rw-r--r-- | src/pip/_internal/req/req_uninstall.py | 16 | ||||
-rw-r--r-- | src/pip/_internal/utils/misc.py | 15 |
2 files changed, 11 insertions, 20 deletions
diff --git a/src/pip/_internal/req/req_uninstall.py b/src/pip/_internal/req/req_uninstall.py index 9b5cbf4a0..9136ae930 100644 --- a/src/pip/_internal/req/req_uninstall.py +++ b/src/pip/_internal/req/req_uninstall.py @@ -13,6 +13,7 @@ from pip._internal.utils.egg_link import egg_link_path_from_location from pip._internal.utils.logging import getLogger, indent_log from pip._internal.utils.misc import ask, is_local, normalize_path, normalize_path_cached, renames, rmtree from pip._internal.utils.temp_dir import AdjacentTempDirectory, TempDirectory +from pip._internal.utils.virtualenv import running_under_virtualenv logger = getLogger(__name__) @@ -312,7 +313,7 @@ class UninstallPathSet: self._pth: Dict[str, UninstallPthEntries] = {} self._dist = dist self._moved_paths = StashedUninstallPathSet() - normalize_path_cached.cache_clear() + self._normalize_path_cached = functools.lru_cache(maxsize=256)(normalize_path) def _permitted(self, path: str) -> bool: """ @@ -320,14 +321,17 @@ class UninstallPathSet: remove/modify, False otherwise. """ - return is_local(path) + # aka is_local, but caching normalized sys.prefix + if not running_under_virtualenv(): + return True + return path.startswith(self._normalize_path_cached(sys.prefix)) def add(self, path: str) -> None: head, tail = os.path.split(path) # we normalize the head to resolve parent directory symlinks, but not # the tail, since we only want to uninstall symlinks, not their targets - path = os.path.join(normalize_path_cached(head), os.path.normcase(tail)) + path = os.path.join(self._normalize_path_cached(head), os.path.normcase(tail)) if not os.path.exists(path): return @@ -342,7 +346,7 @@ class UninstallPathSet: self.add(cache_from_source(path)) def add_pth(self, pth_file: str, entry: str) -> None: - pth_file = normalize_path_cached(pth_file) + pth_file = self._normalize_path_cached(pth_file) if self._permitted(pth_file): if pth_file not in self._pth: self._pth[pth_file] = UninstallPthEntries(pth_file) @@ -435,7 +439,7 @@ class UninstallPathSet: ) return cls(dist) - normalized_dist_location = normalize_path_cached(dist_location) + normalized_dist_location = normalize_path(dist_location) if not dist.local: logger.info( "Not uninstalling %s at %s, outside environment %s", @@ -532,7 +536,7 @@ class UninstallPathSet: # above, so this only covers the setuptools-style editable. with open(develop_egg_link) as fh: link_pointer = os.path.normcase(fh.readline().strip()) - normalized_link_pointer = normalize_path_cached(link_pointer) + normalized_link_pointer = paths_to_remove._normalize_path_cached(link_pointer) assert os.path.samefile( normalized_link_pointer, normalized_dist_location ), ( diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py index 50c6e77ac..bfed82702 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -3,7 +3,6 @@ import contextlib import errno -import functools import getpass import hashlib import io @@ -295,17 +294,6 @@ def normalize_path(path: str, resolve_symlinks: bool = True) -> str: return os.path.normcase(path) -@functools.lru_cache(maxsize=128) -def normalize_path_cached(path: str, resolve_symlinks: bool = True) -> str: - """ - Cache the results of normalize_path when called frequently during certain - operations. Separate function because it is probably unsafe to - cache normalize_path in the general case, e.g. symlinks can be changed - while the process is running. - """ - return normalize_path(path, resolve_symlinks) - - def splitext(path: str) -> Tuple[str, str]: """Like os.path.splitext, but take off .tar too""" base, ext = posixpath.splitext(path) @@ -343,8 +331,7 @@ def is_local(path: str) -> bool: """ if not running_under_virtualenv(): return True - # Safe to call cached because sys.prefix shouldn't change - return path.startswith(normalize_path_cached(sys.prefix)) + return path.startswith(normalize_path(sys.prefix)) def write_output(msg: Any, *args: Any) -> None: |