Bug 1967109 - Remove usages of six in python directory. r=firefox-build-system-reviewers,glandium

Also removes six from deps of mozterm's setup.py as only usages of six in it were removed last month in D245270.

Differential Revision: https://phabricator.services.mozilla.com/D249889
This commit is contained in:
Anutrix
2025-05-20 20:41:29 +00:00
committed by ahochheiden@mozilla.com
parent 0888571e1e
commit b9c9bb727f
16 changed files with 51 additions and 118 deletions

View File

@@ -25,11 +25,10 @@ import binascii
import hashlib
import logging
import os
import urllib.parse as urlparse
import dlmanager
import mozpack.path as mozpath
import six
import six.moves.urllib.parse as urlparse
from mozbuild.dirutils import mkdir
@@ -179,7 +178,7 @@ class ArtifactCache:
# extract the build ID from the downloaded artifact and use it to make a
# human readable unique name, but extracting build IDs is time consuming
# (especially on Mac OS X, where we must mount a large DMG file).
hash = hashlib.sha256(six.ensure_binary(url)).hexdigest()[:16]
hash = hashlib.sha256(url.encode()).hexdigest()[:16]
# Strip query string and fragments.
basename = os.path.basename(urlparse.urlparse(url).path)
fname = hash + "-" + basename

View File

@@ -12,7 +12,6 @@ import sys
from pathlib import Path
import mozpack.path as mozpath
import six
from mach.mixin.process import ProcessExecutionMixin
from mozboot.mozconfig import MozconfigFindException
from mozfile import which
@@ -279,7 +278,7 @@ class MozbuildObject(ProcessExecutionMixin):
# the environment variable, which has an impact on autodetection (when
# path is MozconfigLoader.AUTODETECT), and memoization wouldn't account
# for it without the explicit (unused) argument.
out = six.StringIO()
out = io.StringIO()
env = os.environ
if path and path != MozconfigLoader.AUTODETECT:
env = dict(env)

View File

