aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/compat.py
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2022-08-19 15:34:33 +0100
committerdirkf <fieldhouse@gmx.net>2022-08-19 15:34:33 +0100
commit46b8ae2f520c17aaa756082676788c6287b6809e (patch)
treee9fa81fd136e44d03a9caf541e2e1143804404a6 /youtube_dl/compat.py
parent538ec65ba7634bb9ad9f8eb4ce72713c673969dc (diff)
downloadyoutube-dl-46b8ae2f520c17aaa756082676788c6287b6809e.tar.xz
[jsinterp] Clean up and pull yt-dlp style
* add compat_re_Pattern * improve compat_collections_chain_map * use class JS_Undefined * remove unused code
Diffstat (limited to 'youtube_dl/compat.py')
-rw-r--r--youtube_dl/compat.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py
index 6d2c31a61..3002109ca 100644
--- a/youtube_dl/compat.py
+++ b/youtube_dl/compat.py
@@ -3023,18 +3023,34 @@ except ImportError:
self.maps[0].__setitem__(k, v)
return
- def __delitem__(self, k):
+ def __contains__(self, k):
+ return any((k in m) for m in self.maps)
+
+ def __delitem(self, k):
if k in self.maps[0]:
del self.maps[0][k]
return
raise KeyError(k)
+ def __delitem__(self, k):
+ self.__delitem(k)
+
def __iter__(self):
return itertools.chain(*reversed(self.maps))
def __len__(self):
return len(iter(self))
+ # to match Py3, don't del directly
+ def pop(self, k, *args):
+ if self.__contains__(k):
+ off = self.__getitem__(k)
+ self.__delitem(k)
+ return off
+ elif len(args) > 0:
+ return args[0]
+ raise KeyError(k)
+
def new_child(self, m=None, **kwargs):
m = m or {}
m.update(kwargs)
@@ -3044,6 +3060,8 @@ except ImportError:
def parents(self):
return compat_collections_chain_map(*(self.maps[1:]))
+# Pythons disagree on the type of a pattern (RegexObject, _sre.SRE_Pattern, Pattern, ...?)
+compat_re_Pattern = type(re.compile(''))
if sys.version_info < (3, 3):
def compat_b64decode(s, *args, **kwargs):
@@ -3110,6 +3128,7 @@ __all__ = [
'compat_os_name',
'compat_parse_qs',
'compat_print',
+ 'compat_re_Pattern',
'compat_realpath',
'compat_setenv',
'compat_shlex_quote',