Skip to content

Commit 743d82b

Browse files
authored
Merge pull request swiftlang#41902 from dduan/dd/delete-six
[utils] Remove Python 2
2 parents e2d6f5a + 0250794 commit 743d82b

28 files changed

+78
-289
lines changed

utils/GYBUnicodeDataUtils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,6 @@ def map_index(idx):
355355
else:
356356
return idx
357357

358-
# NOTE: Python 2's `map` function returns a list. Where Python 3's
359-
# `map` function returns an iterator. To work around this the
360-
# result of the `map` is explicitly converted to a `list`.
361358
return list(map(map_index, indexes))
362359

363360
# If self.bmp_data contains identical data blocks, keep the first one,

utils/backtrace-check

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ def main():
7272
found_stack_trace_start = False
7373
found_stack_trace_entry = False
7474
for line in lines:
75-
# In Python 2, string objects can contain Unicode characters.
76-
if sys.version_info.major == 2:
77-
line = line.decode('utf-8', 'replace')
78-
7975
line = line.rstrip('\n')
8076

8177
# First see if we found the start of our stack trace start. If so, set

utils/build-script

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
# This source file is part of the Swift.org open source project
44
#
@@ -32,8 +32,6 @@ from build_swift.build_swift.constants import SWIFT_BUILD_ROOT
3232
from build_swift.build_swift.constants import SWIFT_REPO_NAME
3333
from build_swift.build_swift.constants import SWIFT_SOURCE_ROOT
3434

35-
import six
36-
3735
from swift_build_support.swift_build_support import build_script_invocation
3836
from swift_build_support.swift_build_support import shell
3937
from swift_build_support.swift_build_support import targets
@@ -120,7 +118,7 @@ class JSONDumper(json.JSONEncoder):
120118
def default(self, o):
121119
if hasattr(o, '__dict__'):
122120
return vars(o)
123-
return six.text_type(o)
121+
return str(o)
124122

125123

126124
def print_xcodebuild_versions(file=sys.stdout):
@@ -499,7 +497,7 @@ def main_preset():
499497
try:
500498
preset_parser.read_files(args.preset_file_names)
501499
except presets.PresetError as e:
502-
fatal_error(six.text_type(e))
500+
fatal_error(str(e))
503501

504502
if args.show_presets:
505503
for name in sorted(preset_parser.preset_names,
@@ -520,7 +518,7 @@ def main_preset():
520518
args.preset,
521519
vars=args.preset_substitutions)
522520
except presets.PresetError as e:
523-
fatal_error(six.text_type(e))
521+
fatal_error(str(e))
524522

525523
preset_args = migration.migrate_swift_sdks(preset.args)
526524

utils/build_swift/build_swift/argparse/actions.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import argparse
1919
import copy
2020

21-
import six
22-
2321
from .types import BoolType, PathType
2422

2523

@@ -81,7 +79,7 @@ def __init__(self,
8179
if dests == argparse.SUPPRESS:
8280
dests = []
8381
metavar = metavar or ''
84-
elif isinstance(dests, six.string_types):
82+
elif isinstance(dests, (str,)):
8583
dests = [dests]
8684
metavar = metavar or dests[0].upper()
8785

@@ -138,7 +136,7 @@ def __init__(self, option_strings, join=None, **kwargs):
138136
**kwargs)
139137

140138
def __call__(self, parser, namespace, values, option_string=None):
141-
if isinstance(values, six.string_types):
139+
if isinstance(values, (str,)):
142140
values = [values]
143141

144142
for dest in self.dests:
@@ -343,5 +341,5 @@ def __call__(self, parser, namespace, values, option_string=None):
343341
if self.message is not None:
344342
parser.error(self.message)
345343

346-
arg = option_string or six.text_type(values)
344+
arg = option_string or str(values)
347345
parser.error('unsupported argument: {}'.format(arg))

utils/build_swift/build_swift/argparse/parser.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import argparse
2020
from contextlib import contextmanager
2121

22-
import six
23-
2422
from . import Namespace, SUPPRESS, actions
2523
from .actions import Action
2624

@@ -133,7 +131,7 @@ def thunk(**kwargs):
133131
*names, action=action, **kwargs)
134132

135133
def add_positional(self, dests, action=None, **kwargs):
136-
if isinstance(dests, six.string_types):
134+
if isinstance(dests, (str,)):
137135
dests = [dests]
138136

139137
if any(dest.startswith('-') for dest in dests):
@@ -145,7 +143,7 @@ def add_positional(self, dests, action=None, **kwargs):
145143
return self._add_argument(dests, action, **kwargs)
146144

