From fe07e788bf7718d429d6fc7e4bcb0c761ffd2cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergey=20M=E2=80=A4?= Date: Tue, 17 Nov 2020 01:30:43 +0700 Subject: [PATCH] [utils] Skip ! prefixed code in js_to_json --- test/test_utils.py | 22 ++++++++++++++++++++++ youtube_dl/utils.py | 5 +++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index c2d1e4fb1..925a21d34 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -937,6 +937,28 @@ class TestUtil(unittest.TestCase): self.assertEqual(d['x'], 1) self.assertEqual(d['y'], 'a') + # Just drop ! prefix for now though this results in a wrong value + on = js_to_json('''{ + a: !0, + b: !1, + c: !!0, + d: !!42.42, + e: !!![], + f: !"abc", + g: !"", + !42: 42 + }''') + self.assertEqual(json.loads(on), { + 'a': 0, + 'b': 1, + 'c': 0, + 'd': 42.42, + 'e': [], + 'f': "abc", + 'g': "", + '42': 42 + }) + on = js_to_json('["abc", "def",]') self.assertEqual(json.loads(on), ['abc', 'def']) diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 737e2810e..321f903ab 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -4078,7 +4078,7 @@ def js_to_json(code): v = m.group(0) if v in ('true', 'false', 'null'): return v - elif v.startswith('/*') or v.startswith('//') or v == ',': + elif v.startswith('/*') or v.startswith('//') or v.startswith('!') or v == ',': return "" if v[0] in ("'", '"'): @@ -4103,7 +4103,8 @@ def js_to_json(code): {comment}|,(?={skip}[\]}}])| (?:(?