[pypy-svn] r73757 - pypy/trunk/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Thu Apr 15 09:59:08 CEST 2010


Author: arigo
Date: Thu Apr 15 09:59:07 2010
New Revision: 73757

Modified:
   pypy/trunk/pypy/objspace/std/formatting.py
   pypy/trunk/pypy/objspace/std/stringobject.py
   pypy/trunk/pypy/objspace/std/unicodetype.py
Log:
Fix non-RPython code: can't read the attributes of a UnicodeDecodeError.


Modified: pypy/trunk/pypy/objspace/std/formatting.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/formatting.py	(original)
+++ pypy/trunk/pypy/objspace/std/formatting.py	Thu Apr 15 09:59:07 2010
@@ -329,7 +329,8 @@
             length = len(r)
             if do_unicode and isinstance(r, str):
                 # convert string to unicode explicitely here
-                r = unicode(r)
+                from pypy.objspace.std.unicodetype import plain_str2unicode
+                r = plain_str2unicode(self.space, r)
             prec = self.prec
             if prec == -1 and self.width == 0:
                 # fast path
@@ -488,18 +489,8 @@
             result = formatter.format()
         except NeedUnicodeFormattingError:
             # fall through to the unicode case
-            try:
-                fmt = unicode(fmt)
-            except UnicodeDecodeError, e:
-                raise OperationError(space.w_UnicodeDecodeError,
-                    space.newtuple([
-                        space.wrap(e.encoding),
-                        space.wrap(e.object),
-                        space.wrap(e.start),
-                        space.wrap(e.end),
-                        space.wrap(e.reason),
-                    ])
-                )
+            from pypy.objspace.std.unicodetype import plain_str2unicode
+            fmt = plain_str2unicode(space, fmt)
         else:
             return space.wrap(result)
     else:

Modified: pypy/trunk/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/stringobject.py	Thu Apr 15 09:59:07 2010
@@ -39,25 +39,10 @@
 W_StringObject.PREBUILT = [W_StringObject(chr(i)) for i in range(256)]
 del i
 
-def _decode_ascii(space, s):
-    try:
-        return s.decode("ascii")
-    except UnicodeDecodeError:
-        for i in range(len(s)):
-            if ord(s[i]) > 127:
-                raise OperationError(
-                    space.w_UnicodeDecodeError,
-                    space.newtuple([
-                    space.wrap('ascii'),
-                    space.wrap(s),
-                    space.wrap(i),
-                    space.wrap(i+1),
-                    space.wrap("ordinal not in range(128)")]))
-        assert False, "unreachable"
-
 def unicode_w__String(space, w_self):
     # XXX should this use the default encoding?
-    return _decode_ascii(space, w_self._value)
+    from pypy.objspace.std.unicodetype import plain_str2unicode
+    return plain_str2unicode(space, w_self._value)
 
 def _is_generic(space, w_self, fun): 
     v = w_self._value

Modified: pypy/trunk/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/unicodetype.py	(original)
+++ pypy/trunk/pypy/objspace/std/unicodetype.py	Thu Apr 15 09:59:07 2010
@@ -13,6 +13,22 @@
         return wrapunicode(space, uni)
     return W_UnicodeObject(uni)
 
+def plain_str2unicode(space, s):
+    try:
+        return unicode(s)
+    except UnicodeDecodeError:
+        for i in range(len(s)):
+            if ord(s[i]) > 127:
+                raise OperationError(
+                    space.w_UnicodeDecodeError,
+                    space.newtuple([
+                    space.wrap('ascii'),
+                    space.wrap(s),
+                    space.wrap(i),
+                    space.wrap(i+1),
+                    space.wrap("ordinal not in range(128)")]))
+        assert False, "unreachable"
+
 
 unicode_capitalize = SMM('capitalize', 1,
                          doc='S.capitalize() -> unicode\n\nReturn a'



More information about the Pypy-commit mailing list