summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_json/test_decode.py
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2013-10-20 23:10:55 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2013-10-20 23:10:55 (GMT)
commit566a2be95c3aba65302d3d5b8108b27f810e1eaf (patch)
treee093ba0634bea66fa1561c13c88eccccc0aae299 /Lib/test/test_json/test_decode.py
parenta0e768ccc250dbe6ffab812b7964538013ae36c4 (diff)
downloadcpython-566a2be95c3aba65302d3d5b8108b27f810e1eaf.zip
cpython-566a2be95c3aba65302d3d5b8108b27f810e1eaf.tar.gz
cpython-566a2be95c3aba65302d3d5b8108b27f810e1eaf.tar.bz2
#18958: Improve error message for json.load(s) while passing a string that starts with a UTF-8 BOM.
Diffstat (limited to 'Lib/test/test_json/test_decode.py')
-rw-r--r--Lib/test/test_json/test_decode.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py
index 05d07b7..35c02de 100644
--- a/Lib/test/test_json/test_decode.py
+++ b/Lib/test/test_json/test_decode.py
@@ -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