summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Ferrell <51765748+Paul-Ferrell@users.noreply.github.com>2021-11-29 05:27:02 -0700
committerGitHub <noreply@github.com>2021-11-29 13:27:02 +0100
commitc0edb17b93f6716835870380cb8a83d11dffc891 (patch)
tree57a8ad8623b4e1833c9a94be6eed5247c762b32d
parentbdde70c9d3b46b612f88712ccfb1c650139579eb (diff)
downloadspack-c0edb17b93f6716835870380cb8a83d11dffc891.tar.gz
spack-c0edb17b93f6716835870380cb8a83d11dffc891.tar.bz2
spack-c0edb17b93f6716835870380cb8a83d11dffc891.tar.xz
spack-c0edb17b93f6716835870380cb8a83d11dffc891.zip
Handle byte sequences which are not encoded as UTF8 while logging. (#21447)
Fix builds which produce a lines with non-UTF8 output while logging The alternative is to read in binary mode, and then decode while ignoring errors.
-rw-r--r--lib/spack/llnl/util/tty/log.py7
-rw-r--r--lib/spack/spack/test/llnl/util/tty/log.py18
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/spack/llnl/util/tty/log.py b/lib/spack/llnl/util/tty/log.py
index a2362b31ee..81c779661f 100644
--- a/lib/spack/llnl/util/tty/log.py
+++ b/lib/spack/llnl/util/tty/log.py
@@ -780,7 +780,12 @@ def _writer_daemon(stdin_multiprocess_fd, read_multiprocess_fd, write_fd, echo,
try:
while line_count < 100:
# Handle output from the calling process.
- line = _retry(in_pipe.readline)()
+ try:
+ line = _retry(in_pipe.readline)()
+ except UnicodeDecodeError:
+ # installs like --test=root gpgme produce non-UTF8 logs
+ line = '<line lost: output was not encoded as UTF-8>\n'
+
if not line:
return
line_count += 1
diff --git a/lib/spack/spack/test/llnl/util/tty/log.py b/lib/spack/spack/test/llnl/util/tty/log.py
index 983dea9c0b..c3cb96da8a 100644
--- a/lib/spack/spack/test/llnl/util/tty/log.py
+++ b/lib/spack/spack/test/llnl/util/tty/log.py
@@ -62,6 +62,24 @@ def test_log_python_output_without_echo(capfd, tmpdir):
assert capfd.readouterr()[0] == ''
+def test_log_python_output_with_invalid_utf8(capfd, tmpdir):
+ with tmpdir.as_cwd():
+ with log_output('foo.txt'):
+ sys.stdout.buffer.write(b'\xc3\x28\n')
+
+ # python2 and 3 treat invalid UTF-8 differently
+ if sys.version_info.major == 2:
+ expected = b'\xc3(\n'
+ else:
+ expected = b'<line lost: output was not encoded as UTF-8>\n'
+ with open('foo.txt', 'rb') as f:
+ written = f.read()
+ assert written == expected
+
+ # nothing on stdout or stderr
+ assert capfd.readouterr()[0] == ''
+
+
def test_log_python_output_and_echo_output(capfd, tmpdir):
with tmpdir.as_cwd():
# echo two lines