From a5d5be3462c527a45903c8a697d044550a45113a Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 28 Dec 2022 12:18:10 -0800 Subject: [PATCH] Fix incorrect SIGPIPE handling --- inquisitor/cli.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/inquisitor/cli.py b/inquisitor/cli.py index 2487e70..dbf53e5 100644 --- a/inquisitor/cli.py +++ b/inquisitor/cli.py @@ -236,12 +236,13 @@ def command_help(args): return 0 +def nocommand(args): + print("command required") + return 0 + + def main(): """CLI entry point""" - # Enable piping - from signal import signal, SIGPIPE, SIG_DFL - signal(SIGPIPE, SIG_DFL) - # Collect the commands from this module import inquisitor.cli commands = { @@ -283,8 +284,11 @@ def main(): add_logging_handler(verbose=args.verbose, log_filename=None) # Execute command - if args.command: - sys.exit(commands[args.command](args.args)) - else: - print("command required") - sys.exit(0) + try: + command = commands.get(args.command, nocommand) + sys.exit(command(args.args)) + except BrokenPipeError: + # See https://docs.python.org/3.10/library/signal.html#note-on-sigpipe + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + sys.exit(1)