Skip to content

[utils] Remove Python 2 #41902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions utils/GYBUnicodeDataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,6 @@ def map_index(idx):
else:
return idx

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

# If self.bmp_data contains identical data blocks, keep the first one,
Expand Down
4 changes: 0 additions & 4 deletions utils/backtrace-check
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ def main():
found_stack_trace_start = False
found_stack_trace_entry = False
for line in lines:
# In Python 2, string objects can contain Unicode characters.
if sys.version_info.major == 2:
line = line.decode('utf-8', 'replace')

line = line.rstrip('\n')

# First see if we found the start of our stack trace start. If so, set
Expand Down
10 changes: 4 additions & 6 deletions utils/build-script
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# This source file is part of the Swift.org open source project
#
Expand Down Expand Up @@ -32,8 +32,6 @@ from build_swift.build_swift.constants import SWIFT_BUILD_ROOT
from build_swift.build_swift.constants import SWIFT_REPO_NAME
from build_swift.build_swift.constants import SWIFT_SOURCE_ROOT

import six

from swift_build_support.swift_build_support import build_script_invocation
from swift_build_support.swift_build_support import shell
from swift_build_support.swift_build_support import targets
Expand Down Expand Up @@ -120,7 +118,7 @@ class JSONDumper(json.JSONEncoder):
def default(self, o):
if hasattr(o, '__dict__'):
return vars(o)
return six.text_type(o)
return str(o)


def print_xcodebuild_versions(file=sys.stdout):
Expand Down Expand Up @@ -499,7 +497,7 @@ def main_preset():
try:
preset_parser.read_files(args.preset_file_names)
except presets.PresetError as e:
fatal_error(six.text_type(e))
fatal_error(str(e))

if args.show_presets:
for name in sorted(preset_parser.preset_names,
Expand All @@ -520,7 +518,7 @@ def main_preset():
args.preset,
vars=args.preset_substitutions)
except presets.PresetError as e:
fatal_error(six.text_type(e))
fatal_error(str(e))

preset_args = migration.migrate_swift_sdks(preset.args)

Expand Down
8 changes: 3 additions & 5 deletions utils/build_swift/build_swift/argparse/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import argparse
import copy

import six

from .types import BoolType, PathType


Expand Down Expand Up @@ -81,7 +79,7 @@ def __init__(self,
if dests == argparse.SUPPRESS:
dests = []
metavar = metavar or ''
elif isinstance(dests, six.string_types):
elif isinstance(dests, (str,)):
dests = [dests]
metavar = metavar or dests[0].upper()

Expand Down Expand Up @@ -138,7 +136,7 @@ def __init__(self, option_strings, join=None, **kwargs):
**kwargs)

def __call__(self, parser, namespace, values, option_string=None):
if isinstance(values, six.string_types):
if isinstance(values, (str,)):
values = [values]

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

arg = option_string or six.text_type(values)
arg = option_string or str(values)
parser.error('unsupported argument: {}'.format(arg))
6 changes: 2 additions & 4 deletions utils/build_swift/build_swift/argparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import argparse
from contextlib import contextmanager

import six

from . import Namespace, SUPPRESS, actions
from .actions import Action

Expand Down Expand Up @@ -133,7 +131,7 @@ def thunk(**kwargs):
*names, action=action, **kwargs)

def add_positional(self, dests, action=None, **kwargs):
if isinstance(dests, six.string_types):
if isinstance(dests, (str,)):
dests = [dests]

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

def add_option(self, option_strings, *actions, **kwargs):
if isinstance(option_strings, six.string_types):
if isinstance(option_strings, (str,)):
option_strings = [option_strings]

if not all(opt.startswith('-') for opt in option_strings):
Expand Down
4 changes: 1 addition & 3 deletions utils/build_swift/build_swift/argparse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import re
import shlex

import six

from . import ArgumentTypeError
from ..versions import Version

