From: Michael Orlitzky Date: Tue, 20 Sep 2022 23:48:10 +0000 (-0400) Subject: djbdns/io.py: handle BrokenPipeError in parse_logfile(). X-Git-Tag: 0.0.1~16 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=djbdns-logparse.git;a=commitdiff_plain;h=4645a2edd7eeb0470d5796f4f15f02d23429288e djbdns/io.py: handle BrokenPipeError in parse_logfile(). This avoids a (harmless) crash when the output of djbdns-logparse is piped to a process (such as head/tail) that closes the pipe before djbdns-logparse is done writing to it. --- diff --git a/djbdns/io.py b/djbdns/io.py index 7c70bab..1d68fac 100644 --- a/djbdns/io.py +++ b/djbdns/io.py @@ -43,7 +43,13 @@ def parse_logfile(file : TextIO): if not friendly_line: friendly_line = line - print(friendly_line) + try: + print(friendly_line) + except BrokenPipeError: + # If our stdout is being piped to another process and if + # that process closes the pipe, this error will be raised + # the next time we try to write to stdout. + break # Ensure that the pipe gets closed. tai.communicate()