Skip to content

Commit

Permalink
[jsinterp] Fix bug in operator precedence
Browse files Browse the repository at this point in the history
* from yt-dlp/yt-dlp@164b03c
* added tests
  • Loading branch information
dirkf committed Aug 25, 2022
1 parent 573b134 commit d619dd7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions test/test_jsinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@ def test_try(self):
''')
self.assertEqual(jsi.call_function('x'), 10)

def test_catch(self):
jsi = JSInterpreter('''
function x() { try{throw 10} catch(e){return 5} }
''')
self.assertEqual(jsi.call_function('x'), 5)

@unittest.expectedFailure
def test_finally(self):
jsi = JSInterpreter('''
function x() { try{throw 10} finally {return 42} }
''')
self.assertEqual(jsi.call_function('x'), 42)
jsi = JSInterpreter('''
function x() { try{throw 10} catch(e){return 5} finally {return 42} }
''')
self.assertEqual(jsi.call_function('x'), 42)

def test_nested_try(self):
jsi = JSInterpreter('''
function x() {try {
try{throw 10} finally {throw 42}
} catch(e){return 5} }
''')
self.assertEqual(jsi.call_function('x'), 5)

def test_for_loop_continue(self):
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }
Expand Down
4 changes: 4 additions & 0 deletions test/test_youtube_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
'https://www.youtube.com/s/player/1f7d5369/player_ias.vflset/en_US/base.js',
'batNX7sYqIJdkJ', 'IhOkL_zxbkOZBw',
),
(
'https://www.youtube.com/s/player/dc0c6770/player_ias.vflset/en_US/base.js',
'5EHDMgYLV6HPGk_Mu-kk', 'n9lUJLHbxUI0GQ',
),
]


Expand Down
7 changes: 6 additions & 1 deletion youtube_dl/jsinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import math
import operator
import re
from collections import Counter

from .utils import (
error_to_compat_str,
Expand Down Expand Up @@ -108,8 +109,8 @@ def _js_ternary(cndn, if_true=True, if_false=False):

_COMP_OPERATORS = (
('===', operator.is_),
('==', _js_eq_op(operator.eq)),
('!==', operator.is_not),
('==', _js_eq_op(operator.eq)),
('!=', _js_eq_op(operator.ne)),
('<=', _js_comp_op(operator.le)),
('>=', _js_comp_op(operator.ge)),
Expand Down Expand Up @@ -241,7 +242,9 @@ def _regex_flags(cls, expr):
def _separate(cls, expr, delim=',', max_split=None, skip_delims=None):
if not expr:
return
# collections.Counter() is ~10% slower
counters = {k: 0 for k in _MATCHING_PARENS.values()}
# counters = Counter()
start, splits, pos, delim_len = 0, 0, 0, len(delim) - 1
in_quote, escaping, skipping = None, False, 0
after_op, in_regex_char_group, skip_re = True, False, 0
Expand Down Expand Up @@ -442,6 +445,7 @@ def interpret_statement(self, stmt, local_vars, allow_recursion=100):
return ret, should_abort or should_return

elif md.get('catch'):

catch_expr, expr = self._separate_at_paren(expr[m.end():], '}')
if self._EXC_NAME in local_vars:
catch_vars = local_vars.new_child({m.group('err'): local_vars.pop(self._EXC_NAME)})
Expand All @@ -450,6 +454,7 @@ def interpret_statement(self, stmt, local_vars, allow_recursion=100):
return ret, True

ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)

return ret, should_abort or should_return

elif md.get('for'):
Expand Down

0 comments on commit d619dd7

Please sign in to comment.