diff options
author | Georg Brandl <georg@python.org> | 2014-11-10 20:07:57 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2014-11-10 20:07:57 +0100 |
commit | 1b0b3bd0c52d997f707201fbaf973b9f49c5dcb8 (patch) | |
tree | 015960488bea5a16e37b269bbf6a85c7be1202ec /pygments/cmdline.py | |
parent | eb7a8bb7f62f7738d3f95033570890a54d6b6537 (diff) | |
download | pygments-1b0b3bd0c52d997f707201fbaf973b9f49c5dcb8.tar.gz |
Wrap the whole inner body of main() in the try-except.
Diffstat (limited to 'pygments/cmdline.py')
-rw-r--r-- | pygments/cmdline.py | 81 |
1 files 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 |