Skip to content

Commit 4332480

Browse files
authored
Print printchplenv warnings at the end of the output (#26272)
Changes how `printchplenv` prints warnings so that the warnings are outputed last. Prior to this PR, `printchplenv` warnings could get lost if the terminal scrolled past. This PR puts the warnings last so that users can be more directly notified. The exception is in the error case. If there was an error and printchplenv is going to exit, all to the warnings are printed before the errors, so that users can see warnings that may have lead to the error being produced. Note: if a user throws `--ignore-errors`, no warning buffering is done, otherwise `printchplenv` may crash without dumping the buffer out. Testing: - `CHPL_LLVM_VERSION=2 printchplenv --all` shows the warning at the bottom - `CHPL_DEVELOPER=1 CHPL_TARGET_COMPILER=gnu CHPL_TARGET_CC=clang CHPL_LLVM_VERSION=2 printchplenv --all` shows the warning and then the exception - `CHPL_DEVELOPER=0 CHPL_TARGET_COMPILER=gnu CHPL_TARGET_CC=clang CHPL_LLVM_VERSION=2 printchplenv --all` shows the warning and then prints the error - `CHPL_TARGET_COMPILER=gnu CHPL_TARGET_CC=clang CHPL_LLVM_VERSION=2 printchplenv --all --ignore-errors` does no buffering [Reviewed by @vasslitvinov]
2 parents 6aa7405 + f3d3ced commit 4332480

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

util/chplenv/printchplenv.py

+2
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ def main():
633633
ret = printchplenv(contents, filters, options.format, only=only)
634634
stdout.write(ret)
635635

636+
utils.flush_warnings()
637+
636638

637639
if __name__ == '__main__':
638640
main()

util/chplenv/utils.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,51 @@
1313
# Backport for pre Python 3.2
1414
from distutils.spawn import find_executable as which
1515

16+
# should not need this outside of this class
17+
_warning_buffer = []
18+
_buffer_warnings = True
19+
def flush_warnings():
20+
for warning in _warning_buffer:
21+
sys.stderr.write('Warning: ')
22+
sys.stderr.write(warning)
23+
sys.stderr.write('\n')
24+
_warning_buffer.clear()
25+
def set_buffer_warnings(buffer_warnings):
26+
global _buffer_warnings
27+
_buffer_warnings = buffer_warnings
28+
29+
def should_buffer_warnings():
30+
return _buffer_warnings and not ignore_errors
1631

1732
def warning(msg):
1833
if not os.environ.get('CHPLENV_SUPPRESS_WARNINGS'):
19-
sys.stderr.write('Warning: ')
20-
sys.stderr.write(msg)
21-
sys.stderr.write('\n')
34+
if should_buffer_warnings():
35+
_warning_buffer.append(msg)
36+
else:
37+
sys.stderr.write('Warning: ')
38+
sys.stderr.write(msg)
39+
sys.stderr.write('\n')
2240

2341
ignore_errors = False
2442

2543
def error(msg, exception=Exception):
2644
"""Exception raising wrapper that differentiates developer-mode output"""
2745
developer = os.environ.get('CHPL_DEVELOPER')
2846
if developer and developer != "0" and not ignore_errors:
47+
# before rasing the exception, flush the warnings
48+
flush_warnings()
2949
raise exception(msg)
3050
else:
31-
sys.stderr.write('\nError: ')
32-
sys.stderr.write(msg)
33-
sys.stderr.write('\n')
34-
if not ignore_errors:
51+
out = '\nError: {}\n'.format(msg)
52+
if ignore_errors:
53+
sys.stderr.write(out)
54+
else:
55+
# flush warnings, print error, and exit
56+
# technically, there is no need to flush warnings here, but might as well
57+
flush_warnings()
58+
sys.stderr.write(out)
3559
sys.exit(1)
3660

37-
3861
def memoize(func):
3962
"""Function memoizing decorator"""
4063
cache = func.cache = {}

0 commit comments

Comments
 (0)