3 from unittest
import TestCase
8 class TestScanString(TestCase
):
9 def test_py_scanstring(self
):
10 self
._test
_scanstring
(json
.decoder
.py_scanstring
)
12 def test_c_scanstring(self
):
13 self
._test
_scanstring
(json
.decoder
.c_scanstring
)
15 def _test_scanstring(self
, scanstring
):
17 scanstring('"z\\ud834\\udd20x"', 1, None, True),
18 (u
'z\U0001d120x', 16))
20 if sys
.maxunicode
== 65535:
22 scanstring(u
'"z\U0001d120x"', 1, None, True),
26 scanstring(u
'"z\U0001d120x"', 1, None, True),
30 scanstring('"\\u007b"', 1, None, True),
34 scanstring('"A JSON payload should be an object or array, not a string."', 1, None, True),
35 (u
'A JSON payload should be an object or array, not a string.', 60))
38 scanstring('["Unclosed array"', 2, None, True),
39 (u
'Unclosed array', 17))
42 scanstring('["extra comma",]', 2, None, True),
46 scanstring('["double extra comma",,]', 2, None, True),
47 (u
'double extra comma', 21))
50 scanstring('["Comma after the close"],', 2, None, True),
51 (u
'Comma after the close', 24))
54 scanstring('["Extra close"]]', 2, None, True),
58 scanstring('{"Extra comma": true,}', 2, None, True),
62 scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, None, True),
63 (u
'Extra value after close', 26))
66 scanstring('{"Illegal expression": 1 + 2}', 2, None, True),
67 (u
'Illegal expression', 21))
70 scanstring('{"Illegal invocation": alert()}', 2, None, True),
71 (u
'Illegal invocation', 21))
74 scanstring('{"Numbers cannot have leading zeroes": 013}', 2, None, True),
75 (u
'Numbers cannot have leading zeroes', 37))
78 scanstring('{"Numbers cannot be hex": 0x14}', 2, None, True),
79 (u
'Numbers cannot be hex', 24))
82 scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, None, True),
86 scanstring('{"Missing colon" null}', 2, None, True),
87 (u
'Missing colon', 16))
90 scanstring('{"Double colon":: null}', 2, None, True),
91 (u
'Double colon', 15))
94 scanstring('{"Comma instead of colon", null}', 2, None, True),
95 (u
'Comma instead of colon', 25))
98 scanstring('["Colon instead of comma": false]', 2, None, True),
99 (u
'Colon instead of comma', 25))
102 scanstring('["Bad value", truth]', 2, None, True),
105 def test_issue3623(self
):
106 self
.assertRaises(ValueError, json
.decoder
.scanstring
, b
"xxx", 1,
108 self
.assertRaises(UnicodeDecodeError,
109 json
.encoder
.encode_basestring_ascii
, b
"xx\xff")
111 def test_overflow(self
):
112 self
.assertRaises(OverflowError, json
.decoder
.scanstring
, b
"xxx", sys
.maxsize
+1)