[Python-checkins] cpython: #18958: Improve error message for json.load(s) while passing a string that

ezio.melotti python-checkins at python.org
Mon Oct 21 01:11:10 CEST 2013


http://hg.python.org/cpython/rev/ac016cba7e64
changeset:   86525:ac016cba7e64
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Mon Oct 21 02:10:55 2013 +0300
summary:
  #18958: Improve error message for json.load(s) while passing a string that starts with a UTF-8 BOM.

files:
  Lib/json/__init__.py              |   2 ++
  Lib/test/test_json/test_decode.py |  14 ++++++++++++++
  Misc/NEWS                         |   3 +++
  3 files changed, 19 insertions(+), 0 deletions(-)


diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
--- a/Lib/json/__init__.py
+++ b/Lib/json/__init__.py
@@ -313,6 +313,8 @@
     if not isinstance(s, str):
         raise TypeError('the JSON object must be str, not {!r}'.format(
                             s.__class__.__name__))
+    if s.startswith(u'\ufeff'):
+        raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
     if (cls is None and object_hook is None and
             parse_int is None and parse_float is None and
             parse_constant is None and object_pairs_hook is None and not kw):
diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py
--- a/Lib/test/test_json/test_decode.py
+++ b/Lib/test/test_json/test_decode.py
@@ -77,5 +77,19 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -62,6 +62,9 @@
 Library
 -------
 
+- Issue #18958: Improve error message for json.load(s) while passing a string
+  that starts with a UTF-8 BOM.
+
 - Issue #19307: Improve error message for json.load(s) while passing objects
   of the wrong type.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list