[pypy-svn] r71817 - in pypy/trunk/pypy: objspace/flow objspace/flow/test rlib translator/c/test

arigo at codespeak.net arigo at codespeak.net
Fri Mar 5 18:13:11 CET 2010


Author: arigo
Date: Fri Mar  5 18:13:09 2010
New Revision: 71817

Modified:
   pypy/trunk/pypy/objspace/flow/objspace.py
   pypy/trunk/pypy/objspace/flow/test/test_objspace.py
   pypy/trunk/pypy/rlib/runicode.py
   pypy/trunk/pypy/translator/c/test/test_typed.py
Log:
Bah.  Fix an issue with runicode.  It required a bit more
hacking at the flow space.


Modified: pypy/trunk/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/trunk/pypy/objspace/flow/objspace.py	(original)
+++ pypy/trunk/pypy/objspace/flow/objspace.py	Fri Mar  5 18:13:09 2010
@@ -370,6 +370,9 @@
     def call_args(self, w_callable, args):
         try:
             fn = self.unwrap(w_callable)
+            if hasattr(fn, "_flowspace_rewrite_directly_as_"):
+                fn = fn._flowspace_rewrite_directly_as_
+                w_callable = self.wrap(fn)
             sc = self.specialcases[fn]   # TypeError if 'fn' not hashable
         except (UnwrapException, KeyError, TypeError):
             pass

Modified: pypy/trunk/pypy/objspace/flow/test/test_objspace.py
==============================================================================
--- pypy/trunk/pypy/objspace/flow/test/test_objspace.py	(original)
+++ pypy/trunk/pypy/objspace/flow/test/test_objspace.py	Fri Mar  5 18:13:09 2010
@@ -892,6 +892,22 @@
                     raise TypeError()
         py.test.raises(Exception, self.codetest, f)
 
+    def test__flowspace_rewrite_directly_as_(self):
+        def g(x):
+            pass
+        def f(x):
+            pass
+        f._flowspace_rewrite_directly_as_ = g
+        def h(x):
+            f(x)
+        graph = self.codetest(h)
+        assert self.all_operations(graph) == {'simple_call': 1}
+        for block in graph.iterblocks():
+            if block.operations:
+                op = block.operations[0]
+                assert op.opname == 'simple_call'
+                assert op.args[0] == Constant(g)
+
 
 class TestFlowObjSpaceDelay(Base):
     def setup_class(cls):

Modified: pypy/trunk/pypy/rlib/runicode.py
==============================================================================
--- pypy/trunk/pypy/rlib/runicode.py	(original)
+++ pypy/trunk/pypy/rlib/runicode.py	Fri Mar  5 18:13:09 2010
@@ -16,26 +16,28 @@
     # when sizeof(wchar_t) == 4.
     # Note that Python3 uses a similar implementation.
     def UNICHR(c):
-        if we_are_translated():
+        assert not we_are_translated()
+        if c < sys.maxunicode or c > MAXUNICODE:
             return unichr(c)
         else:
-            if c < sys.maxunicode or c > MAXUNICODE:
-                return unichr(c)
-            else:
-                c -= 0x10000
-                return (unichr(0xD800 + (c >> 10)) +
-                        unichr(0xDC00 + (c & 0x03FF)))
+            c -= 0x10000
+            return (unichr(0xD800 + (c >> 10)) +
+                    unichr(0xDC00 + (c & 0x03FF)))
+    UNICHR._flowspace_rewrite_directly_as_ = unichr
+    # ^^^ NB.: for translation, it's essential to use this hack instead
+    # of calling unichr() from UNICHR(), because unichr() detects if there
+    # is a "try:except ValueError" immediately around it.
 
     def ORD(u):
-        if we_are_translated():
-            return ord(u)
-        else:
-            if isinstance(u, unicode) and len(u) == 2:
-                ch1 = ord(u[0])
-                ch2 = ord(u[1])
-                if 0xD800 <= ch1 <= 0xDBFF and 0xDC00 <= ch2 <= 0xDFFF:
-                    return (((ch1 - 0xD800) << 10) | (ch2 - 0xDC00)) + 0x10000
-            return ord(u)
+        assert not we_are_translated()
+        if isinstance(u, unicode) and len(u) == 2:
+            ch1 = ord(u[0])
+            ch2 = ord(u[1])
+            if 0xD800 <= ch1 <= 0xDBFF and 0xDC00 <= ch2 <= 0xDFFF:
+                return (((ch1 - 0xD800) << 10) | (ch2 - 0xDC00)) + 0x10000
+        return ord(u)
+    ORD._flowspace_rewrite_directly_as_ = ord
+
 else:
     UNICHR = unichr
     ORD = ord

Modified: pypy/trunk/pypy/translator/c/test/test_typed.py
==============================================================================
--- pypy/trunk/pypy/translator/c/test/test_typed.py	(original)
+++ pypy/trunk/pypy/translator/c/test/test_typed.py	Fri Mar  5 18:13:09 2010
@@ -212,6 +212,18 @@
         assert fn(-12) == -42
         assert fn(sys.maxint) == -42
 
+    def test_UNICHR(self):
+        from pypy.rlib.runicode import UNICHR
+        def f(x):
+            try:
+                return ord(UNICHR(x))
+            except ValueError:
+                return -42
+        fn = self.getcompiled(f, [int])
+        assert fn(65) == 65
+        assert fn(-12) == -42
+        assert fn(sys.maxint) == -42
+
     def test_list_indexerror(self):
         def f(i):
             lst = [123, 456]



More information about the Pypy-commit mailing list