From d0a1bf49e2bcbabba74960027c5e3f75ab27853a Mon Sep 17 00:00:00 2001 From: erkin Date: Tue, 30 Mar 2021 12:31:43 +0300 Subject: T3354: Handle user break and prematurely closed stdin --- src/helpers/strip-private.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/helpers') diff --git a/src/helpers/strip-private.py b/src/helpers/strip-private.py index 023298eec..420a039eb 100755 --- a/src/helpers/strip-private.py +++ b/src/helpers/strip-private.py @@ -79,13 +79,18 @@ def strip_lines(rules: tuple) -> None: """ Read stdin line by line and apply the given stripping rules. """ - for line in sys.stdin: - if not args.keep_address: - line = strip_address(line) - for (condition, regexp, subst) in rules: - if condition: - line = regexp.sub(subst, line) - print(line, end='') + try: + for line in sys.stdin: + if not args.keep_address: + line = strip_address(line) + for (condition, regexp, subst) in rules: + if condition: + line = regexp.sub(subst, line) + print(line, end='') + # stdin can be cut for any reason, such as user interrupt or the pager terminating before the text can be read. + # All we can do is gracefully exit. + except (BrokenPipeError, EOFError, KeyboardInterrupt): + sys.exit(1) if __name__ == "__main__": args = parser.parse_args() -- cgit v1.2.3