Skip to content

Commit

Permalink
Remove Python 2 use of std{out,err} over std{out,err}.buffer (#7968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Jun 28, 2019
1 parent 159e3b1 commit 847dbe4
Show file tree
Hide file tree
Showing 15 changed files with 16 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/python/pants/base/BUILD
Expand Up @@ -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',
]
Expand Down
3 changes: 1 addition & 2 deletions src/python/pants/base/exception_sink.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/base/exiter.py
Expand Up @@ -5,8 +5,6 @@
import sys
import traceback

from future.utils import PY3

from pants.util.strutil import ensure_binary


Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/bin/remote_pants_runner.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/goal/BUILD
Expand Up @@ -31,7 +31,6 @@ python_library(
name = 'context',
sources = ['context.py'],
dependencies = [
'3rdparty/python:future',
':products',
':workspace',
'src/python/pants/base:build_environment',
Expand Down
3 changes: 1 addition & 2 deletions src/python/pants/goal/context.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/java/BUILD
Expand Up @@ -17,7 +17,6 @@ python_library(
name = 'nailgun_client',
sources = ['nailgun_client.py'],
dependencies = [
'3rdparty/python:future',
'3rdparty/python:psutil',
':nailgun_io',
':nailgun_protocol',
Expand Down
5 changes: 2 additions & 3 deletions src/python/pants/java/nailgun_client.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/net/BUILD
Expand Up @@ -4,7 +4,6 @@
python_library(
sources = rglobs('*.py'),
dependencies = [
'3rdparty/python:future',
'3rdparty/python:requests',
'3rdparty/python:pyopenssl',
'3rdparty/python:six',
Expand Down
3 changes: 1 addition & 2 deletions src/python/pants/net/http/fetcher.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/pantsd/pants_daemon.py
Expand Up @@ -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.
"""
Expand Down
6 changes: 2 additions & 4 deletions src/python/pants/reporting/reporting.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions src/python/pants/util/BUILD
Expand Up @@ -110,9 +110,6 @@ python_library(
python_library(
name = 'process_handler',
sources = ['process_handler.py'],
dependencies= [
'3rdparty/python:future',
]
)

python_library(
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/util/contextutil.py
Expand Up @@ -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'),\
Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/util/process_handler.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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())

0 comments on commit 847dbe4

Please sign in to comment.