aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r--yt_dlp/utils.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py
index fa9c509b2..65d585d05 100644
--- a/yt_dlp/utils.py
+++ b/yt_dlp/utils.py
@@ -3972,6 +3972,9 @@ class LazyList(collections.abc.Sequence):
''' Lazy immutable list from an iterable
Note that slices of a LazyList are lists and not LazyList'''
+ class IndexError(IndexError):
+ pass
+
def __init__(self, iterable):
self.__iterable = iter(iterable)
self.__cache = []
@@ -4015,22 +4018,28 @@ class LazyList(collections.abc.Sequence):
or (stop is None and step > 0)):
# We need to consume the entire iterable to be able to slice from the end
# Obviously, never use this with infinite iterables
- return self.__exhaust()[idx]
-
+ self.__exhaust()
+ try:
+ return self.__cache[idx]
+ except IndexError as e:
+ raise self.IndexError(e) from e
n = max(start or 0, stop or 0) - len(self.__cache) + 1
if n > 0:
self.__cache.extend(itertools.islice(self.__iterable, n))
- return self.__cache[idx]
+ try:
+ return self.__cache[idx]
+ except IndexError as e:
+ raise self.IndexError(e) from e
def __bool__(self):
try:
self[-1] if self.__reversed else self[0]
- except IndexError:
+ except self.IndexError:
return False
return True
def __len__(self):
- self.exhaust()
+ self.__exhaust()
return len(self.__cache)
def reverse(self):