[pypy-svn] r48328 - in pypy/branch/pypy-rpython-unicode/annotation: . test

fijal at codespeak.net fijal at codespeak.net
Tue Nov 6 01:08:35 CET 2007


Author: fijal
Date: Tue Nov  6 01:08:34 2007
New Revision: 48328

Modified:
   pypy/branch/pypy-rpython-unicode/annotation/binaryop.py
   pypy/branch/pypy-rpython-unicode/annotation/test/test_annrpython.py
   pypy/branch/pypy-rpython-unicode/annotation/unaryop.py
Log:
Proper support for unicode() and unichr() operations


Modified: pypy/branch/pypy-rpython-unicode/annotation/binaryop.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/annotation/binaryop.py	(original)
+++ pypy/branch/pypy-rpython-unicode/annotation/binaryop.py	Tue Nov  6 01:08:34 2007
@@ -433,6 +433,9 @@
     def union((uchr1, uchr2)):
         return SomeUnicodeCodePoint()
 
+    def add((chr1, chr2)):
+        return SomeUnicodeString()
+
 class __extend__(pairtype(SomeString, SomeObject)):
 
     def mod((str, args)):
@@ -664,10 +667,25 @@
 
 class __extend__(pairtype(SomeString, SomeUnicodeString),
                  pairtype(SomeUnicodeString, SomeString),
+                 pairtype(SomeUnicodeCodePoint, SomeString),
+                 pairtype(SomeString, SomeUnicodeCodePoint),
+                 pairtype(SomeUnicodeCodePoint, SomeUnicodeString),
+                 pairtype(SomeUnicodeString, SomeUnicodeCodePoint),
+                 pairtype(SomeUnicodeString, SomeChar),
+                 pairtype(SomeChar, SomeUnicodeString),
+                 pairtype(SomeUnicodeCodePoint, SomeChar),
+                 pairtype(SomeChar, SomeUnicodeCodePoint),
                  pairtype(SomeUnicodeString, SomeUnicodeString)):
     def union((str1, str2)):
-        return SomeUnicodeString(can_be_None=str1.can_be_None or
-                                 str2.can_be_None)
+        return SomeUnicodeString(can_be_None=str1.can_be_none() or
+                                 str2.can_be_none())
+
+    def add((str1, str2)):
+        # propagate const-ness to help getattr(obj, 'prefix' + const_name)
+        result = SomeUnicodeString()
+        if str1.is_immutable_constant() and str2.is_immutable_constant():
+            result.const = str1.const + str2.const
+        return result
 
 class __extend__(pairtype(SomeInteger, SomeList)):
     

Modified: pypy/branch/pypy-rpython-unicode/annotation/test/test_annrpython.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/annotation/test/test_annrpython.py	(original)
+++ pypy/branch/pypy-rpython-unicode/annotation/test/test_annrpython.py	Tue Nov  6 01:08:34 2007
@@ -2906,6 +2906,28 @@
         s = a.build_types(f, [str])
         assert isinstance(s, annmodel.SomeUnicodeString)
 
+    def test_unicode_add(self):
+        def f(x):
+            return unicode(x) + unichr(1234)
+
+        def g(x):
+            return unichr(x) + unichr(2)
+
+        def h(x):
+            return x + u'a'
+
+        def i(x):
+            return unicode(x) + 'xxx'
+
+        for func in f, h, i:
+            a = self.RPythonAnnotator()
+            s = a.build_types(f, [str])
+            assert isinstance(s, annmodel.SomeUnicodeString)
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int])
+        assert isinstance(s, annmodel.SomeUnicodeString)
+
+
 def g(n):
     return [0,1,2,n]
 

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	Tue Nov  6 01:08:34 2007
@@ -9,7 +9,7 @@
      SomeExternalObject, SomeTypedAddressAccess, SomeAddress, \
      SomeCTypesObject, s_ImpossibleValue, s_Bool, s_None, \
      unionof, set, missing_operation, add_knowntypedata, HarmlesslyBlocked, \
-     SomeGenericCallable, SomeWeakRef
+     SomeGenericCallable, SomeWeakRef, SomeUnicodeString
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.annotation import builtin
 from pypy.annotation.binaryop import _clone ## XXX where to put this?
@@ -25,7 +25,7 @@
                         'iter', 'next', 'invert', 'type', 'issubtype',
                         'pos', 'neg', 'nonzero', 'abs', 'hex', 'oct',
                         'ord', 'int', 'float', 'long', 'id',
-                        'neg_ovf', 'abs_ovf', 'hint'])
+                        'neg_ovf', 'abs_ovf', 'hint', 'unicode', 'unichr'])
 
 for opname in UNARY_OPERATIONS:
     missing_operation(SomeObject, opname)
@@ -103,6 +103,10 @@
         getbookkeeper().count('str', obj)
         return SomeString()
 
+    def unicode(obj):
+        getbookkeeper().count('unicode', obj)
+        return SomeUnicodeString()
+
     def repr(obj):
         getbookkeeper().count('repr', obj)
         return SomeString()
@@ -202,6 +206,10 @@
     def invert(self):
         return SomeInteger(knowntype=self.knowntype)
 
+    def unichr(obj):
+        getbookkeeper().count('unichr', obj)
+        return SomeUnicodeCodePoint()
+
     invert.can_only_throw = []
 
     def pos(self):



More information about the Pypy-commit mailing list