Skip to content

Commit

Permalink
[compat] Add compat_contextlib_suppress
Browse files Browse the repository at this point in the history
with compat_contextlib_suppress(*Exceptions):
    # code that fails silently for any of Exceptions
  • Loading branch information
dirkf committed Mar 27, 2024
1 parent 71211e7 commit 182f63e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,24 @@ def compat_socket_create_connection(address, timeout, source_address=None):
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():
Expand Down Expand Up @@ -3263,6 +3281,7 @@ def compat_datetime_timedelta_total_seconds(td):
'compat_http_cookiejar_Cookie',
'compat_http_cookies',
'compat_http_cookies_SimpleCookie',
'compat_contextlib_suppress',
'compat_ctypes_WINFUNCTYPE',
'compat_etree_fromstring',
'compat_filter',
Expand Down

0 comments on commit 182f63e

Please sign in to comment.