From 1b0b3bd0c52d997f707201fbaf973b9f49c5dcb8 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Mon, 10 Nov 2014 20:07:57 +0100 Subject: Wrap the whole inner body of main() in the try-except. --- pygments/cmdline.py | 81 +++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/pygments/cmdline.py b/pygments/cmdline.py index c3f00fca..20e87c36 100644 --- a/pygments/cmdline.py +++ b/pygments/cmdline.py @@ -198,19 +198,7 @@ def _print_list(what): print(" %s" % docstring_headline(cls)) -def main(args=sys.argv): - """ - Main command line entry point. - """ - # pylint: disable-msg=R0911,R0912,R0915 - - usage = USAGE % ((args[0],) * 6) - - try: - popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHgs") - except getopt.GetoptError: - print(usage, file=sys.stderr) - return 2 +def main_inner(popts, args, usage): opts = {} O_opts = [] P_opts = [] @@ -473,33 +461,48 @@ def main(args=sys.argv): return 1 # ... and do it! + if '-s' not in opts: + # process whole input as per normal... + highlight(code, lexer, fmter, outfile) + return 0 + else: + if not lexer: + print('Error: when using -s a lexer has to be selected with -l', + file=sys.stderr) + return 1 + # line by line processing of stdin (eg: for 'tail -f')... + try: + while 1: + if sys.version_info > (3,): + # Python 3: we have to use .buffer to get a binary stream + line = sys.stdin.buffer.readline() + else: + line = sys.stdin.readline() + if not line: + break + if not inencoding: + line = guess_decode_from_terminal(line, sys.stdin)[0] + highlight(line, lexer, fmter, outfile) + if hasattr(outfile, 'flush'): + outfile.flush() + except KeyboardInterrupt: + return 0 + + +def main(args=sys.argv): + """ + Main command line entry point. + """ + usage = USAGE % ((args[0],) * 6) + try: - if '-s' not in opts: - # process whole input as per normal... - highlight(code, lexer, fmter, outfile) - else: - if not lexer: - print('Error: when using -s a lexer has to be selected with -l', - file=sys.stderr) - return 1 - # line by line processing of stdin (eg: for 'tail -f')... - try: - while 1: - if sys.version_info > (3,): - # Python 3: we have to use .buffer to get a binary stream - line = sys.stdin.buffer.readline() - else: - line = sys.stdin.readline() - if not line: - break - if not inencoding: - line = guess_decode_from_terminal(line, sys.stdin)[0] - highlight(line, lexer, fmter, outfile) - if hasattr(outfile, 'flush'): - outfile.flush() - except KeyboardInterrupt: - return 0 + popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHgs") + except getopt.GetoptError: + print(usage, file=sys.stderr) + return 2 + try: + return main_inner(popts, args, usage) except Exception: import traceback info = traceback.format_exception(*sys.exc_info()) @@ -511,5 +514,3 @@ def main(args=sys.argv): print('*** Error while highlighting:', file=sys.stderr) print(msg, file=sys.stderr) return 1 - - return 0 -- cgit v1.2.1