Skip to content

Commit 70b3ec0

Browse files
authored
Merge pull request #1959 from hugovk/fix-flake8-2020
Fix for Python 4: replace unsafe six.PY3 with PY2
2 parents 7e97def + b84a099 commit 70b3ec0

12 files changed

+32
-31
lines changed

changelog.d/1959.change.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix for Python 4: replace unsafe six.PY3 with six.PY2

setuptools/command/build_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_ext_filename(self, fullname):
113113
if fullname in self.ext_map:
114114
ext = self.ext_map[fullname]
115115
use_abi3 = (
116-
six.PY3
116+
not six.PY2
117117
and getattr(ext, 'py_limited_api')
118118
and get_abi3_suffix()
119119
)

setuptools/command/develop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _resolve_setup_path(egg_base, install_dir, egg_path):
108108
return path_to_setup
109109

110110
def install_for_development(self):
111-
if six.PY3 and getattr(self.distribution, 'use_2to3', False):
111+
if not six.PY2 and getattr(self.distribution, 'use_2to3', False):
112112
# If we run 2to3 we can not do this inplace:
113113

114114
# Ensure metadata is up-to-date

setuptools/command/easy_install.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ def get_exe_prefixes(exe_filename):
15671567
continue
15681568
if parts[0].upper() in ('PURELIB', 'PLATLIB'):
15691569
contents = z.read(name)
1570-
if six.PY3:
1570+
if not six.PY2:
15711571
contents = contents.decode()
15721572
for pth in yield_lines(contents):
15731573
pth = pth.strip().replace('\\', '/')

setuptools/command/egg_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def write_file(self, what, filename, data):
266266
to the file.
267267
"""
268268
log.info("writing %s to %s", what, filename)
269-
if six.PY3:
269+
if not six.PY2:
270270
data = data.encode("utf-8")
271271
if not self.dry_run:
272272
f = open(filename, 'wb')

setuptools/command/sdist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def read_manifest(self):
207207
manifest = open(self.manifest, 'rb')
208208
for line in manifest:
209209
# The manifest must contain UTF-8. See #303.
210-
if six.PY3:
210+
if not six.PY2:
211211
try:
212212
line = line.decode('UTF-8')
213213
except UnicodeDecodeError:

setuptools/command/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def with_project_on_sys_path(self, func):
129129

130130
@contextlib.contextmanager
131131
def project_on_sys_path(self, include_dists=[]):
132-
with_2to3 = six.PY3 and getattr(self.distribution, 'use_2to3', False)
132+
with_2to3 = not six.PY2 and getattr(self.distribution, 'use_2to3', False)
133133

134134
if with_2to3:
135135
# If we run 2to3 we can not do this inplace:
@@ -240,7 +240,7 @@ def run_tests(self):
240240
# Purge modules under test from sys.modules. The test loader will
241241
# re-import them from the build location. Required when 2to3 is used
242242
# with namespace packages.
243-
if six.PY3 and getattr(self.distribution, 'use_2to3', False):
243+
if not six.PY2 and getattr(self.distribution, 'use_2to3', False):
244244
module = self.test_suite.split('.')[0]
245245
if module in _namespace_packages:
246246
del_modules = []

setuptools/command/upload_docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
def _encode(s):
27-
errors = 'surrogateescape' if six.PY3 else 'strict'
27+
errors = 'strict' if six.PY2 else 'surrogateescape'
2828
return s.encode('utf-8', errors)
2929

3030

@@ -153,7 +153,7 @@ def upload_file(self, filename):
153153
# set up the authentication
154154
credentials = _encode(self.username + ':' + self.password)
155155
credentials = standard_b64encode(credentials)
156-
if six.PY3:
156+
if not six.PY2:
157157
credentials = credentials.decode('ascii')
158158
auth = "Basic " + credentials
159159

setuptools/dist.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def _parse_config_files(self, filenames=None):
571571
from setuptools.extern.six.moves.configparser import ConfigParser
572572

573573
# Ignore install directory options if we have a venv
574-
if six.PY3 and sys.prefix != sys.base_prefix:
574+
if not six.PY2 and sys.prefix != sys.base_prefix:
575575
ignore_options = [
576576
'install-base', 'install-platbase', 'install-lib',
577577
'install-platlib', 'install-purelib', 'install-headers',
@@ -593,7 +593,7 @@ def _parse_config_files(self, filenames=None):
593593
with io.open(filename, encoding='utf-8') as reader:
594594
if DEBUG:
595595
self.announce(" reading {filename}".format(**locals()))
596-
(parser.read_file if six.PY3 else parser.readfp)(reader)
596+
(parser.readfp if six.PY2 else parser.read_file)(reader)
597597
for section in parser.sections():
598598
options = parser.options(section)
599599
opt_dict = self.get_option_dict(section)
@@ -636,7 +636,7 @@ def _try_str(val):
636636
637637
Ref #1653
638638
"""
639-
if six.PY3:
639+
if not six.PY2:
640640
return val
641641
try:
642642
return val.encode()

