[pypy-svn] r48465 - in pypy/branch/pypy-rpython-unicode: annotation rpython rpython/lltypesystem rpython/ootypesystem rpython/test

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Nov 9 12:29:16 CET 2007


Author: cfbolz
Date: Fri Nov  9 12:29:14 2007
New Revision: 48465

Modified:
   pypy/branch/pypy-rpython-unicode/annotation/unaryop.py
   pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py
   pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py
   pypy/branch/pypy-rpython-unicode/rpython/rstr.py
   pypy/branch/pypy-rpython-unicode/rpython/test/test_runicode.py
Log:
add a decode method to strings


Modified: pypy/branch/pypy-rpython-unicode/annotation/unaryop.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/annotation/unaryop.py	(original)
+++ pypy/branch/pypy-rpython-unicode/annotation/unaryop.py	Fri Nov  9 12:29:14 2007
@@ -464,11 +464,12 @@
 class __extend__(SomeUnicodeString):
     def method_encode(uni, s_enc):
         if not s_enc.is_constant():
-            raise TypeError("Non-constant decoding not supported")
+            raise TypeError("Non-constant encoding not supported")
         enc = s_enc.const
         if enc != 'ascii':
-            raise TypeError("Decoding %s not supported for unicode" % (enc,))
+            raise TypeError("Encoding %s not supported for unicode" % (enc,))
         return SomeString()
+    method_encode.can_only_throw = [UnicodeEncodeError]
 
 class __extend__(SomeString):
     def method_upper(str):
@@ -477,6 +478,15 @@
     def method_lower(str):
         return SomeString()
 
+    def method_decode(str, s_enc):
+        if not s_enc.is_constant():
+            raise TypeError("Non-constant encoding not supported")
+        enc = s_enc.const
+        if enc != 'ascii':
+            raise TypeError("Encoding %s not supported for strings" % (enc,))
+        return SomeUnicodeString()
+    method_decode.can_only_throw = [UnicodeDecodeError]
+
 class __extend__(SomeChar):
 
     def len(chr):

Modified: pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/lltypesystem/rstr.py	Fri Nov  9 12:29:14 2007
@@ -228,6 +228,8 @@
         lgt = len(str.chars)
         s = mallocunicode(lgt)
         for i in range(lgt):
+            if ord(str.chars[i]) > 127:
+                raise UnicodeDecodeError
             s.chars[i] = cast_primitive(UniChar, str.chars[i])
         return s
 

Modified: pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py	Fri Nov  9 12:29:14 2007
@@ -90,7 +90,10 @@
         lgt = s.ll_strlen()
         res.ll_allocate(lgt)
         for i in range(lgt):
-            res.ll_append_char(cast_primitive(UniChar, s.ll_stritem_nonneg(i)))
+            c = s.ll_stritem_nonneg(i)
+            if ord(c) > 127:
+                raise UnicodeDecodeError
+            res.ll_append_char(cast_primitive(UniChar, c))
         return res.ll_build()
 
     def ll_unichr2unicode(ch):

Modified: pypy/branch/pypy-rpython-unicode/rpython/rstr.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/rstr.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/rstr.py	Fri Nov  9 12:29:14 2007
@@ -24,8 +24,7 @@
         raise TypeError("Cannot do toupper on unicode string")
 
     def rtype_method_lower(self, hop):
-        raise TypeError("Cannot do toupper on unicode string")
-    
+        raise TypeError("Cannot do tolower on unicode string")
 
 class __extend__(annmodel.SomeString):
     def rtyper_makerepr(self, rtyper):
@@ -253,9 +252,14 @@
             return hop.inputconst(hop.r_result, hop.s_result.const)
         repr = hop.args_r[0].repr
         v_str = hop.inputarg(repr, 0)
-        hop.exception_cannot_occur()
+        hop.exception_is_here()
         return hop.gendirectcall(self.ll.ll_str2unicode, v_str)
 
+    def rtype_method_decode(self, hop):
+        v_self = hop.inputarg(self, 0)
+        hop.exception_is_here()
+        return hop.gendirectcall(self.ll.ll_str2unicode, v_self)
+
     def rtype_float(self, hop):
         hop.has_implicit_exception(ValueError)   # record that we know about it
         string_repr = hop.args_r[0].repr

Modified: pypy/branch/pypy-rpython-unicode/rpython/test/test_runicode.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/test/test_runicode.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/test/test_runicode.py	Fri Nov  9 12:29:14 2007
@@ -100,6 +100,25 @@
         assert self.interpret(f, [38]) == f(38)
         assert self.interpret(f, [138]) == f(138)
 
+    def test_unicode_decode(self):
+        def f(x):
+            y = 'xxx'
+            return (y + chr(x)).decode('ascii')
+
+        assert self.ll_to_string(self.interpret(f, [38])) == f(38)
+
+    def test_unicode_decode_error(self):
+        def f(x):
+            y = 'xxx'
+            try:
+                x = (y + chr(x)).decode('ascii')
+                return len(x)
+            except UnicodeDecodeError:
+                return -1
+
+        assert self.interpret(f, [38]) == f(38)
+        assert self.interpret(f, [138]) == f(138)
+
 
     def test_unichar_const(self):
         def fn(c):



More information about the Pypy-commit mailing list