#18958: Improve error message for json.load(s) while passing a string that starts with a UTF-8 BOM.

This commit is contained in:
Ezio Melotti 2013-10-21 02:10:55 +03:00
parent a0e768ccc2
commit 566a2be95c
3 changed files with 19 additions and 0 deletions

View file

@ -77,5 +77,19 @@ class TestDecode:
with self.assertRaisesRegex(TypeError, msg):
self.json.load(BytesIO(b'[1,2,3]'))
def test_string_with_utf8_bom(self):
# see #18958
bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
with self.assertRaises(ValueError) as cm:
self.loads(bom_json)
self.assertIn('BOM', str(cm.exception))
with self.assertRaises(ValueError) as cm:
self.json.load(StringIO(bom_json))
self.assertIn('BOM', str(cm.exception))
# make sure that the BOM is not detected in the middle of a string
bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
self.assertEqual(self.loads(bom_in_str), '\ufeff')
self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
class TestPyDecode(TestDecode, PyTest): pass
class TestCDecode(TestDecode, CTest): pass