3 from unittest
import TestCase
5 import simplejson
as json
6 import simplejson
.decoder
8 class TestScanString(TestCase
):
9 def test_py_scanstring(self
):
10 self
._test
_scanstring
(simplejson
.decoder
.py_scanstring
)
12 def test_c_scanstring(self
):
13 if not simplejson
.decoder
.c_scanstring
:
15 self
._test
_scanstring
(simplejson
.decoder
.c_scanstring
)
17 def _test_scanstring(self
, scanstring
):
19 scanstring('"z\\ud834\\udd20x"', 1, None, True),
20 (u
'z\U0001d120x', 16))
22 if sys
.maxunicode
== 65535:
24 scanstring(u
'"z\U0001d120x"', 1, None, True),
28 scanstring(u
'"z\U0001d120x"', 1, None, True),
32 scanstring('"\\u007b"', 1, None, True),
36 scanstring('"A JSON payload should be an object or array, not a string."', 1, None, True),
37 (u
'A JSON payload should be an object or array, not a string.', 60))
40 scanstring('["Unclosed array"', 2, None, True),
41 (u
'Unclosed array', 17))
44 scanstring('["extra comma",]', 2, None, True),
48 scanstring('["double extra comma",,]', 2, None, True),
49 (u
'double extra comma', 21))
52 scanstring('["Comma after the close"],', 2, None, True),
53 (u
'Comma after the close', 24))
56 scanstring('["Extra close"]]', 2, None, True),
60 scanstring('{"Extra comma": true,}', 2, None, True),
64 scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, None, True),
65 (u
'Extra value after close', 26))
68 scanstring('{"Illegal expression": 1 + 2}', 2, None, True),
69 (u
'Illegal expression', 21))
72 scanstring('{"Illegal invocation": alert()}', 2, None, True),
73 (u
'Illegal invocation', 21))
76 scanstring('{"Numbers cannot have leading zeroes": 013}', 2, None, True),
77 (u
'Numbers cannot have leading zeroes', 37))
80 scanstring('{"Numbers cannot be hex": 0x14}', 2, None, True),
81 (u
'Numbers cannot be hex', 24))
84 scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, None, True),
88 scanstring('{"Missing colon" null}', 2, None, True),
89 (u
'Missing colon', 16))
92 scanstring('{"Double colon":: null}', 2, None, True),
93 (u
'Double colon', 15))
96 scanstring('{"Comma instead of colon", null}', 2, None, True),
97 (u
'Comma instead of colon', 25))
100 scanstring('["Colon instead of comma": false]', 2, None, True),
101 (u
'Colon instead of comma', 25))
104 scanstring('["Bad value", truth]', 2, None, True),
107 def test_issue3623(self
):
108 self
.assertRaises(ValueError, json
.decoder
.scanstring
, "xxx", 1,
110 self
.assertRaises(UnicodeDecodeError,
111 json
.encoder
.encode_basestring_ascii
, "xx\xff")