@@ -17,7 +17,6 @@ import xml.etree.ElementTree as ET
from types import SimpleNamespace
import mozpack.path as mozpath
import six
import yaml
from mach.decorators import Command, CommandArgument, SubCommand
from mach.main import Mach
@@ -1847,7 +1846,7 @@ def _parse_xml_output(path, clang_output):
list of patches, and calculates line level informations from the
character level provided changes.
"""
content = six.ensure_str(open(path).read())
content = open(path).read()
def _nb_of_lines(start, end):
return len(content[start:end].splitlines())

View File

@@ -9,7 +9,6 @@ import os
import re
import mozpack.path as mozpath
import six
from mozbuild.util import hash_file
@@ -253,7 +252,7 @@ class WarningsDatabase:
normalized = list(v2)
obj["files"][k][k2] = normalized
to_write = six.ensure_text(json.dumps(obj, indent=2))
to_write = json.dumps(obj, indent=2)
fh.write(to_write)
def deserialize(self, fh):

View File

@@ -2,7 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import codecs
import builtins
import inspect
import logging
import os
@@ -14,8 +14,6 @@ from contextlib import contextmanager
from functools import wraps
import mozpack.path as mozpath
import six
from six.moves import builtins as __builtin__
from mozbuild.configure.help import HelpFormatter
from mozbuild.configure.options import (
@@ -32,7 +30,6 @@ from mozbuild.util import (
ReadOnlyNamespace,
memoize,
memoized_property,
system_encoding,
)
# TRACE logging level, below (thus more verbose than) DEBUG
@@ -299,7 +296,7 @@ class ConfigureSandbox(dict):
# files more python3-ready.
BUILTINS = ReadOnlyDict(
{
b: getattr(__builtin__, b, None)
b: getattr(builtins, b, None)
for b in (
"AssertionError",
"False",
@@ -433,7 +430,7 @@ class ConfigureSandbox(dict):
def wrapped(*args, **kwargs):
out_args = [
(
six.ensure_text(arg, encoding=encoding or "utf-8")
arg.decode(encoding=encoding or "utf-8")
if isinstance(arg, bytes)
else arg
)
@@ -1055,45 +1052,12 @@ class ConfigureSandbox(dict):
return ReadOnlyNamespace(**wrapped_subprocess)
@memoized_property
def _wrapped_six(self):
if six.PY3:
return six
wrapped_six = {}
exec("from six import *", {}, wrapped_six)
wrapped_six_moves = {}
exec("from six.moves import *", {}, wrapped_six_moves)
wrapped_six_moves_builtins = {}
exec("from six.moves.builtins import *", {}, wrapped_six_moves_builtins)
# Special case for the open() builtin, because otherwise, using it
# fails with "IOError: file() constructor not accessible in
# restricted mode". We also make open() look more like python 3's,
# decoding to unicode strings unless the mode says otherwise.
def wrapped_open(name, mode=None, buffering=None):
args = (name,)
kwargs = {}
if buffering is not None:
kwargs["buffering"] = buffering
if mode is not None:
args += (mode,)
if "b" in mode:
return open(*args, **kwargs)
kwargs["encoding"] = system_encoding
return codecs.open(*args, **kwargs)
wrapped_six_moves_builtins["open"] = wrapped_open
wrapped_six_moves["builtins"] = ReadOnlyNamespace(**wrapped_six_moves_builtins)
wrapped_six["moves"] = ReadOnlyNamespace(**wrapped_six_moves)
return ReadOnlyNamespace(**wrapped_six)
def _get_one_import(self, _from, _import, _as, glob):
"""Perform the given import, placing the result into the dict glob."""
if not _from and _import == "__builtin__":
raise Exception("Importing __builtin__ is forbidden")
if _from == "__builtin__":
_from = "six.moves.builtins"
_from = "builtins"
# The special `__sandbox__` module gives access to the sandbox
# instance.
if not _from and _import == "__sandbox__":

View File

@@ -2,7 +2,6 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import codecs
import io
import itertools
import locale
@@ -12,7 +11,6 @@ import sys
from collections import deque
from contextlib import contextmanager
import six
from looseversion import LooseVersion
@@ -76,24 +74,8 @@ class ConfigureOutputHandler(logging.Handler):
def __init__(self, stdout=sys.stdout, stderr=sys.stderr, maxlen=20):
super(ConfigureOutputHandler, self).__init__()
# Python has this feature where it sets the encoding of pipes to
# ascii, which blatantly fails when trying to print out non-ascii.
def fix_encoding(fh):
if six.PY3:
return fh
try:
isatty = fh.isatty()
except AttributeError:
isatty = True
if not isatty:
encoding = getpreferredencoding()
if encoding:
return codecs.getwriter(encoding)(fh)
return fh
self._stdout = fix_encoding(stdout)
self._stderr = fix_encoding(stderr) if stdout != stderr else self._stdout
self._stdout = stdout
self._stderr = stderr if stdout != stderr else self._stdout
try:
fd1 = self._stdout.fileno()
fd2 = self._stderr.fileno()
@@ -126,7 +108,7 @@ class ConfigureOutputHandler(logging.Handler):
try:
if record.levelno == logging.INFO:
stream = self._stdout
msg = six.ensure_text(record.getMessage())
msg = record.getMessage()
if self._stdout_waiting == self.INTERRUPTED and self._same_output:
msg = " ... %s" % msg
self._stdout_waiting = msg.endswith("... ")
@@ -210,7 +192,8 @@ class LineIO:
self._errors = errors
def write(self, buf):
buf = six.ensure_text(buf, encoding=self._encoding or "utf-8")
if isinstance(buf, bytes):
buf = buf.decode(encoding=self._encoding or "utf-8")
lines = buf.splitlines()
if not lines:
return

View File

@@ -16,7 +16,6 @@ from collections import Counter, OrderedDict, namedtuple
from itertools import dropwhile, islice, takewhile
from textwrap import TextWrapper
import six
from mach.site import CommandSiteManager
try:
@@ -362,8 +361,8 @@ class BuildMonitor(MozbuildObject):
with open(
build_resources_profile_path, "w", encoding="utf-8", newline="\n"
) as fh:
to_write = six.ensure_text(
json.dumps(self.resources.as_profile(), separators=(",", ":"))
to_write = json.dumps(
self.resources.as_profile(), separators=(",", ":")
)
fh.write(to_write)
except Exception as e:
@@ -914,7 +913,6 @@ class CCacheStats:
self._parse_line(line)
def _parse_line(self, line):
line = six.ensure_text(line)
for stat_key, stat_description in self.STATS_KEYS:
if line.startswith(stat_description):
raw_value = self._strip_prefix(line, stat_description)
@@ -1714,17 +1712,16 @@ class BuildDriver(MozbuildObject):
def _write_mozconfig_json(self):
mozconfig_json = mozpath.join(self.topobjdir, ".mozconfig.json")
with FileAvoidWrite(mozconfig_json) as fh:
to_write = six.ensure_text(
json.dumps(
{
"topsrcdir": self.topsrcdir,
"topobjdir": self.topobjdir,
"mozconfig": self.mozconfig,
},
sort_keys=True,
indent=2,
)
to_write = json.dumps(
{
"topsrcdir": self.topsrcdir,
"topobjdir": self.topobjdir,
"mozconfig": self.mozconfig,
},
sort_keys=True,
indent=2,
)
# json.dumps in python2 inserts some trailing whitespace while
# json.dumps in python3 does not, which defeats the FileAvoidWrite
# mechanism. Strip the trailing whitespace to avoid rewriting this
@@ -1852,7 +1849,7 @@ class BuildDriver(MozbuildObject):
# We'll just use an empty substs if there is no config.
pass
clobberer = Clobberer(self.topsrcdir, self.topobjdir, substs)
clobber_output = six.StringIO()
clobber_output = io.StringIO()
res = clobberer.maybe_do_clobber(os.getcwd(), auto_clobber, clobber_output)
clobber_output.seek(0)
for line in clobber_output.readlines():

View File

@@ -21,7 +21,6 @@ from collections import Counter, OrderedDict
from types import FunctionType
import mozpack.path as mozpath
import six
from mozbuild.util import (
HierarchicalStringList,
@@ -827,7 +826,7 @@ class PathMeta(type):
return super(PathMeta, cls).__call__(context, value)
class Path(six.with_metaclass(PathMeta, ContextDerivedValue, str)):
class Path(ContextDerivedValue, str, metaclass=PathMeta):
"""Stores and resolves a source path relative to a given context
This class is used as a backing type for some of the sandbox variables.

