Skip to content
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

Properly render \n in exceptions with Py3 #7073

Merged
merged 7 commits into from
Jan 14, 2019
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
6 changes: 5 additions & 1 deletion src/python/pants/base/exception_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from builtins import object, str

import setproctitle
from future.utils import PY3

from pants.base.build_environment import get_buildroot
from pants.base.exiter import Exiter
Expand Down Expand Up @@ -142,6 +143,9 @@ def reset_exiter(cls, exiter):
# NB: mutate process-global state!
sys.excepthook = cls._log_unhandled_exception_and_exit

# TODO(7080): audit all usages of this function and add Py3 support, e.g. use `sys.stderr.buffer` instead
# of sys.stdout. However, we shouldn't need to proactively enforce that the stream is BytesIO, because Py3
# should already check when we try to use this.
@classmethod
def reset_interactive_output_stream(cls, interactive_output_stream):
"""
Expand Down Expand Up @@ -381,4 +385,4 @@ def handle_signal_gracefully(cls, signum, frame):
# Sets except hook.
ExceptionSink.reset_exiter(Exiter(exiter=sys.exit))
# Sets a SIGUSR2 handler.
ExceptionSink.reset_interactive_output_stream(sys.stderr)
ExceptionSink.reset_interactive_output_stream(sys.stderr.buffer if PY3 else sys.stderr)
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 5 additions & 3 deletions src/python/pants/base/exiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import sys
from builtins import object

from future.utils import PY3

from pants.util.strutil import ensure_binary


Expand Down Expand Up @@ -51,11 +53,11 @@ def exit(self, result=0, msg=None, out=None):
:param out: The file descriptor to emit `msg` to. (Optional)
"""
if msg:
out = out or sys.stderr
# sys.stderr expects bytes in Py2, unicode in Py3
out = out or (sys.stderr.buffer if PY3 else sys.stderr)
msg = ensure_binary(msg)
try:
print(msg, file=out)
out.write(msg)
out.write(b'\n')
# TODO: Determine whether this call is a no-op because the stream gets flushed on exit, or
# if we could lose what we just printed, e.g. if we get interrupted by a signal while
# exiting and the stream is buffered like stdout.
Expand Down