From 1dc27e1c3bda9cb8f44b805c89918aa7d11ffcdc Mon Sep 17 00:00:00 2001 From: dirkf Date: Fri, 7 Mar 2025 19:40:53 +0000 Subject: [PATCH] [JSInterp] Make indexing error handling more conformant * by default TypeError -> undefined, else raise * set allow_undefined=True/False to override --- youtube_dl/jsinterp.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py index 9b4157a43..5a45fbb03 100644 --- a/youtube_dl/jsinterp.py +++ b/youtube_dl/jsinterp.py @@ -672,14 +672,15 @@ class JSInterpreter(object): except Exception as e: raise self.Exception('Failed to evaluate {left_val!r:.50} {op} {right_val!r:.50}'.format(**locals()), expr, cause=e) - def _index(self, obj, idx, allow_undefined=True): + def _index(self, obj, idx, allow_undefined=None): if idx == 'length' and isinstance(obj, list): return len(obj) try: return obj[int(idx)] if isinstance(obj, list) else obj[compat_str(idx)] except (TypeError, KeyError, IndexError) as e: - if allow_undefined: - # when is not allowed? + # allow_undefined is None gives correct behaviour + if allow_undefined or ( + allow_undefined is None and not isinstance(e, TypeError)): return JS_Undefined raise self.Exception('Cannot get index {idx!r:.100}'.format(**locals()), expr=repr(obj), cause=e)