Expand All @@ -42,7 +40,7 @@ def _repr(cls, args):
"""

_args = []
for key, value in six.iteritems(args):
for key, value in args.items():
_args.append('{}={}'.format(key, repr(value)))

return '{}({})'.format(type(cls).__name__, ', '.join(_args))
Expand Down
5 changes: 1 addition & 4 deletions utils/build_swift/build_swift/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import itertools
import subprocess

import six
from six.moves import map

from swift_build_support.swift_build_support.targets import \
StdlibDeploymentTarget

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

if pipe.returncode != 0:
raise ValueError(six.text_type(err.splitlines()[0].decode()))
raise ValueError(str(err.splitlines()[0].decode()))
13 changes: 3 additions & 10 deletions utils/build_swift/build_swift/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

from __future__ import absolute_import, unicode_literals

import configparser
import functools
import io
from collections import OrderedDict, namedtuple

from six import StringIO
from six.moves import configparser

from . import class_utils


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

fp = StringIO(string)

# ConfigParser changes drastically from Python 2 to 3
if hasattr(self._parser, 'read_file'):
self._parser.read_file(fp)
else:
self._parser.readfp(fp)
fp = io.StringIO(string)

self._parser.read_file(fp)
self._parse_raw_presets()

# -------------------------------------------------------------------------
Expand Down
84 changes: 15 additions & 69 deletions utils/build_swift/build_swift/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,11 @@
import subprocess
import sys
from copy import copy as _copy
from pathlib import Path
from pipes import quote as _quote
from shlex import split
from subprocess import CalledProcessError

import six
from six.moves import map


try:
# Python 2
from pipes import quote as _quote
except ImportError:
from shutil import quote as _quote


try:
# Python 3.4
from pathlib import Path
except ImportError:
Path = None


__all__ = [
'CalledProcessError',
Expand Down Expand Up @@ -111,7 +96,7 @@ def _convert_pathlib_path(path):
return path

if isinstance(path, Path):
return six.text_type(path)
return str(path)

return path

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

if isinstance(args, six.string_types):
if isinstance(args, (str,)):
return shlex.split(args)

def normalize_arg(arg):
arg = _convert_pathlib_path(arg)

if isinstance(arg, six.string_types):
return [six.text_type(arg)]
if isinstance(arg, (str,)):
return [str(arg)]
if isinstance(arg, AbstractWrapper):
return list(map(_convert_pathlib_path, arg.command))

Expand All @@ -173,42 +158,14 @@ def normalize_arg(arg):
# -----------------------------------------------------------------------------
# Decorators

def _backport_devnull(func):
"""Decorator used to backport the subprocess.DEVNULL functionality from
Python 3 to Python 2.
"""

# DEVNULL was introduced in Python 3.3
if _PY_VERSION >= (3, 3):
return func

@functools.wraps(func)
def wrapper(command, **kwargs):
stdout = kwargs.get('stdout', sys.stdout)
stderr = kwargs.get('stderr', sys.stderr)

if stdout != DEVNULL and stderr != DEVNULL:
return func(command, **kwargs)

with open(os.devnull, 'w') as devnull:
if stdout == DEVNULL:
kwargs['stdout'] = devnull
if stderr == DEVNULL:
kwargs['stderr'] = devnull

return func(command, **kwargs)

return wrapper


def _normalize_command(func):
"""Decorator used to uniformly normalize the input command of the
subprocess wrappers.
"""

@functools.wraps(func)
def wrapper(command, **kwargs):
if not isinstance(command, six.string_types):
if not isinstance(command, (str,)):
command = _normalize_args(command)

return func(command, **kwargs)
Expand Down Expand Up @@ -237,10 +194,9 @@ def wrapper(command, **kwargs):
# Public Functions

def quote(command):
"""Extension of the standard pipes.quote (Python 2) or shutil.quote
(Python 3) that handles both strings and lists of strings. This mirrors
how the subprocess package can handle commands as both a standalone string
or list of strings.
"""Extension of the standard shutil.quote that handles both strings and
lists of strings. This mirrors how the subprocess package can handle
commands as both a standalone string or list of strings.

>>> quote('/Applications/App Store.app')
"'/Applications/App Store.app'"
Expand All @@ -249,7 +205,7 @@ def quote(command):
"rm -rf '~/Documents/My Homework'"
"""

if isinstance(command, six.string_types):
if isinstance(command, (str,)):
return _quote(command)

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

@_backport_devnull
@_normalize_command
@_add_echo_kwarg
def closure(command, **kwargs):
Expand All @@ -305,7 +260,6 @@ def __exit__(self, *exc):
self.wait()


@_backport_devnull
@_normalize_command
@_add_echo_kwarg
def call(command, **kwargs):
Expand All @@ -316,7 +270,6 @@ def call(command, **kwargs):
return subprocess.call(command, **kwargs)


@_backport_devnull
@_normalize_command
@_add_echo_kwarg
def check_call(command, **kwargs):
Expand All @@ -327,7 +280,6 @@ def check_call(command, **kwargs):
return subprocess.check_call(command, **kwargs)


@_backport_devnull
@_normalize_command
@_add_echo_kwarg
def check_output(command, **kwargs):
Expand All @@ -337,16 +289,11 @@ def check_output(command, **kwargs):
Output is returned as a unicode string.
"""

if six.PY3:
kwargs['encoding'] = 'utf-8'
kwargs['encoding'] = 'utf-8'

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

if six.PY3:
return output

# Return unicode string rather than bytes in Python 2.
return six.text_type(output, errors='ignore')
return output


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -484,8 +431,7 @@ def wraps(command):
return CommandWrapper(command)


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

self.EXECUTABLE = _convert_pathlib_path(self.EXECUTABLE)

if not isinstance(self.EXECUTABLE, six.string_types):
if not isinstance(self.EXECUTABLE, (str,)):
raise AttributeError(
'{}.EXECUTABLE must be an executable name or path'.format(
type(self).__name__))
Expand Down
Loading