Skip to content

Commit

Permalink
Only do the ensure_text() dance on Windows
Browse files Browse the repository at this point in the history
POSIX is problematic when the environment is not configured properly.
  • Loading branch information
uranusjr committed Oct 27, 2020
1 parent c15f867 commit 5a0118c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions news/8658.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 11 additions & 3 deletions src/pip/_internal/utils/temp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 5a0118c

Please sign in to comment.