From 847dbe46f4d64635c23d2146d36f2642f0bd7918 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Fri, 28 Jun 2019 09:37:15 -0700 Subject: [PATCH] Remove Python 2 use of `std{out,err}` over `std{out,err}.buffer` (#7968) --- src/python/pants/base/BUILD | 1 - src/python/pants/base/exception_sink.py | 3 +-- src/python/pants/base/exiter.py | 4 +--- src/python/pants/bin/remote_pants_runner.py | 6 +++--- src/python/pants/goal/BUILD | 1 - src/python/pants/goal/context.py | 3 +-- src/python/pants/java/BUILD | 1 - src/python/pants/java/nailgun_client.py | 5 ++--- src/python/pants/net/BUILD | 1 - src/python/pants/net/http/fetcher.py | 3 +-- src/python/pants/pantsd/pants_daemon.py | 6 +++--- src/python/pants/reporting/reporting.py | 6 ++---- src/python/pants/util/BUILD | 3 --- src/python/pants/util/contextutil.py | 2 +- src/python/pants/util/process_handler.py | 4 +--- 15 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/python/pants/base/BUILD b/src/python/pants/base/BUILD index 479a7514a63..2fe59b40374 100644 --- a/src/python/pants/base/BUILD +++ b/src/python/pants/base/BUILD @@ -220,7 +220,6 @@ python_library( name='exiter', sources=['exiter.py'], dependencies=[ - '3rdparty/python:future', 'src/python/pants/util:fileutil', 'src/python/pants/util:strutil', ] diff --git a/src/python/pants/base/exception_sink.py b/src/python/pants/base/exception_sink.py index ef2e09088b9..1c864896c03 100644 --- a/src/python/pants/base/exception_sink.py +++ b/src/python/pants/base/exception_sink.py @@ -12,7 +12,6 @@ from contextlib import contextmanager import setproctitle -from future.utils import PY3 from pants.base.exiter import Exiter from pants.util.dirutil import safe_mkdir, safe_open @@ -487,7 +486,7 @@ def _handle_signal_gracefully(cls, signum, signame, traceback_lines): # Sets except hook for exceptions at import time. ExceptionSink.reset_exiter(Exiter(exiter=sys.exit)) # Sets a SIGUSR2 handler. -ExceptionSink.reset_interactive_output_stream(sys.stderr.buffer if PY3 else sys.stderr) +ExceptionSink.reset_interactive_output_stream(sys.stderr.buffer) # Sets a handler that logs nonfatal signals to the exception sink before exiting. ExceptionSink.reset_signal_handler(SignalHandler()) # Set whether to print stacktraces on exceptions or signals during import time. diff --git a/src/python/pants/base/exiter.py b/src/python/pants/base/exiter.py index bcdab24ae9e..44a67938a37 100644 --- a/src/python/pants/base/exiter.py +++ b/src/python/pants/base/exiter.py @@ -5,8 +5,6 @@ import sys import traceback -from future.utils import PY3 - from pants.util.strutil import ensure_binary @@ -53,7 +51,7 @@ def exit(self, result=PANTS_SUCCEEDED_EXIT_CODE, msg=None, out=None): """ if msg: out = out or sys.stderr - if PY3 and hasattr(out, 'buffer'): + if hasattr(out, 'buffer'): out = out.buffer msg = ensure_binary(msg) diff --git a/src/python/pants/bin/remote_pants_runner.py b/src/python/pants/bin/remote_pants_runner.py index 45eb3e03686..672236fc5fd 100644 --- a/src/python/pants/bin/remote_pants_runner.py +++ b/src/python/pants/bin/remote_pants_runner.py @@ -6,7 +6,7 @@ import time from contextlib import contextmanager -from future.utils import PY3, raise_with_traceback +from future.utils import raise_with_traceback from pants.base.exception_sink import ExceptionSink, SignalHandler from pants.console.stty_utils import STTYSettings @@ -85,8 +85,8 @@ def __init__(self, exiter, args, env, options_bootstrapper, stdin=None, stdout=N self._options_bootstrapper = options_bootstrapper self._bootstrap_options = options_bootstrapper.bootstrap_options self._stdin = stdin or sys.stdin - self._stdout = stdout or (sys.stdout.buffer if PY3 else sys.stdout) - self._stderr = stderr or (sys.stderr.buffer if PY3 else sys.stderr) + self._stdout = stdout or sys.stdout.buffer + self._stderr = stderr or sys.stderr.buffer @contextmanager def _trapped_signals(self, client): diff --git a/src/python/pants/goal/BUILD b/src/python/pants/goal/BUILD index 3d92ee40e2d..10ae07b60a5 100644 --- a/src/python/pants/goal/BUILD +++ b/src/python/pants/goal/BUILD @@ -31,7 +31,6 @@ python_library( name = 'context', sources = ['context.py'], dependencies = [ - '3rdparty/python:future', ':products', ':workspace', 'src/python/pants/base:build_environment', diff --git a/src/python/pants/goal/context.py b/src/python/pants/goal/context.py index e2e7bc9be36..3a01b86cc18 100644 --- a/src/python/pants/goal/context.py +++ b/src/python/pants/goal/context.py @@ -6,7 +6,6 @@ from collections import defaultdict from contextlib import contextmanager -from future.utils import PY3 from twitter.common.collections import OrderedSet from pants.base.build_environment import get_buildroot, get_scm @@ -62,7 +61,7 @@ def __init__(self, options, run_tracker, target_roots, self._lock = OwnerPrintingInterProcessFileLock(os.path.join(self._buildroot, '.pants.workdir.file_lock')) self._java_sysprops = None # Computed lazily. self.requested_goals = requested_goals or [] - self._console_outstream = console_outstream or (sys.stdout.buffer if PY3 else sys.stdout) + self._console_outstream = console_outstream or sys.stdout.buffer self._scm = scm or get_scm() self._workspace = workspace or (ScmWorkspace(self._scm) if self._scm else None) self._replace_targets(target_roots) diff --git a/src/python/pants/java/BUILD b/src/python/pants/java/BUILD index 30bd02dd7f0..98c3313fac3 100644 --- a/src/python/pants/java/BUILD +++ b/src/python/pants/java/BUILD @@ -17,7 +17,6 @@ python_library( name = 'nailgun_client', sources = ['nailgun_client.py'], dependencies = [ - '3rdparty/python:future', '3rdparty/python:psutil', ':nailgun_io', ':nailgun_protocol', diff --git a/src/python/pants/java/nailgun_client.py b/src/python/pants/java/nailgun_client.py index 172fa244351..8aa4ba1e6ea 100644 --- a/src/python/pants/java/nailgun_client.py +++ b/src/python/pants/java/nailgun_client.py @@ -10,7 +10,6 @@ import time import psutil -from future.utils import PY3 from pants.java.nailgun_io import NailgunStreamWriter from pants.java.nailgun_protocol import ChunkType, MaybeShutdownSocket, NailgunProtocol @@ -245,8 +244,8 @@ def __init__(self, host=None, port=None, ins=sys.stdin, out=None, err=None, self._address = (self._host, self._port) self._address_string = ':'.join(str(i) for i in self._address) self._stdin = ins - self._stdout = out or (sys.stdout.buffer if PY3 else sys.stdout) - self._stderr = err or (sys.stderr.buffer if PY3 else sys.stderr) + self._stdout = out or sys.stdout.buffer + self._stderr = err or sys.stderr.buffer self._exit_on_broken_pipe = exit_on_broken_pipe self._metadata_base_dir = metadata_base_dir # Mutable session state. diff --git a/src/python/pants/net/BUILD b/src/python/pants/net/BUILD index 3a24839b97d..0550d75e4c6 100644 --- a/src/python/pants/net/BUILD +++ b/src/python/pants/net/BUILD @@ -4,7 +4,6 @@ python_library( sources = rglobs('*.py'), dependencies = [ - '3rdparty/python:future', '3rdparty/python:requests', '3rdparty/python:pyopenssl', '3rdparty/python:six', diff --git a/src/python/pants/net/http/fetcher.py b/src/python/pants/net/http/fetcher.py index 88dfc0bc4c0..4432fca4f1c 100644 --- a/src/python/pants/net/http/fetcher.py +++ b/src/python/pants/net/http/fetcher.py @@ -12,7 +12,6 @@ import requests import six -from future.utils import PY3 from pants.util.dirutil import safe_open from pants.util.strutil import strip_prefix @@ -156,7 +155,7 @@ def __init__(self, width=None, chunk_size_bytes=None, stream=None): if not isinstance(self._width, six.integer_types): raise ValueError('The width must be an integer, given {}'.format(self._width)) self._chunk_size_bytes = chunk_size_bytes or 10 * 1024 - self._stream = stream or (sys.stderr.buffer if PY3 else sys.stderr) + self._stream = stream or sys.stderr.buffer self._start = time.time() def status(self, code, content_length=None): diff --git a/src/python/pants/pantsd/pants_daemon.py b/src/python/pants/pantsd/pants_daemon.py index cb1167e2b52..58a93516f3c 100644 --- a/src/python/pants/pantsd/pants_daemon.py +++ b/src/python/pants/pantsd/pants_daemon.py @@ -38,9 +38,9 @@ class _LoggerStream(object): """A sys.std{out,err} replacement that pipes output to a logger. - N.B. `logging.Logger` expects unicode. However, most of our outstream logic, such as in `Exiter.py`, - will use `sys.std{out,err}.buffer` and thus a bytes interface when running with Python 3. So, we must provide - a `buffer` property, and change the semantics of the buffer to always convert the message to unicode. This + N.B. `logging.Logger` expects unicode. However, most of our outstream logic, such as in `exiter.py`, + will use `sys.std{out,err}.buffer` and thus a bytes interface. So, we must provide a `buffer` + property, and change the semantics of the buffer to always convert the message to unicode. This is an unfortunate code smell, as `logging` does not expose a bytes interface so this is the best solution we could think of. """ diff --git a/src/python/pants/reporting/reporting.py b/src/python/pants/reporting/reporting.py index 5213490a77a..ecfda136a2b 100644 --- a/src/python/pants/reporting/reporting.py +++ b/src/python/pants/reporting/reporting.py @@ -5,8 +5,6 @@ import sys from io import BytesIO -from future.utils import PY3 - from pants.base.workunit import WorkUnitLabel from pants.reporting.html_reporter import HtmlReporter from pants.reporting.invalidation_report import InvalidationReport @@ -173,8 +171,8 @@ def update_reporting(self, global_options, is_quiet, run_tracker): timing=timing, cache_stats=cache_stats)) else: # Set up the new console reporter. - stdout = sys.stdout.buffer if PY3 else sys.stdout - stderr = sys.stderr.buffer if PY3 else sys.stderr + stdout = sys.stdout.buffer + stderr = sys.stderr.buffer settings = PlainTextReporter.Settings(log_level=log_level, outfile=stdout, errfile=stderr, color=color, indent=True, timing=timing, cache_stats=cache_stats, label_format=self.get_options().console_label_format, diff --git a/src/python/pants/util/BUILD b/src/python/pants/util/BUILD index d2a2257412d..44bc5afdd98 100644 --- a/src/python/pants/util/BUILD +++ b/src/python/pants/util/BUILD @@ -110,9 +110,6 @@ python_library( python_library( name = 'process_handler', sources = ['process_handler.py'], - dependencies= [ - '3rdparty/python:future', - ] ) python_library( diff --git a/src/python/pants/util/contextutil.py b/src/python/pants/util/contextutil.py index 3ab1c3b8b3d..ad613119c4c 100644 --- a/src/python/pants/util/contextutil.py +++ b/src/python/pants/util/contextutil.py @@ -141,7 +141,7 @@ def stdio_as(stdout_fd, stderr_fd, stdin_fd): impossible for this method to locate all python objects which refer to those fds, so it's up to the caller to guarantee that `0, 1, 2` are safe to replace. - In Python3, the streams expect unicode. To write and read bytes, access their buffer, e.g. `stdin.buffer.read()`. + The streams expect unicode. To write and read bytes, access their buffer, e.g. `stdin.buffer.read()`. """ with _stdio_stream_as(stdin_fd, 0, 'stdin', 'r'),\ _stdio_stream_as(stdout_fd, 1, 'stdout', 'w'),\ diff --git a/src/python/pants/util/process_handler.py b/src/python/pants/util/process_handler.py index 129764d08fe..cd507287241 100644 --- a/src/python/pants/util/process_handler.py +++ b/src/python/pants/util/process_handler.py @@ -6,8 +6,6 @@ import sys from abc import ABC, abstractmethod -from future.utils import PY3 - class ProcessHandler(ABC): """An abstraction of process handling calls using the same interface as subprocess(32).Popen. @@ -91,6 +89,6 @@ def _tee(infile, outfile, return_function): accumulator = io.BytesIO() for line in iter(infile.readline, b""): accumulator.write(line) - outfile.buffer.write(line) if PY3 else outfile.write(line) + outfile.buffer.write(line) infile.close() return_function(accumulator.getvalue())