[utils] Correct octal/hexadecimal number detection in js_to_json

This commit is contained in:
Yen Chi Hsuan 2016-08-19 20:37:17 +08:00
parent 9e5751b9fe
commit e4659b4547
No known key found for this signature in database
GPG Key ID: 3FDDD575826C5C30
3 changed files with 12 additions and 3 deletions

View File

@ -1,3 +1,9 @@
version <unreleased>
Core
* Fix js_to_json(): correct octal or hexadecimal number detection
version 2016.08.19 version 2016.08.19
Core Core

View File

@ -712,6 +712,9 @@ class TestUtil(unittest.TestCase):
inp = '''{"foo":101}''' inp = '''{"foo":101}'''
self.assertEqual(js_to_json(inp), '''{"foo":101}''') self.assertEqual(js_to_json(inp), '''{"foo":101}''')
inp = '''{"duration": "00:01:07"}'''
self.assertEqual(js_to_json(inp), '''{"duration": "00:01:07"}''')
def test_js_to_json_edgecases(self): def test_js_to_json_edgecases(self):
on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}") on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}")
self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"}) self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"})

View File

@ -2038,14 +2038,14 @@ def js_to_json(code):
}.get(m.group(0), m.group(0)), v[1:-1]) }.get(m.group(0), m.group(0)), v[1:-1])
INTEGER_TABLE = ( INTEGER_TABLE = (
(r'^0[xX][0-9a-fA-F]+', 16), (r'^(0[xX][0-9a-fA-F]+)\s*:?$', 16),
(r'^0+[0-7]+', 8), (r'^(0+[0-7]+)\s*:?$', 8),
) )
for regex, base in INTEGER_TABLE: for regex, base in INTEGER_TABLE:
im = re.match(regex, v) im = re.match(regex, v)
if im: if im:
i = int(im.group(0), base) i = int(im.group(1), base)
return '"%d":' % i if v.endswith(':') else '%d' % i return '"%d":' % i if v.endswith(':') else '%d' % i
return '"%s"' % v return '"%s"' % v