147145
def add_option(self, option_strings, *actions, **kwargs):
148-
if isinstance(option_strings, six.string_types):
146+
if isinstance(option_strings, (str,)):
149147
option_strings = [option_strings]
150148

151149
if not all(opt.startswith('-') for opt in option_strings):

utils/build_swift/build_swift/argparse/types.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import re
2020
import shlex
2121

22-
import six
23-
2422
from . import ArgumentTypeError
2523
from ..versions import Version
2624

@@ -42,7 +40,7 @@ def _repr(cls, args):
4240
"""
4341

4442
_args = []
45-
for key, value in six.iteritems(args):
43+
for key, value in args.items():
4644
_args.append('{}={}'.format(key, repr(value)))
4745

4846
return '{}({})'.format(type(cls).__name__, ', '.join(_args))

utils/build_swift/build_swift/migration.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import itertools
1818
import subprocess
1919

20-
import six
21-
from six.moves import map
22-
2320
from swift_build_support.swift_build_support.targets import \
2421
StdlibDeploymentTarget
2522

@@ -137,4 +134,4 @@ def check_impl_args(build_script_impl, args):
137134
_, err = pipe.communicate()
138135

139136
if pipe.returncode != 0:
140-
raise ValueError(six.text_type(err.splitlines()[0].decode()))
137+
raise ValueError(str(err.splitlines()[0].decode()))

utils/build_swift/build_swift/presets.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414

1515
from __future__ import absolute_import, unicode_literals
1616

17+
import configparser
1718
import functools
1819
import io
1920
from collections import OrderedDict, namedtuple
2021

21-
from six import StringIO
22-
from six.moves import configparser
23-
2422
from . import class_utils
2523

2624

@@ -313,14 +311,9 @@ def read_string(self, string):
313311
"""Reads and parses a string containing preset definintions.
314312
"""
315313

316-
fp = StringIO(string)
317-
318-
# ConfigParser changes drastically from Python 2 to 3
319-
if hasattr(self._parser, 'read_file'):
320-
self._parser.read_file(fp)
321-
else:
322-
self._parser.readfp(fp)
314+
fp = io.StringIO(string)
323315

316+
self._parser.read_file(fp)
324317
self._parse_raw_presets()
325318

326319
# -------------------------------------------------------------------------

utils/build_swift/build_swift/shell.py

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,11 @@
2424
import subprocess
2525
import sys
2626
from copy import copy as _copy
27+
from pathlib import Path
28+
from pipes import quote as _quote
2729
from shlex import split
2830
from subprocess import CalledProcessError
2931

30-
import six
31-
from six.moves import map
32-
33-
34-
try:
35-
# Python 2
36-
from pipes import quote as _quote
37-
except ImportError:
38-
from shutil import quote as _quote
39-
40-
41-
try:
42-
# Python 3.4
43-
from pathlib import Path
44-
except ImportError:
45-
Path = None
46-
4732

4833
__all__ = [
4934
'CalledProcessError',
@@ -111,7 +96,7 @@ def _convert_pathlib_path(path):
11196
return path
11297

11398
if isinstance(path, Path):
114-
return six.text_type(path)
99+
return str(path)
115100

116101
return path
117102

@@ -150,14 +135,14 @@ def _normalize_args(args):
150135
CommandWrapper instances into a one-dimensional list of strings.
151136
"""
152137

153-
if isinstance(args, six.string_types):
138+
if isinstance(args, (str,)):
154139
return shlex.split(args)
155140

156141
def normalize_arg(arg):
157142
arg = _convert_pathlib_path(arg)
158143

159-
if isinstance(arg, six.string_types):
160-
return [six.text_type(arg)]
144+
if isinstance(arg, (str,)):
145+
return [str(arg)]
161146
if isinstance(arg, AbstractWrapper):
162147
return list(map(_convert_pathlib_path, arg.command))
163148

@@ -173,42 +158,14 @@ def normalize_arg(arg):
173158
# -----------------------------------------------------------------------------
174159
# Decorators
175160

