Skip to content

Commit a0ec4be

Browse files
authored
Merge pull request #8666 from uranusjr/rmtree-unicode-use-file-system-encoding
Only do the ensure_text() dance on Windows
2 parents 9a5441c + 94fbb6c commit a0ec4be

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

news/8658.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Only converts Windows path to unicode on Python 2 to avoid regressions when a
2+
POSIX environment does not configure the file system encoding correctly.

src/pip/_internal/utils/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def get_prog():
137137
# Retry every half second for up to 3 seconds
138138
@retry(stop_max_delay=3000, wait_fixed=500)
139139
def rmtree(dir, ignore_errors=False):
140-
# type: (Text, bool) -> None
140+
# type: (AnyStr, bool) -> None
141141
shutil.rmtree(dir, ignore_errors=ignore_errors,
142142
onerror=rmtree_errorhandler)
143143

src/pip/_internal/utils/temp_dir.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pip._vendor.contextlib2 import ExitStack
1111
from pip._vendor.six import ensure_text
1212

13+
from pip._internal.utils.compat import WINDOWS
1314
from pip._internal.utils.misc import enum, rmtree
1415
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1516

@@ -195,10 +196,17 @@ def cleanup(self):
195196
"""Remove the temporary directory created and reset state
196197
"""
197198
self._deleted = True
198-
if os.path.exists(self._path):
199-
# Make sure to pass unicode on Python 2 to make the contents also
200-
# use unicode, ensuring non-ASCII names and can be represented.
199+
if not os.path.exists(self._path):
200+
return
201+
# Make sure to pass unicode on Python 2 to make the contents also
202+
# use unicode, ensuring non-ASCII names and can be represented.
203+
# This is only done on Windows because POSIX platforms use bytes
204+
# natively for paths, and the bytes-text conversion omission avoids
205+
# errors caused by the environment configuring encodings incorrectly.
206+
if WINDOWS:
201207
rmtree(ensure_text(self._path))
208+
else:
209+
rmtree(self._path)
202210

203211

204212
class AdjacentTempDirectory(TempDirectory):

0 commit comments

Comments
 (0)