From 94fbb6cf78c267bf7cdf83eeeb2536ad56cfe639 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Fri, 31 Jul 2020 07:58:28 +0800 Subject: [PATCH] Only do the ensure_text() dance on Windows POSIX is problematic when the environment is not configured properly. --- news/8658.bugfix.rst | 2 ++ src/pip/_internal/utils/misc.py | 2 +- src/pip/_internal/utils/temp_dir.py | 14 +++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 news/8658.bugfix.rst diff --git a/news/8658.bugfix.rst b/news/8658.bugfix.rst new file mode 100644 index 00000000000..6e43c8b3c0e --- /dev/null +++ b/news/8658.bugfix.rst @@ -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 c122beb32bb..4fb64d2672a 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -137,7 +137,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..487f40db51f 100644 --- a/src/pip/_internal/utils/temp_dir.py +++ b/src/pip/_internal/utils/temp_dir.py @@ -10,6 +10,7 @@ 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 +194,17 @@ 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. + 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: rmtree(ensure_text(self._path)) + else: + rmtree(self._path) class AdjacentTempDirectory(TempDirectory):