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 Jul 30, 2020
1 parent 31299ee commit fbea9f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions news/8658.bugfix
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 @@ -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)

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

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

0 comments on commit fbea9f8

Please sign in to comment.