Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(165398)

Side by Side Diff: Lib/test/test_json/test_decode.py

Issue 19361: Specialize exceptions thrown by JSON parser
Patch Set: Created 5 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_json/__init__.py ('k') | Lib/test/test_json/test_fail.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import decimal 1 import decimal
2 from io import StringIO, BytesIO 2 from io import StringIO, BytesIO
3 from collections import OrderedDict 3 from collections import OrderedDict
4 from test.test_json import PyTest, CTest 4 from test.test_json import PyTest, CTest
5 5
6 6
7 class TestDecode: 7 class TestDecode:
8 def test_decimal(self): 8 def test_decimal(self):
9 rval = self.loads('1.1', parse_float=decimal.Decimal) 9 rval = self.loads('1.1', parse_float=decimal.Decimal)
10 self.assertTrue(isinstance(rval, decimal.Decimal)) 10 self.assertTrue(isinstance(rval, decimal.Decimal))
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 self.assertIs(b, d) 56 self.assertIs(b, d)
57 57
58 def test_keys_reuse(self): 58 def test_keys_reuse(self):
59 s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' 59 s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'
60 self.check_keys_reuse(s, self.loads) 60 self.check_keys_reuse(s, self.loads)
61 self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode) 61 self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode)
62 62
63 def test_extra_data(self): 63 def test_extra_data(self):
64 s = '[1, 2, 3]5' 64 s = '[1, 2, 3]5'
65 msg = 'Extra data' 65 msg = 'Extra data'
66 self.assertRaisesRegex(ValueError, msg, self.loads, s) 66 self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
67 67
68 def test_invalid_escape(self): 68 def test_invalid_escape(self):
69 s = '["abc\\y"]' 69 s = '["abc\\y"]'
70 msg = 'escape' 70 msg = 'escape'
71 self.assertRaisesRegex(ValueError, msg, self.loads, s) 71 self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s)
72 72
73 def test_invalid_input_type(self): 73 def test_invalid_input_type(self):
74 msg = 'the JSON object must be str' 74 msg = 'the JSON object must be str'
75 for value in [1, 3.14, b'bytes', b'\xff\x00', [], {}, None]: 75 for value in [1, 3.14, b'bytes', b'\xff\x00', [], {}, None]:
76 self.assertRaisesRegex(TypeError, msg, self.loads, value) 76 self.assertRaisesRegex(TypeError, msg, self.loads, value)
77 with self.assertRaisesRegex(TypeError, msg): 77 with self.assertRaisesRegex(TypeError, msg):
78 self.json.load(BytesIO(b'[1,2,3]')) 78 self.json.load(BytesIO(b'[1,2,3]'))
79 79
80 def test_string_with_utf8_bom(self): 80 def test_string_with_utf8_bom(self):
81 # see #18958 81 # see #18958
82 bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8') 82 bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
83 with self.assertRaises(ValueError) as cm: 83 with self.assertRaises(self.JSONDecodeError) as cm:
84 self.loads(bom_json) 84 self.loads(bom_json)
85 self.assertIn('BOM', str(cm.exception)) 85 self.assertIn('BOM', str(cm.exception))
86 with self.assertRaises(ValueError) as cm: 86 with self.assertRaises(self.JSONDecodeError) as cm:
87 self.json.load(StringIO(bom_json)) 87 self.json.load(StringIO(bom_json))
88 self.assertIn('BOM', str(cm.exception)) 88 self.assertIn('BOM', str(cm.exception))
89 # make sure that the BOM is not detected in the middle of a string 89 # make sure that the BOM is not detected in the middle of a string
90 bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8')) 90 bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
91 self.assertEqual(self.loads(bom_in_str), '\ufeff') 91 self.assertEqual(self.loads(bom_in_str), '\ufeff')
92 self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff') 92 self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
93 93
94 def test_negative_index(self): 94 def test_negative_index(self):
95 d = self.json.JSONDecoder() 95 d = self.json.JSONDecoder()
96 self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000) 96 self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
97 97
98 class TestPyDecode(TestDecode, PyTest): pass 98 class TestPyDecode(TestDecode, PyTest): pass
99 class TestCDecode(TestDecode, CTest): pass 99 class TestCDecode(TestDecode, CTest): pass
OLDNEW
« no previous file with comments | « Lib/test/test_json/__init__.py ('k') | Lib/test/test_json/test_fail.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+