View File

@@ -10,7 +10,6 @@ from collections.abc import Iterable
import gyp
import gyp.msvs_emulation
import mozpack.path as mozpath
import six
from mozpack.files import FileFinder
from mozbuild import shellutil
@@ -227,7 +226,7 @@ def process_gyp_result(
elif spec["type"] in ("static_library", "shared_library", "executable"):
# Remove leading 'lib' from the target_name if any, and use as
# library name.
name = six.ensure_text(spec["target_name"])
name = spec["target_name"]
if spec["type"] in ("static_library", "shared_library"):
if name.startswith("lib"):
name = name[3:]

View File

@@ -21,7 +21,6 @@ import os
import sys
import weakref
import six
from mozpack.files import FileFinder
from mozbuild.util import ReadOnlyDict
@@ -154,7 +153,7 @@ class Sandbox(dict):
assert os.path.isabs(path)
try:
source = six.ensure_text(self._finder.get(path).read())
source = self._finder.get(path).read().decode()
except Exception:
raise SandboxLoadError(
self._context.source_stack, sys.exc_info()[2], read_error=path

View File

@@ -1687,6 +1687,8 @@ def _run_android(
no_attach=False,
use_existing_process=False,
):
from shlex import quote as shlex_quote
from mozrunner.devices.android_device import (
InstallIntent,
UninstallIntent,
@@ -1694,7 +1696,6 @@ def _run_android(
metadata_for_app,
verify_android_device,
)
from six.moves import shlex_quote
metadata = metadata_for_app(app)

View File

@@ -9,7 +9,6 @@ import subprocess
from mozboot.util import get_tools_dir
from mozfile import which
from packaging.version import Version
from six import PY3
NODE_MIN_VERSION = Version("12.22.12")
NPM_MIN_VERSION = Version("6.14.16")
@@ -54,7 +53,7 @@ def check_executable_version(exe, wrap_call_with_node=False):
if binary:
out = (
subprocess.check_output(
[binary, exe, "--version"], universal_newlines=PY3
[binary, exe, "--version"], universal_newlines=True
)
.lstrip("v")
.rstrip()
@@ -64,7 +63,7 @@ def check_executable_version(exe, wrap_call_with_node=False):
# direct.
if not out:
out = (
subprocess.check_output([exe, "--version"], universal_newlines=PY3)
subprocess.check_output([exe, "--version"], universal_newlines=True)
.lstrip("v")
.rstrip()
)

View File

@@ -2,9 +2,9 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import configparser
from mozpack.files import FileFinder
from six import string_types
from six.moves import configparser
def get_application_ini_value(
@@ -36,7 +36,7 @@ def get_application_ini_values(finder_or_application_directory, *args):
Raises an `Exception` if any string is not found."""
if isinstance(finder_or_application_directory, string_types):
if isinstance(finder_or_application_directory, str):
finder = FileFinder(finder_or_application_directory)
else:
finder = finder_or_application_directory

View File

@@ -22,6 +22,7 @@ import time
import urllib
from collections import defaultdict
from pathlib import Path
from shlex import quote as shlex_quote
import mozpack.path as mozpath
from mach.util import get_state_dir
@@ -31,7 +32,6 @@ from mozpack.files import FileFinder, JarFinder
from mozpack.manifests import InstallManifest
from mozpack.mozjar import JarReader
from mozpack.packager.unpack import UnpackFinder
from six.moves import shlex_quote
from mozbuild.configure import confvars
from mozbuild.dirutils import ensureParentDir

View File

@@ -19,8 +19,6 @@ import sys
from io import BytesIO, StringIO
from pathlib import Path
import six
from mozbuild.dirutils import ensureParentDir
try:
@@ -201,7 +199,9 @@ class FileAvoidWrite(BytesIO):
self._binary_mode = "b" in readmode
def write(self, buf):
BytesIO.write(self, six.ensure_binary(buf))
if isinstance(buf, str):
buf = buf.encode()
BytesIO.write(self, buf)
def avoid_writing_to_file(self):
self._write_to_file = False
@@ -218,8 +218,12 @@ class FileAvoidWrite(BytesIO):
of the result.
"""
# Use binary data if the caller explicitly asked for it.
ensure = six.ensure_binary if self._binary_mode else six.ensure_text
buf = ensure(self.getvalue())
buf = self.getvalue()
if self._binary_mode:
if isinstance(buf, str):
buf = buf.encode()
elif isinstance(buf, bytes):
buf = buf.decode()
BytesIO.close(self)
existed = False
@@ -247,9 +251,6 @@ class FileAvoidWrite(BytesIO):
writemode = "w"
if self._binary_mode:
writemode += "b"
buf = six.ensure_binary(buf)
else:
buf = six.ensure_text(buf)
path = Path(self.name)
if path.is_symlink():
# Migration to code autogeneration can encounter with existing symlinks, e.g. bug 1953858.
@@ -402,11 +403,7 @@ class List(list):
)
if key.step:
raise ValueError("List cannot be sliced with a nonzero step " "value")
# Python 2 and Python 3 do this differently for some reason.
if six.PY2:
return super(List, self).__setslice__(key.start, key.stop, val)
else:
return super(List, self).__setitem__(key, val)
return super(List, self).__setitem__(key, val)
return super(List, self).__setitem__(key, val)
def __setslice__(self, i, j, sequence):
@@ -1071,13 +1068,13 @@ def group_unified_files(files, unified_prefix, unified_suffix, files_per_unified
dummy_fill_value = ("dummy",)
def filter_out_dummy(iterable):
return six.moves.filter(lambda x: x != dummy_fill_value, iterable)
return filter(lambda x: x != dummy_fill_value, iterable)
# From the itertools documentation, slightly modified:
def grouper(n, iterable):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return six.moves.zip_longest(fillvalue=dummy_fill_value, *args)
return itertools.zip_longest(fillvalue=dummy_fill_value, *args)
for i, unified_group in enumerate(grouper(files_per_unified_file, files)):
just_the_filenames = list(filter_out_dummy(unified_group))
@@ -1093,7 +1090,7 @@ def pair(iterable):
[(1,2), (3,4), (5,6)]
"""
i = iter(iterable)
return six.moves.zip_longest(i, i)
return itertools.zip_longest(i, i)
def pairwise(iterable):
@@ -1239,7 +1236,6 @@ def hexdump(buf):
"""
Returns a list of hexdump-like lines corresponding to the given input buffer.
"""
assert six.PY3
off_format = f"%0{len(str(len(buf)))}x "
lines = []
for off in range(0, len(buf), 16):

View File

@@ -5,7 +5,7 @@
from setuptools import setup
VERSION = "1.0.0"
DEPS = ["six >= 1.13.0"]
DEPS = []
setup(
name="mozterm",