Skip to content

Commit dd35f86

Browse files
committed
Fix for Python 4: replace unsafe six.PY3 with PY2
1 parent 11ec2a3 commit dd35f86

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/pip/_internal/utils/compat.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import shutil
1010
import sys
1111

12-
from pip._vendor.six import PY3, text_type
12+
from pip._vendor.six import PY2, text_type
1313
from pip._vendor.urllib3.util import IS_PYOPENSSL
1414

1515
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -47,10 +47,7 @@
4747

4848
HAS_TLS = (ssl is not None) or IS_PYOPENSSL
4949

50-
if PY3:
51-
uses_pycache = True
52-
from importlib.util import cache_from_source
53-
else:
50+
if PY2:
5451
import imp
5552

5653
try:
@@ -60,11 +57,12 @@
6057
cache_from_source = None
6158

6259
uses_pycache = cache_from_source is not None
60+
else:
61+
uses_pycache = True
62+
from importlib.util import cache_from_source
6363

6464

65-
if PY3:
66-
backslashreplace_decode = "backslashreplace"
67-
else:
65+
if PY2:
6866
# In version 3.4 and older, backslashreplace exists
6967
# but does not support use for decoding.
7068
# We implement our own replace handler for this
@@ -80,7 +78,8 @@ def backslashreplace_decode_fn(err):
8078
backslashreplace_decode_fn,
8179
)
8280
backslashreplace_decode = "backslashreplace_decode"
83-
81+
else:
82+
backslashreplace_decode = "backslashreplace"
8483

8584
def str_to_display(data, desc=None):
8685
# type: (Union[bytes, Text], Optional[str]) -> Text
@@ -155,19 +154,19 @@ def console_to_str(data):
155154
return str_to_display(data, desc='Subprocess output')
156155

157156

158-
if PY3:
157+
if PY2:
159158
def native_str(s, replace=False):
160159
# type: (str, bool) -> str
161-
if isinstance(s, bytes):
162-
return s.decode('utf-8', 'replace' if replace else 'strict')
160+
# Replace is ignored -- unicode to UTF-8 can't fail
161+
if isinstance(s, text_type):
162+
return s.encode('utf-8')
163163
return s
164164

165165
else:
166166
def native_str(s, replace=False):
167167
# type: (str, bool) -> str
168-
# Replace is ignored -- unicode to UTF-8 can't fail
169-
if isinstance(s, text_type):
170-
return s.encode('utf-8')
168+
if isinstance(s, bytes):
169+
return s.decode('utf-8', 'replace' if replace else 'strict')
171170
return s
172171

173172

@@ -201,16 +200,17 @@ def get_path_uid(path):
201200
return file_uid
202201

203202

204-
if PY3:
205-
from importlib.machinery import EXTENSION_SUFFIXES
203+
if PY2:
204+
from imp import get_suffixes
206205

207206
def get_extension_suffixes():
208-
return EXTENSION_SUFFIXES
207+
return [suffix[0] for suffix in get_suffixes()]
208+
209209
else:
210-
from imp import get_suffixes
210+
from importlib.machinery import EXTENSION_SUFFIXES
211211

212212
def get_extension_suffixes():
213-
return [suffix[0] for suffix in get_suffixes()]
213+
return EXTENSION_SUFFIXES
214214

215215

216216
def expanduser(path):

0 commit comments

Comments
 (0)