Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 566a2be

Browse files
committed
#18958: Improve error message for json.load(s) while passing a string that starts with a UTF-8 BOM.
1 parent a0e768c commit 566a2be

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/json/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
313313
if not isinstance(s, str):
314314
raise TypeError('the JSON object must be str, not {!r}'.format(
315315
s.__class__.__name__))
316+
if s.startswith(u'\ufeff'):
317+
raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
316318
if (cls is None and object_hook is None and
317319
parse_int is None and parse_float is None and
318320
parse_constant is None and object_pairs_hook is None and not kw):

Lib/test/test_json/test_decode.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,19 @@ def test_invalid_input_type(self):
7777
with self.assertRaisesRegex(TypeError, msg):
7878
self.json.load(BytesIO(b'[1,2,3]'))
7979

80+
def test_string_with_utf8_bom(self):
81+
# see #18958
82+
bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
83+
with self.assertRaises(ValueError) as cm:
84+
self.loads(bom_json)
85+
self.assertIn('BOM', str(cm.exception))
86+
with self.assertRaises(ValueError) as cm:
87+
self.json.load(StringIO(bom_json))
88+
self.assertIn('BOM', str(cm.exception))
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'))
91+
self.assertEqual(self.loads(bom_in_str), '\ufeff')
92+
self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
93+
8094
class TestPyDecode(TestDecode, PyTest): pass
8195
class TestCDecode(TestDecode, CTest): pass

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Core and Builtins
6262
Library
6363
-------
6464

65+
- Issue #18958: Improve error message for json.load(s) while passing a string
66+
that starts with a UTF-8 BOM.
67+
6568
- Issue #19307: Improve error message for json.load(s) while passing objects
6669
of the wrong type.
6770

0 commit comments

Comments
 (0)