diff --git a/news/8658.bugfix b/news/8658.bugfix new file mode 100644 index 00000000000..6e43c8b3c0e --- /dev/null +++ b/news/8658.bugfix @@ -0,0 +1,2 @@ +Only converts Windows path to unicode on Python 2 to avoid regressions when a +POSIX environment does not configure the file system encoding correctly. diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py index 24a7455628d..80b90c81816 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -134,7 +134,7 @@ def get_prog(): # Retry every half second for up to 3 seconds @retry(stop_max_delay=3000, wait_fixed=500) def rmtree(dir, ignore_errors=False): - # type: (Text, bool) -> None + # type: (AnyStr, bool) -> None shutil.rmtree(dir, ignore_errors=ignore_errors, onerror=rmtree_errorhandler) diff --git a/src/pip/_internal/utils/temp_dir.py b/src/pip/_internal/utils/temp_dir.py index 03aa8286670..d2c42feea28 100644 --- a/src/pip/_internal/utils/temp_dir.py +++ b/src/pip/_internal/utils/temp_dir.py @@ -4,12 +4,14 @@ import itertools import logging import os.path +import sys import tempfile from contextlib import contextmanager from pip._vendor.contextlib2 import ExitStack from pip._vendor.six import ensure_text +from pip._internal.utils.compat import WINDOWS from pip._internal.utils.misc import enum, rmtree from pip._internal.utils.typing import MYPY_CHECK_RUNNING @@ -193,10 +195,18 @@ def cleanup(self): """Remove the temporary directory created and reset state """ self._deleted = True - if os.path.exists(self._path): - # Make sure to pass unicode on Python 2 to make the contents also - # use unicode, ensuring non-ASCII names and can be represented. - rmtree(ensure_text(self._path)) + if not os.path.exists(self._path): + return + # Make sure to pass unicode on Python 2 to make the contents also + # use unicode, ensuring non-ASCII names and can be represented. + # This is only done on Windows because POSIX platforms use bytes + # natively for paths, and the bytes-text conversion omission avoids + # errors caused by the environment configuring encodings incorrectly. + if WINDOWS: + encoding = sys.getfilesystemencoding() + rmtree(ensure_text(self._path, encoding=encoding)) + else: + rmtree(self._path) class AdjacentTempDirectory(TempDirectory):