logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git
commit: a4fc1151f125a2dfb89b2d1505838bcbb71aacc9
parent a464c159e61ab5c2c3babc8a4eb1d3e7923f1fe0
Author: dirkf <fieldhouse@gmx.net>
Date:   Tue, 25 Mar 2025 21:56:32 +0000

[JSInterp] Improve indexing
* catch invalid list index with `ValueError` (eg [1, 2]['ab'] -> undefined)
* allow assignment outside existing list (eg var l = [1,2]; l[9] = 0;)

Diffstat:

Myoutube_dl/jsinterp.py6+++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py @@ -678,7 +678,7 @@ class JSInterpreter(object): return len(obj) try: return obj[int(idx)] if isinstance(obj, list) else obj[compat_str(idx)] - except (TypeError, KeyError, IndexError) as e: + except (TypeError, KeyError, IndexError, ValueError) as e: # allow_undefined is None gives correct behaviour if allow_undefined or ( allow_undefined is None and not isinstance(e, TypeError)): @@ -1038,6 +1038,10 @@ class JSInterpreter(object): left_val = self._index(left_val, idx) if isinstance(idx, float): idx = int(idx) + if isinstance(left_val, list) and len(left_val) <= int_or_none(idx, default=-1): + # JS Array is a sparsely assignable list + # TODO: handle extreme sparsity without memory bloat, eg using auxiliary dict + left_val.extend((idx - len(left_val) + 1) * [JS_Undefined]) left_val[idx] = self._operator( m.group('op'), self._index(left_val, idx) if m.group('op') else None, m.group('expr'), expr, local_vars, allow_recursion)