| 1 |
import decimal |
| 2 |
from io import StringIO |
| 3 |
from collections import OrderedDict |
| 4 |
from test.test_json import PyTest, CTest |
| 5 |
from test import support |
| 6 |
|
| 7 |
|
| 8 |
class TestDecode: |
| 9 |
def test_decimal(self): |
| 10 |
rval = self.loads('1.1', parse_float=decimal.Decimal) |
| 11 |
self.assertIsInstance(rval, decimal.Decimal) |
| 12 |
self.assertEqual(rval, decimal.Decimal('1.1')) |
| 13 |
|
| 14 |
def test_float(self): |
| 15 |
rval = self.loads('1', parse_int=float) |
| 16 |
self.assertIsInstance(rval, float) |
| 17 |
self.assertEqual(rval, 1.0) |
| 18 |
|
| 19 |
def test_nonascii_digits_rejected(self): |
| 20 |
# JSON specifies only ascii digits, see gh-125687 |
| 21 |
for num in ["1\uff10", "0.\uff10", "0e\uff10"]: |
| 22 |
with self.assertRaises(self.JSONDecodeError): |
| 23 |
self.loads(num) |
| 24 |
|
| 25 |
def test_bytes(self): |
| 26 |
self.assertEqual(self.loads(b"1"), 1) |
| 27 |
|
| 28 |
def test_parse_constant(self): |
| 29 |
for constant, expected in [ |
| 30 |
("Infinity", "INFINITY"), |
| 31 |
("-Infinity", "-INFINITY"), |
| 32 |
("NaN", "NAN"), |
| 33 |
]: |
| 34 |
self.assertEqual( |
| 35 |
self.loads(constant, parse_constant=str.upper), expected |
| 36 |
) |
| 37 |
|
| 38 |
def test_constant_invalid_case(self): |
| 39 |
for constant in [ |
| 40 |
"nan", "NAN", "naN", "infinity", "INFINITY", "inFiniTy" |
| 41 |
]: |
| 42 |
with self.assertRaises(self.JSONDecodeError): |
| 43 |
self.loads(constant) |
| 44 |
|
| 45 |
def test_empty_objects(self): |
| 46 |
self.assertEqual(self.loads('{}'), {}) |
| 47 |
self.assertEqual(self.loads('[]'), []) |
| 48 |
self.assertEqual(self.loads('""'), "") |
| 49 |
|
| 50 |
def test_object_pairs_hook(self): |
| 51 |
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' |
| 52 |
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), |
| 53 |
("qrt", 5), ("pad", 6), ("hoy", 7)] |
| 54 |
self.assertEqual(self.loads(s), eval(s)) |
| 55 |
self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p) |
| 56 |
self.assertEqual(self.json.load(StringIO(s), |
| 57 |
object_pairs_hook=lambda x: x), p) |
| 58 |
od = self.loads(s, object_pairs_hook=OrderedDict) |
| 59 |
self.assertEqual(od, OrderedDict(p)) |
| 60 |
self.assertEqual(type(od), OrderedDict) |
| 61 |
# the object_pairs_hook takes priority over the object_hook |
| 62 |
self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict, |
| 63 |
object_hook=lambda x: None), |
| 64 |
OrderedDict(p)) |
| 65 |
# check that empty object literals work (see #17368) |
| 66 |
self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict), |
| 67 |
OrderedDict()) |
| 68 |
self.assertEqual(self.loads('{"empty": {}}', |
| 69 |
object_pairs_hook=OrderedDict), |
| 70 |
OrderedDict([('empty', OrderedDict())])) |
| 71 |
|
| 72 |
def test_decoder_optimizations(self): |
| 73 |
# Several optimizations were made that skip over calls to |
| 74 |
# the whitespace regex, so this test is designed to try and |
| 75 |
# exercise the uncommon cases. The array cases are already covered. |
| 76 |
rval = self.loads('{ "key" : "value" , "k":"v" }') |
| 77 |
self.assertEqual(rval, {"key":"value", "k":"v"}) |
| 78 |
|
| 79 |
def check_keys_reuse(self, source, loads): |
| 80 |
rval = loads(source) |
| 81 |
(a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) |
| 82 |
self.assertIs(a, c) |
| 83 |
self.assertIs(b, d) |
| 84 |
|
| 85 |
def test_keys_reuse(self): |
| 86 |
s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' |
| 87 |
self.check_keys_reuse(s, self.loads) |
| 88 |
decoder = self.json.decoder.JSONDecoder() |
| 89 |
self.check_keys_reuse(s, decoder.decode) |
| 90 |
self.assertFalse(decoder.memo) |
| 91 |
|
| 92 |
def test_extra_data(self): |
| 93 |
s = '[1, 2, 3]5' |
| 94 |
msg = 'Extra data' |
| 95 |
self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s) |
| 96 |
|
| 97 |
def test_invalid_escape(self): |
| 98 |
s = '["abc\\y"]' |
| 99 |
msg = 'escape' |
| 100 |
self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s) |
| 101 |
|
| 102 |
def test_invalid_input_type(self): |
| 103 |
msg = 'the JSON object must be str' |
| 104 |
for value in [1, 3.14, [], {}, None]: |
| 105 |
self.assertRaisesRegex(TypeError, msg, self.loads, value) |
| 106 |
|
| 107 |
def test_string_with_utf8_bom(self): |
| 108 |
# see #18958 |
| 109 |
bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8') |
| 110 |
with self.assertRaises(self.JSONDecodeError) as cm: |
| 111 |
self.loads(bom_json) |
| 112 |
self.assertIn('BOM', str(cm.exception)) |
| 113 |
with self.assertRaises(self.JSONDecodeError) as cm: |
| 114 |
self.json.load(StringIO(bom_json)) |
| 115 |
self.assertIn('BOM', str(cm.exception)) |
| 116 |
# make sure that the BOM is not detected in the middle of a string |
| 117 |
bom = ''.encode('utf-8-sig').decode('utf-8') |
| 118 |
bom_in_str = f'"{bom}"' |
| 119 |
self.assertEqual(self.loads(bom_in_str), '\ufeff') |
| 120 |
self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff') |
| 121 |
|
| 122 |
def test_negative_index(self): |
| 123 |
d = self.json.JSONDecoder() |
| 124 |
self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000) |
| 125 |
|
| 126 |
def test_limit_int(self): |
| 127 |
maxdigits = 5000 |
| 128 |
with support.adjust_int_max_str_digits(maxdigits): |
| 129 |
self.loads('1' * maxdigits) |
| 130 |
with self.assertRaises(ValueError): |
| 131 |
self.loads('1' * (maxdigits + 1)) |
| 132 |
|
| 133 |
|
| 134 |
class TestPyDecode(TestDecode, PyTest): pass |
| 135 |
class TestCDecode(TestDecode, CTest): pass |