diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py index b0ac3a704..30c48adfc 100644 --- a/test/test_jsinterp.py +++ b/test/test_jsinterp.py @@ -455,6 +455,7 @@ class TestJSInterpreter(unittest.TestCase): def test_regex(self): self._test('function f() { let a=/,,[/,913,/](,)}/; }', None) + self._test('function f() { let a=/,,[/,913,/](,)}/; return a.source; }', ',,[/,913,/](,)}') jsi = JSInterpreter(''' function x() { let a=/,,[/,913,/](,)}/; "".replace(a, ""); return a; } diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py index 971387df2..4a464743f 100644 --- a/youtube_dl/jsinterp.py +++ b/youtube_dl/jsinterp.py @@ -353,7 +353,7 @@ class LocalNameSpace(ChainMap): raise NotImplementedError('Deleting is not supported') def __repr__(self): - return 'LocalNameSpace%s' % (self.maps, ) + return 'LocalNameSpace({0!r})'.format(self.maps) class Debugger(object): @@ -374,6 +374,9 @@ class Debugger(object): @classmethod def wrap_interpreter(cls, f): + if not cls.ENABLED: + return f + @wraps(f) def interpret_statement(self, stmt, local_vars, allow_recursion, *args, **kwargs): if cls.ENABLED and stmt.strip(): @@ -414,7 +417,17 @@ class JSInterpreter(object): msg = '{0} in: {1!r:.100}'.format(msg.rstrip(), expr) super(JSInterpreter.Exception, self).__init__(msg, *args, **kwargs) - class JS_RegExp(object): + class JS_Object(object): + def __getitem__(self, key): + if hasattr(self, key): + return getattr(self, key) + raise KeyError(key) + + def dump(self): + """Serialise the instance""" + raise NotImplementedError + + class JS_RegExp(JS_Object): RE_FLAGS = { # special knowledge: Python's re flags are bitmask values, current max 128 # invent new bitmask values well above that for literal parsing @@ -435,16 +448,24 @@ class JSInterpreter(object): def __init__(self, pattern_txt, flags=0): if isinstance(flags, compat_str): flags, _ = self.regex_flags(flags) - # First, avoid https://github.com/python/cpython/issues/74534 self.__self = None pattern_txt = str_or_none(pattern_txt) or '(?:)' - self.__pattern_txt = pattern_txt.replace('[[', r'[\[') + # escape unintended embedded flags + pattern_txt = re.sub( + r'(\(\?)([aiLmsux]*)(-[imsx]+:|(?