From 81856c0d15d14b823431db983152d4488779af08 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Thu, 17 Nov 2016 04:44:08 -0800 Subject: [PATCH] [python] fix another bug in pygments.highlight() Summary: Turns out there was also a problem with how this function deals with final newlines... Reviewed By: jeremydubreil Differential Revision: D4191154 fbshipit-source-id: fc10517 --- infer/lib/python/inferlib/colorize.py | 13 ++++++++++++- infer/lib/python/inferlib/source.py | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) 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