summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/tty/color.py
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2020-05-08 23:04:07 -0700
committerTodd Gamblin <tgamblin@llnl.gov>2020-05-09 00:56:18 -0700
commit7b8e5c89999621d3e036e52998a541c3a34e8fd0 (patch)
treee7a079e30870a95aa713b5c6e899f8407b8ad423 /lib/spack/llnl/util/tty/color.py
parent5883e9df53258f7fe32cbf76f14abe9e42dfeebc (diff)
downloadspack-7b8e5c89999621d3e036e52998a541c3a34e8fd0.tar.gz
spack-7b8e5c89999621d3e036e52998a541c3a34e8fd0.tar.bz2
spack-7b8e5c89999621d3e036e52998a541c3a34e8fd0.tar.xz
spack-7b8e5c89999621d3e036e52998a541c3a34e8fd0.zip
bugfix: don't use sys.stdout as a default arg value (#16541)
After migrating to `travis-ci.com`, we saw I/O issues in our tests -- tests that relied on `capfd` and `capsys` were failing. We've also seen this in GitHub actions, and it's kept us from switching to them so far. Turns out that the issue is that using streams like `sys.stdout` as default arguments doesn't play well with `pytest` and output redirection, as `pytest` changes the values of `sys.stdout` and `sys.stderr`. if these values are evaluated before output redirection (as they are when used as default arg values), output won't be captured properly later. - [x] replace all stream default arg values with `None`, and only assign stream values inside functions. - [x] fix tests we didn't notice were relying on this erroneous behavior
Diffstat (limited to 'lib/spack/llnl/util/tty/color.py')
-rw-r--r--lib/spack/llnl/util/tty/color.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/lib/spack/llnl/util/tty/color.py b/lib/spack/llnl/util/tty/color.py
index 79f62c0040..b6f5ec782f 100644
--- a/lib/spack/llnl/util/tty/color.py
+++ b/lib/spack/llnl/util/tty/color.py
@@ -215,20 +215,22 @@ def cextra(string):
return len(''.join(re.findall(r'\033[^m]*m', string)))
-def cwrite(string, stream=sys.stdout, color=None):
+def cwrite(string, stream=None, color=None):
"""Replace all color expressions in string with ANSI control
codes and write the result to the stream. If color is
False, this will write plain text with no color. If True,
then it will always write colored output. If not supplied,
then it will be set based on stream.isatty().
"""
+ stream = sys.stdout if stream is None else stream
if color is None:
color = get_color_when()
stream.write(colorize(string, color=color))
-def cprint(string, stream=sys.stdout, color=None):
+def cprint(string, stream=None, color=None):
"""Same as cwrite, but writes a trailing newline to the stream."""
+ stream = sys.stdout if stream is None else stream
cwrite(string + "\n", stream, color)