[pypy-svn] r18626 - in pypy/dist/pypy: objspace/flow objspace/std rpython translator/c/test

arigo at codespeak.net arigo at codespeak.net
Sat Oct 15 15:10:42 CEST 2005


Author: arigo
Date: Sat Oct 15 15:10:41 2005
New Revision: 18626

Modified:
   pypy/dist/pypy/objspace/flow/objspace.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/translator/c/test/test_annotated.py
Log:
unichr(too_big) => ValueError.

Note that "too big" means greater than sys.maxunicode, which
is still taken from the underlying CPython.



Modified: pypy/dist/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/objspace.py	(original)
+++ pypy/dist/pypy/objspace/flow/objspace.py	Sat Oct 15 15:10:41 2005
@@ -437,6 +437,7 @@
 implicit_exceptions = {
     int: [ValueError],      # built-ins that can always raise exceptions
     chr: [ValueError],
+    unichr: [ValueError],
     }
 
 def _add_exceptions(names, exc):

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Sat Oct 15 15:10:41 2005
@@ -337,8 +337,8 @@
         try:
             chars = [unichr(c) for c in chars]
         except ValueError, e:  # unichr(out-of-range)
-            raise OperationError(self.w_ValueError,
-                                 self.wrap("character code not in range(0x110000)"))
+            msg = "character code not in range(%s)" % hex(sys.maxunicode+1)
+            raise OperationError(self.w_ValueError, self.wrap(msg))
         return W_UnicodeObject(self, chars)
 
     def newseqiter(self, w_obj):

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Sat Oct 15 15:10:41 2005
@@ -1,3 +1,4 @@
+import sys
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.objspace import op_appendices
@@ -220,6 +221,9 @@
 
     def rtype_unichr(_, hop):
         vlist =  hop.inputargs(Signed)
+        if hop.has_implicit_exception(ValueError):
+            hop.exception_is_here()
+            hop.gendirectcall(ll_check_unichr, vlist[0])
         return hop.genop('cast_int_to_unichar', vlist, resulttype=UniChar)
 
     def rtype_is_true(self, hop):
@@ -414,6 +418,12 @@
     else:
         raise ValueError
 
+def ll_check_unichr(n):
+    if 0 <= n <= sys.maxunicode:
+        return
+    else:
+        raise ValueError
+
 #
 # _________________________ Conversions _________________________
 

Modified: pypy/dist/pypy/translator/c/test/test_annotated.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_annotated.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_annotated.py	Sat Oct 15 15:10:41 2005
@@ -188,3 +188,14 @@
         assert fn(65) == 'Yes A'
         assert fn(256) == 'No'
         assert fn(-1) == 'No'
+
+    def test_unichr(self):
+        def f(x=int):
+            try:
+                return ord(unichr(x))
+            except ValueError:
+                return -42
+        fn = self.getcompiled(f)
+        assert fn(65) == 65
+        assert fn(-12) == -42
+        assert fn(sys.maxint) == -42



More information about the Pypy-commit mailing list