diff --git a/infer/lib/python/inferlib/colorize.py b/infer/lib/python/inferlib/colorize.py index 0c67ef5e3..0d33f5088 100644 --- a/infer/lib/python/inferlib/colorize.py +++ b/infer/lib/python/inferlib/colorize.py @@ -73,7 +73,18 @@ def syntax_highlighting(source_name, mode, s): while (i < len(s) and s[i] == '\n'): initial_newlines += '\n' i += 1 - return initial_newlines + pygments.highlight(s, lexer, formatter) + # pygments.highlight() also insists that all string end with exactly one + # newline character regardless of the input string! + final_newlines = '' + i = 1 + while (i <= len(s) and s[-i] == '\n'): + final_newlines += '\n' + i += 1 + colorized_string = pygments.highlight(s, lexer, formatter) + # strip the result from pygments.highlight() to get rid of the + # potentially spurious final newline, and also to continue to + # work in case the bugs in pygments.highlight() gets fixed. + return initial_newlines + colorized_string.strip('\n') + final_newlines def color(s, color, mode): diff --git a/infer/lib/python/inferlib/source.py b/infer/lib/python/inferlib/source.py index 2466855e3..eb33c54f3 100644 --- a/infer/lib/python/inferlib/source.py +++ b/infer/lib/python/inferlib/source.py @@ -72,7 +72,7 @@ def build_source_context(source_name, mode, report_line): # avoid going past the end of the file for line in source_file: last_line = line_number - if start_line <= line_number <= end_line: + if start_line <= line_number < end_line: excerpt += line elif line_number > end_line: # OPTIM: no need to read past the last line of the excerpt