[pypy-commit] pypy fastjson: raise the appropriate applevel exception if we get an invalid utf8

antocuni noreply at buildbot.pypy.org
Wed Jun 5 16:52:46 CEST 2013


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: fastjson
Changeset: r64801:86bc837fcae3
Date: 2013-06-05 16:18 +0200
http://bitbucket.org/pypy/pypy/changeset/86bc837fcae3/

Log:	raise the appropriate applevel exception if we get an invalid utf8

diff --git a/pypy/module/_fastjson/interp_decoder.py b/pypy/module/_fastjson/interp_decoder.py
--- a/pypy/module/_fastjson/interp_decoder.py
+++ b/pypy/module/_fastjson/interp_decoder.py
@@ -213,7 +213,7 @@
                 content_utf8 = self.getslice(start, self.i-1)
                 if bits & 0x80:
                     # the 8th bit is set, it's an utf8 strnig
-                    content_unicode = content_utf8.decode('utf-8')
+                    content_unicode = unicodehelper.decode_utf8(self.space, content_utf8)
                 else:
                     # ascii only, faster to decode
                     content_unicode = content_utf8.decode('ascii')
diff --git a/pypy/module/_fastjson/test/test__fastjson.py b/pypy/module/_fastjson/test/test__fastjson.py
--- a/pypy/module/_fastjson/test/test__fastjson.py
+++ b/pypy/module/_fastjson/test/test__fastjson.py
@@ -69,6 +69,12 @@
         s = r'"\u1234"'
         assert _fastjson.loads(s) == u'\u1234'
 
+    def test_invalid_utf_8(self):
+        import _fastjson
+        s = '"\xe0"' # this is an invalid UTF8 sequence inside a string
+        raises(UnicodeDecodeError, "_fastjson.loads(s)")
+
+
     def test_decode_numeric(self):
         import _fastjson
         def check(s, val):


More information about the pypy-commit mailing list