From 981f87b00fe464fe284055b541a715a4aca1d60d Mon Sep 17 00:00:00 2001 From: h3ndr1k Date: Mon, 12 Apr 2021 12:49:09 +0200 Subject: [PATCH] Add 'wait and cooperative exit' handling for SIGINT This allows compatible shells to stop script execution when the user sends SIGINT. Only tested on Ubuntu Linux --- youtube_dl/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index e1bd67919..3a52c3e11 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -478,7 +478,13 @@ def main(argv=None): except SameFileError: sys.exit('ERROR: fixed output name but more than one file to download') except KeyboardInterrupt: - sys.exit('\nERROR: Interrupted by user') + print('\nERROR: Interrupted by user') + # 'wait and cooperative exit' handling of SIGINT + # this signals our parent that we exited because of SIGINT + # the compatible parent shell can then stop script execution + import signal + signal.signal(signal.SIGINT, signal.SIG_DFL) + os.kill(os.getpid(), signal.SIGINT) __all__ = ['main', 'YoutubeDL', 'gen_extractors', 'list_extractors']