[pypy-svn] r42218 - in pypy/branch/pypy-string-formatting: annotation rpython rpython/test

arigo at codespeak.net arigo at codespeak.net
Fri Apr 20 21:34:21 CEST 2007


Author: arigo
Date: Fri Apr 20 21:34:20 2007
New Revision: 42218

Modified:
   pypy/branch/pypy-string-formatting/annotation/binaryop.py
   pypy/branch/pypy-string-formatting/rpython/rstr.py
   pypy/branch/pypy-string-formatting/rpython/test/test_rstr.py
Log:
Allow the RPython type 'character' to be converted to 'unicode
character' transparently.


Modified: pypy/branch/pypy-string-formatting/annotation/binaryop.py
==============================================================================
--- pypy/branch/pypy-string-formatting/annotation/binaryop.py	(original)
+++ pypy/branch/pypy-string-formatting/annotation/binaryop.py	Fri Apr 20 21:34:20 2007
@@ -399,7 +399,10 @@
     def union((chr1, chr2)):
         return SomeChar()
 
-class __extend__(pairtype(SomeUnicodeCodePoint, SomeUnicodeCodePoint)):
+
+class __extend__(pairtype(SomeUnicodeCodePoint, SomeUnicodeCodePoint),
+                 pairtype(SomeChar, SomeUnicodeCodePoint),
+                 pairtype(SomeUnicodeCodePoint, SomeChar)):
 
     def union((uchr1, uchr2)):
         return SomeUnicodeCodePoint()

Modified: pypy/branch/pypy-string-formatting/rpython/rstr.py
==============================================================================
--- pypy/branch/pypy-string-formatting/rpython/rstr.py	(original)
+++ pypy/branch/pypy-string-formatting/rpython/rstr.py	Fri Apr 20 21:34:20 2007
@@ -7,7 +7,7 @@
 from pypy.rpython.rtuple import AbstractTupleRepr
 from pypy.rpython.rslice import AbstractSliceRepr
 from pypy.rpython import rint
-from pypy.rpython.lltypesystem.lltype import Signed, Bool, Void
+from pypy.rpython.lltypesystem.lltype import Signed, Bool, Void, UniChar
 
 class AbstractStringRepr(Repr):
     pass
@@ -496,6 +496,10 @@
             return llops.gendirectcall(r_from.ll.ll_stritem_nonneg, v, c_zero)
         return NotImplemented
 
+class __extend__(pairtype(AbstractCharRepr, AbstractUniCharRepr)):
+    def convert_from_to((r_from, r_to), v, llops):
+        v2 = llops.genop('cast_char_to_int', [v], resulttype=Signed)
+        return llops.genop('cast_int_to_unichar', [v2], resulttype=UniChar)
 
 # ____________________________________________________________
 #

Modified: pypy/branch/pypy-string-formatting/rpython/test/test_rstr.py
==============================================================================
--- pypy/branch/pypy-string-formatting/rpython/test/test_rstr.py	(original)
+++ pypy/branch/pypy-string-formatting/rpython/test/test_rstr.py	Fri Apr 20 21:34:20 2007
@@ -148,6 +148,18 @@
             return d[c]
         assert self.interpret(fn, [u'\u03b1']) == 42
 
+    def test_convert_char_to_unichar(self):
+        def g(c):
+            return ord(c)
+        def fn(n):
+            if n < 0:
+                c = unichr(-n)
+            else:
+                c = chr(n)
+            return g(c)
+        assert self.interpret(fn, [65]) == 65
+        assert self.interpret(fn, [-5555]) == 5555
+
     def test_is_none(self):
         def fn(i):
             s1 = ['foo', None][i]



More information about the Pypy-commit mailing list