[pypy-commit] pypy stdlib-2.7.8: complain if the codec doesn't return unicode

pjenvey noreply at buildbot.pypy.org
Sat Aug 23 20:05:42 CEST 2014


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: stdlib-2.7.8
Changeset: r73012:deedb883c0e5
Date: 2014-08-23 11:04 -0700
http://bitbucket.org/pypy/pypy/changeset/deedb883c0e5/

Log:	complain if the codec doesn't return unicode

diff --git a/pypy/interpreter/pyparser/pyparse.py b/pypy/interpreter/pyparser/pyparse.py
--- a/pypy/interpreter/pyparser/pyparse.py
+++ b/pypy/interpreter/pyparser/pyparse.py
@@ -1,17 +1,15 @@
-from pypy.interpreter import gateway
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.pyparser import future, parser, pytokenizer, pygram, error
 from pypy.interpreter.astcompiler import consts
 
+def recode_to_utf8(space, bytes, encoding):
+    w_text = space.call_method(space.wrap(bytes), "decode",
+                               space.wrap(encoding))
+    if not space.isinstance_w(w_text, space.w_unicode):
+        raise error.SyntaxError("codec did not return a unicode object")
+    w_recoded = space.call_method(w_text, "encode", space.wrap("utf-8"))
+    return space.str_w(w_recoded)
 
-_recode_to_utf8 = gateway.applevel(r'''
-    def _recode_to_utf8(text, encoding):
-        return unicode(text, encoding).encode("utf-8")
-''').interphook('_recode_to_utf8')
-
-def recode_to_utf8(space, text, encoding):
-    return space.str_w(_recode_to_utf8(space, space.wrap(text),
-                                          space.wrap(encoding)))
 def _normalize_encoding(encoding):
     """returns normalized name for <encoding>
 
diff --git a/pypy/interpreter/pyparser/test/test_pyparse.py b/pypy/interpreter/pyparser/test/test_pyparse.py
--- a/pypy/interpreter/pyparser/test/test_pyparse.py
+++ b/pypy/interpreter/pyparser/test/test_pyparse.py
@@ -64,6 +64,13 @@
         assert exc.msg == ("'ascii' codec can't decode byte 0xc3 "
                            "in position 16: ordinal not in range(128)")
 
+    def test_non_unicode_codec(self):
+        exc = py.test.raises(SyntaxError, self.parse, """\
+# coding: string-escape
+\x70\x72\x69\x6e\x74\x20\x32\x2b\x32\x0a
+""").value
+        assert exc.msg == "codec did not return a unicode object"
+
     def test_syntax_error(self):
         parse = self.parse
         exc = py.test.raises(SyntaxError, parse, "name another for").value


More information about the pypy-commit mailing list