From 3d10fd40b56264836874da19aa72351e6d5cfbf7 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 12 Sep 2021 16:27:00 -0700 Subject: [PATCH] remove print flush for better performance. Roll json.JSONDecodeError into the parse try/except block --- jc/cli.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index d38f6678..2dd4ded6 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -612,7 +612,7 @@ def main(): jc.utils.error_message('Missing piped data. Use "jc -h" for help.') sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) - # parse the data + # parse and print to stdout try: # differentiate between regular and streaming parsers @@ -624,8 +624,7 @@ def main(): pretty=pretty, env_colors=jc_colors, mono=mono, - piped_out=piped_output()), - flush=True) + piped_out=piped_output())) sys.exit(combined_exit_code(magic_exit_code, 0)) @@ -633,6 +632,13 @@ def main(): else: data = magic_stdout or sys.stdin.read() result = parser.parse(data, raw=raw, quiet=quiet) + print(json_out(result, + pretty=pretty, + env_colors=jc_colors, + mono=mono, + piped_out=piped_output())) + + sys.exit(combined_exit_code(magic_exit_code, 0)) except (ParseError, LibraryNotInstalled) as e: if debug: @@ -644,6 +650,15 @@ def main(): ' For details use the -d or -dd option. Use "jc -h" for help.') sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) + except json.JSONDecodeError: + if debug: + raise + else: + jc.utils.error_message( + 'There was an issue generating the JSON output.\n' + ' For details use the -d or -dd option.') + sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) + except Exception: if debug: raise @@ -653,20 +668,6 @@ def main(): ' For details use the -d or -dd option. Use "jc -h" for help.') sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) - # output the json - try: - print(json_out(result, pretty=pretty, env_colors=jc_colors, mono=mono, piped_out=piped_output())) - sys.exit(combined_exit_code(magic_exit_code, 0)) - - except Exception: - if debug: - raise - else: - jc.utils.error_message( - 'There was an issue generating the JSON output.\n' - ' For details use the -d or -dd option.') - sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT)) - if __name__ == '__main__': main()