0
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-22 02:15:12 +00:00

[utils] Fix error when copying LazyList

This commit is contained in:
pukkandan 2021-11-20 08:05:57 +05:30
parent c07a39ae8e
commit 282f570918
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698
3 changed files with 21 additions and 13 deletions

View file

@ -1657,9 +1657,9 @@ def test_LazyList(self):
self.assertEqual(repr(LazyList(it)), repr(it)) self.assertEqual(repr(LazyList(it)), repr(it))
self.assertEqual(str(LazyList(it)), str(it)) self.assertEqual(str(LazyList(it)), str(it))
self.assertEqual(list(LazyList(it).reverse()), it[::-1]) self.assertEqual(list(LazyList(it, reverse=True)), it[::-1])
self.assertEqual(list(LazyList(it).reverse()[1:3:7]), it[::-1][1:3:7]) self.assertEqual(list(reversed(LazyList(it))[::-1]), it)
self.assertEqual(list(LazyList(it).reverse()[::-1]), it) self.assertEqual(list(reversed(LazyList(it))[1:3:7]), it[::-1][1:3:7])
def test_LazyList_laziness(self): def test_LazyList_laziness(self):
@ -1672,13 +1672,13 @@ def test(ll, idx, val, cache):
test(ll, 5, 5, range(6)) test(ll, 5, 5, range(6))
test(ll, -3, 7, range(10)) test(ll, -3, 7, range(10))
ll = LazyList(range(10)).reverse() ll = LazyList(range(10), reverse=True)
test(ll, -1, 0, range(1)) test(ll, -1, 0, range(1))
test(ll, 3, 6, range(10)) test(ll, 3, 6, range(10))
ll = LazyList(itertools.count()) ll = LazyList(itertools.count())
test(ll, 10, 10, range(11)) test(ll, 10, 10, range(11))
ll.reverse() ll = reversed(ll)
test(ll, -15, 14, range(15)) test(ll, -15, 14, range(15))

View file

@ -2166,7 +2166,7 @@ def check_thumbnails(thumbnails):
t['url'] = sanitize_url(t['url']) t['url'] = sanitize_url(t['url'])
if self.params.get('check_formats') is True: if self.params.get('check_formats') is True:
info_dict['thumbnails'] = LazyList(check_thumbnails(thumbnails[::-1])).reverse() info_dict['thumbnails'] = LazyList(check_thumbnails(thumbnails[::-1]), reverse=True)
else: else:
info_dict['thumbnails'] = thumbnails info_dict['thumbnails'] = thumbnails
@ -2361,7 +2361,7 @@ def is_wellformed(f):
# TODO Central sorting goes here # TODO Central sorting goes here
if self.params.get('check_formats') is True: if self.params.get('check_formats') is True:
formats = LazyList(self._check_formats(formats[::-1])).reverse() formats = LazyList(self._check_formats(formats[::-1]), reverse=True)
if not formats or formats[0] is not info_dict: if not formats or formats[0] is not info_dict:
# only set the 'formats' fields if the original info_dict list them # only set the 'formats' fields if the original info_dict list them

View file

@ -4086,10 +4086,10 @@ class LazyList(collections.abc.Sequence):
class IndexError(IndexError): class IndexError(IndexError):
pass pass
def __init__(self, iterable): def __init__(self, iterable, *, reverse=False, _cache=None):
self.__iterable = iter(iterable) self.__iterable = iter(iterable)
self.__cache = [] self.__cache = [] if _cache is None else _cache
self.__reversed = False self.__reversed = reverse
def __iter__(self): def __iter__(self):
if self.__reversed: if self.__reversed:
@ -4155,9 +4155,17 @@ def __len__(self):
self.__exhaust() self.__exhaust()
return len(self.__cache) return len(self.__cache)
def reverse(self): def __reversed__(self):
self.__reversed = not self.__reversed return type(self)(self.__iterable, reverse=not self.__reversed, _cache=self.__cache)
return self
def __copy__(self):
return type(self)(self.__iterable, reverse=self.__reversed, _cache=self.__cache)
def __deepcopy__(self, memo):
# FIXME: This is actually just a shallow copy
id_ = id(self)
memo[id_] = self.__copy__()
return memo[id_]
def __repr__(self): def __repr__(self):
# repr and str should mimic a list. So we exhaust the iterable # repr and str should mimic a list. So we exhaust the iterable