setuptools/tests/test_develop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_2to3_user_mode(self, test_env):
9595
with io.open(fn) as init_file:
9696
init = init_file.read().strip()
9797

98-
expected = 'print("foo")' if six.PY3 else 'print "foo"'
98+
expected = 'print "foo"' if six.PY2 else 'print("foo")'
9999
assert init == expected
100100

101101
def test_console_scripts(self, tmpdir):
@@ -161,7 +161,7 @@ def install_develop(src_dir, target):
161161
reason="https://github.com/pypa/setuptools/issues/851",
162162
)
163163
@pytest.mark.skipif(
164-
platform.python_implementation() == 'PyPy' and six.PY3,
164+
platform.python_implementation() == 'PyPy' and not six.PY2,
165165
reason="https://github.com/pypa/setuptools/issues/1202",
166166
)
167167
def test_namespace_package_importable(self, tmpdir):

setuptools/tests/test_sdist.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def quiet():
5151

5252
# Convert to POSIX path
5353
def posix(path):
54-
if six.PY3 and not isinstance(path, str):
54+
if not six.PY2 and not isinstance(path, str):
5555
return path.replace(os.sep.encode('ascii'), b'/')
5656
else:
5757
return path.replace(os.sep, '/')
@@ -329,7 +329,7 @@ def test_manifest_is_read_with_utf8_encoding(self):
329329
cmd.read_manifest()
330330

331331
# The filelist should contain the UTF-8 filename
332-
if six.PY3:
332+
if not six.PY2:
333333
filename = filename.decode('utf-8')
334334
assert filename in cmd.filelist.files
335335

@@ -383,7 +383,7 @@ def test_sdist_with_utf8_encoded_filename(self):
383383
if sys.platform == 'darwin':
384384
filename = decompose(filename)
385385

386-
if six.PY3:
386+
if not six.PY2:
387387
fs_enc = sys.getfilesystemencoding()
388388

389389
if sys.platform == 'win32':
@@ -425,7 +425,19 @@ def test_sdist_with_latin1_encoded_filename(self):
425425
with quiet():
426426
cmd.run()
427427

428-
if six.PY3:
428+
if six.PY2:
429+
# Under Python 2 there seems to be no decoded string in the
430+
# filelist. However, due to decode and encoding of the
431+
# file name to get utf-8 Manifest the latin1 maybe excluded
432+
try:
433+
# fs_enc should match how one is expect the decoding to
434+
# be proformed for the manifest output.
435+
fs_enc = sys.getfilesystemencoding()
436+
filename.decode(fs_enc)
437+
assert filename in cmd.filelist.files
438+
except UnicodeDecodeError:
439+
filename not in cmd.filelist.files
440+
else:
429441
# not all windows systems have a default FS encoding of cp1252
430442
if sys.platform == 'win32':
431443
# Latin-1 is similar to Windows-1252 however
@@ -440,18 +452,6 @@ def test_sdist_with_latin1_encoded_filename(self):
440452
# The Latin-1 filename should have been skipped
441453
filename = filename.decode('latin-1')
442454
filename not in cmd.filelist.files
443-
else:
444-
# Under Python 2 there seems to be no decoded string in the
445-
# filelist. However, due to decode and encoding of the
446-
# file name to get utf-8 Manifest the latin1 maybe excluded
447-
try:
448-
# fs_enc should match how one is expect the decoding to
449-
# be proformed for the manifest output.
450-
fs_enc = sys.getfilesystemencoding()
451-
filename.decode(fs_enc)
452-
assert filename in cmd.filelist.files
453-
except UnicodeDecodeError:
454-
filename not in cmd.filelist.files
455455

456456
def test_pyproject_toml_in_sdist(self, tmpdir):
457457
"""

setuptools/tests/test_setopt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestEdit:
1515
def parse_config(filename):
1616
parser = configparser.ConfigParser()
1717
with io.open(filename, encoding='utf-8') as reader:
18-
(parser.read_file if six.PY3 else parser.readfp)(reader)
18+
(parser.readfp if six.PY2 else parser.read_file)(reader)
1919
return parser
2020

2121
@staticmethod

0 commit comments

Comments
 (0)