From 78d9d0b7251c9c572f2d178426ff5aa0661c437d Mon Sep 17 00:00:00 2001 From: kikuyan Date: Fri, 9 Jul 2021 02:34:00 +0900 Subject: [PATCH] handle broken pipe error in main() --- youtube_dl/__init__.py | 8 ++++++++ youtube_dl/compat.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index e1bd67919..31f53c9d2 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -16,6 +16,7 @@ from .options import ( parseOpts, ) from .compat import ( + compat_BrokenPipeError, compat_getpass, compat_shlex_split, workaround_optparse_bug9161, @@ -25,6 +26,7 @@ from .utils import ( decodeOption, DEFAULT_OUTTMPL, DownloadError, + error_to_compat_str, expand_path, match_filter_func, MaxDownloadsReached, @@ -479,6 +481,12 @@ def main(argv=None): sys.exit('ERROR: fixed output name but more than one file to download') except KeyboardInterrupt: sys.exit('\nERROR: Interrupted by user') + except compat_BrokenPipeError as err: + # See "Note on SIGPIPE" + # https://docs.python.org/3/library/signal.html#note-on-sigpipe + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + sys.exit('ERROR: %s' % error_to_compat_str(err)) __all__ = ['main', 'YoutubeDL', 'gen_extractors', 'list_extractors'] diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index 9e45c454b..4f8cc23e6 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -2998,7 +2998,14 @@ else: return ctypes.WINFUNCTYPE(*args, **kwargs) +try: # Python >= 3.3 + compat_BrokenPipeError = BrokenPipeError +except NameError: + compat_BrokenPipeError = IOError + + __all__ = [ + 'compat_BrokenPipeError', 'compat_HTMLParseError', 'compat_HTMLParser', 'compat_HTTPError',