176-
def _backport_devnull(func):
177-
"""Decorator used to backport the subprocess.DEVNULL functionality from
178-
Python 3 to Python 2.
179-
"""
180-
181-
# DEVNULL was introduced in Python 3.3
182-
if _PY_VERSION >= (3, 3):
183-
return func
184-
185-
@functools.wraps(func)
186-
def wrapper(command, **kwargs):
187-
stdout = kwargs.get('stdout', sys.stdout)
188-
stderr = kwargs.get('stderr', sys.stderr)
189-
190-
if stdout != DEVNULL and stderr != DEVNULL:
191-
return func(command, **kwargs)
192-
193-
with open(os.devnull, 'w') as devnull:
194-
if stdout == DEVNULL:
195-
kwargs['stdout'] = devnull
196-
if stderr == DEVNULL:
197-
kwargs['stderr'] = devnull
198-
199-
return func(command, **kwargs)
200-
201-
return wrapper
202-
203-
204161
def _normalize_command(func):
205162
"""Decorator used to uniformly normalize the input command of the
206163
subprocess wrappers.
207164
"""
208165

209166
@functools.wraps(func)
210167
def wrapper(command, **kwargs):
211-
if not isinstance(command, six.string_types):
168+
if not isinstance(command, (str,)):
212169
command = _normalize_args(command)
213170

214171
return func(command, **kwargs)
@@ -237,10 +194,9 @@ def wrapper(command, **kwargs):
237194
# Public Functions
238195

239196
def quote(command):
240-
"""Extension of the standard pipes.quote (Python 2) or shutil.quote
241-
(Python 3) that handles both strings and lists of strings. This mirrors
242-
how the subprocess package can handle commands as both a standalone string
243-
or list of strings.
197+
"""Extension of the standard shutil.quote that handles both strings and
198+
lists of strings. This mirrors how the subprocess package can handle
199+
commands as both a standalone string or list of strings.
244200
245201
>>> quote('/Applications/App Store.app')
246202
"'/Applications/App Store.app'"
@@ -249,7 +205,7 @@ def quote(command):
249205
"rm -rf '~/Documents/My Homework'"
250206
"""
251207

252-
if isinstance(command, six.string_types):
208+
if isinstance(command, (str,)):
253209
return _quote(command)
254210

255211
if isinstance(command, collections.Iterable):
@@ -288,7 +244,6 @@ def __init__(self, command, **kwargs):
288244
solution to this problem in the form of their `method_decorator`.
289245
"""
290246

291-
@_backport_devnull
292247
@_normalize_command
293248
@_add_echo_kwarg
294249
def closure(command, **kwargs):
@@ -305,7 +260,6 @@ def __exit__(self, *exc):
305260
self.wait()
306261

307262

308-
@_backport_devnull
309263
@_normalize_command
310264
@_add_echo_kwarg
311265
def call(command, **kwargs):
@@ -316,7 +270,6 @@ def call(command, **kwargs):
316270
return subprocess.call(command, **kwargs)
317271

318272

319-
@_backport_devnull
320273
@_normalize_command
321274
@_add_echo_kwarg
322275
def check_call(command, **kwargs):
@@ -327,7 +280,6 @@ def check_call(command, **kwargs):
327280
return subprocess.check_call(command, **kwargs)
328281

329282

330-
@_backport_devnull
331283
@_normalize_command
332284
@_add_echo_kwarg
333285
def check_output(command, **kwargs):
@@ -337,16 +289,11 @@ def check_output(command, **kwargs):
337289
Output is returned as a unicode string.
338290
"""
339291

340-
if six.PY3:
341-
kwargs['encoding'] = 'utf-8'
292+
kwargs['encoding'] = 'utf-8'
342293

343294
output = subprocess.check_output(command, **kwargs)
344295

345-
if six.PY3:
346-
return output
347-
348-
# Return unicode string rather than bytes in Python 2.
349-
return six.text_type(output, errors='ignore')
296+
return output
350297

351298

352299
# -----------------------------------------------------------------------------
@@ -484,8 +431,7 @@ def wraps(command):
484431
return CommandWrapper(command)
485432

486433

487-
@six.add_metaclass(abc.ABCMeta)
488-
class AbstractWrapper(object):
434+
class AbstractWrapper(object, metaclass=abc.ABCMeta):
489435
"""Abstract base class for implementing wrappers around command line
490436
utilities and executables. Subclasses must implement the `command` method
491437
which returns a command list suitable for use with executor instances.
@@ -555,7 +501,7 @@ def __init__(self):
555501

556502
self.EXECUTABLE = _convert_pathlib_path(self.EXECUTABLE)
557503

558-
if not isinstance(self.EXECUTABLE, six.string_types):
504+
if not isinstance(self.EXECUTABLE, (str,)):
559505
raise AttributeError(
560506
'{}.EXECUTABLE must be an executable name or path'.format(
561507
type(self).__name__))

0 commit comments

Comments
 (0)