aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/compat.py
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2022-08-14 18:45:45 +0100
committerGitHub <noreply@github.com>2022-08-14 18:45:45 +0100
commitd231b56717c73ee597d2e077d11b69ed48a1b02d (patch)
treed38a1b2f948b150c253ba91cc8568b6225fa29fd /youtube_dl/compat.py
parente6a836d54ca1d3cd02f3ee45ef707a46f23e8291 (diff)
downloadyoutube-dl-d231b56717c73ee597d2e077d11b69ed48a1b02d.tar.xz
[jsinterp] Overhaul JSInterp to handle new YT players 4c3f79c5, 324f67b9 (#31170)
* back-port from yt-dlp 8f53dc44a0cc1c2d98c35740b9293462c080f5d0, thanks pukkandan * also support void, improve <</>> precedence, improve expressions in comma-list * add more tests
Diffstat (limited to 'youtube_dl/compat.py')
-rw-r--r--youtube_dl/compat.py54
1 files changed, 53 insertions, 1 deletions
diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py
index 9f5f85dae..6d2c31a61 100644
--- a/youtube_dl/compat.py
+++ b/youtube_dl/compat.py
@@ -2985,7 +2985,6 @@ except ImportError:
except ImportError:
compat_filter = filter
-
try:
from future_builtins import zip as compat_zip
except ImportError: # not 2.6+ or is 3.x
@@ -2995,6 +2994,57 @@ except ImportError: # not 2.6+ or is 3.x
compat_zip = zip
+# method renamed between Py2/3
+try:
+ from itertools import zip_longest as compat_itertools_zip_longest
+except ImportError:
+ from itertools import izip_longest as compat_itertools_zip_longest
+
+
+# new class in collections
+try:
+ from collections import ChainMap as compat_collections_chain_map
+except ImportError:
+ # Py < 3.3
+ class compat_collections_chain_map(compat_collections_abc.MutableMapping):
+
+ maps = [{}]
+
+ def __init__(self, *maps):
+ self.maps = list(maps) or [{}]
+
+ def __getitem__(self, k):
+ for m in self.maps:
+ if k in m:
+ return m[k]
+ raise KeyError(k)
+
+ def __setitem__(self, k, v):
+ self.maps[0].__setitem__(k, v)
+ return
+
+ def __delitem__(self, k):
+ if k in self.maps[0]:
+ del self.maps[0][k]
+ return
+ raise KeyError(k)
+
+ def __iter__(self):
+ return itertools.chain(*reversed(self.maps))
+
+ def __len__(self):
+ return len(iter(self))
+
+ def new_child(self, m=None, **kwargs):
+ m = m or {}
+ m.update(kwargs)
+ return compat_collections_chain_map(m, *self.maps)
+
+ @property
+ def parents(self):
+ return compat_collections_chain_map(*(self.maps[1:]))
+
+
if sys.version_info < (3, 3):
def compat_b64decode(s, *args, **kwargs):
if isinstance(s, compat_str):
@@ -3031,6 +3081,7 @@ __all__ = [
'compat_basestring',
'compat_chr',
'compat_collections_abc',
+ 'compat_collections_chain_map',
'compat_cookiejar',
'compat_cookiejar_Cookie',
'compat_cookies',
@@ -3051,6 +3102,7 @@ __all__ = [
'compat_input',
'compat_integer_types',
'compat_itertools_count',
+ 'compat_itertools_zip_longest',
'compat_kwargs',
'compat_map',
'compat_numeric_types',