aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authordirkf <[email protected]>2024-03-01 15:25:44 +0000
committerdirkf <[email protected]>2024-03-27 13:11:17 +0000
commit182f63e82a390e138f4a133d3ccb9c838222b02d (patch)
treef7c28f322fddbdca6b46edc3a140d6d26239afa4
parent71211e7db7243377f862dfdea9a9c3a511df66c2 (diff)
downloadyoutube-dl-182f63e82a390e138f4a133d3ccb9c838222b02d.tar.gz
youtube-dl-182f63e82a390e138f4a133d3ccb9c838222b02d.zip
[compat] Add compat_contextlib_suppress
with compat_contextlib_suppress(*Exceptions): # code that fails silently for any of Exceptions
-rw-r--r--youtube_dl/compat.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py
index 818ccebd0..637f0d82c 100644
--- a/youtube_dl/compat.py
+++ b/youtube_dl/compat.py
@@ -2943,6 +2943,24 @@ else:
compat_socket_create_connection = socket.create_connection
+try:
+ from contextlib import suppress as compat_contextlib_suppress
+except ImportError:
+ class compat_contextlib_suppress(object):
+ _exceptions = None
+
+ def __init__(self, *exceptions):
+ super(compat_contextlib_suppress, self).__init__()
+ # TODO: [Base]ExceptionGroup (3.12+)
+ self._exceptions = exceptions
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ return exc_val is not None and isinstance(exc_val, self._exceptions or tuple())
+
+
# Fix https://github.com/ytdl-org/youtube-dl/issues/4223
# See http://bugs.python.org/issue9161 for what is broken
def workaround_optparse_bug9161():
@@ -3263,6 +3281,7 @@ __all__ = [
'compat_http_cookiejar_Cookie',
'compat_http_cookies',
'compat_http_cookies_SimpleCookie',
+ 'compat_contextlib_suppress',
'compat_ctypes_WINFUNCTYPE',
'compat_etree_fromstring',
'compat_filter',