Author: Ronan Lamy <[email protected]>
Branch: union-side-effects
Changeset: r86868:ec11787ac32c
Date: 2016-09-04 16:27 +0100
http://bitbucket.org/pypy/pypy/changeset/ec11787ac32c/

Log:    Disallow unions of char and unichar, since they make the annotator
        inconsistent

diff --git a/rpython/annotator/binaryop.py b/rpython/annotator/binaryop.py
--- a/rpython/annotator/binaryop.py
+++ b/rpython/annotator/binaryop.py
@@ -378,11 +378,6 @@
         return SomeChar(no_nul=no_nul)
 
 
-class __extend__(pairtype(SomeChar, SomeUnicodeCodePoint),
-                 pairtype(SomeUnicodeCodePoint, SomeChar)):
-    def union((uchr1, uchr2)):
-        return SomeUnicodeCodePoint()
-
 class __extend__(pairtype(SomeUnicodeCodePoint, SomeUnicodeCodePoint)):
     def union((uchr1, uchr2)):
         no_nul = uchr1.no_nul and uchr2.no_nul
diff --git a/rpython/annotator/test/test_annrpython.py 
b/rpython/annotator/test/test_annrpython.py
--- a/rpython/annotator/test/test_annrpython.py
+++ b/rpython/annotator/test/test_annrpython.py
@@ -3453,17 +3453,6 @@
         assert isinstance(s, annmodel.SomeUnicodeString)
         assert s.no_nul
 
-    def test_unicode_char(self):
-        def f(x, i):
-            for c in x:
-                if c == i:
-                    return c
-            return 'x'
-
-        a = self.RPythonAnnotator()
-        s = a.build_types(f, [unicode, str])
-        assert isinstance(s, annmodel.SomeUnicodeCodePoint)
-
     def test_strformatting_unicode(self):
         def f(x):
             return '%s' % unichr(x)
diff --git a/rpython/rtyper/lltypesystem/rstr.py 
b/rpython/rtyper/lltypesystem/rstr.py
--- a/rpython/rtyper/lltypesystem/rstr.py
+++ b/rpython/rtyper/lltypesystem/rstr.py
@@ -727,6 +727,11 @@
         count = 0
         n = end - start
         m = len(s2.chars)
+        tp = typeOf(s1)
+        if tp == string_repr.lowleveltype or tp == Char:
+            NUL = '\0'
+        else:
+            NUL = u'\0'
 
         if m == 0:
             if mode == FAST_COUNT:
@@ -771,7 +776,7 @@
                     if i + m < len(s1.chars):
                         c = s1.chars[i + m]
                     else:
-                        c = '\0'
+                        c = NUL
                     if not bloom(mask, c):
                         i += m
                     else:
@@ -780,7 +785,7 @@
                     if i + m < len(s1.chars):
                         c = s1.chars[i + m]
                     else:
-                        c = '\0'
+                        c = NUL
                     if not bloom(mask, c):
                         i += m
         else:
diff --git a/rpython/rtyper/rstr.py b/rpython/rtyper/rstr.py
--- a/rpython/rtyper/rstr.py
+++ b/rpython/rtyper/rstr.py
@@ -715,9 +715,7 @@
         return hop.genop('cast_unichar_to_int', vlist, resulttype=Signed)
 
 
-class __extend__(pairtype(AbstractUniCharRepr, AbstractUniCharRepr),
-                 pairtype(AbstractCharRepr, AbstractUniCharRepr),
-                 pairtype(AbstractUniCharRepr, AbstractCharRepr)):
+class __extend__(pairtype(AbstractUniCharRepr, AbstractUniCharRepr)):
     def rtype_eq(_, hop): return _rtype_unchr_compare_template(hop, 'eq')
     def rtype_ne(_, hop): return _rtype_unchr_compare_template(hop, 'ne')
     def rtype_lt(_, hop): return _rtype_unchr_compare_template_ord(hop, 'lt')
@@ -737,12 +735,7 @@
     vlist = hop.inputargs(*hop.args_r)
     vlist2 = []
     for v in vlist:
-        if v.concretetype == lltype.Char:
-            v = hop.genop('cast_char_to_int', [v], resulttype=lltype.Signed)
-        elif v.concretetype == lltype.UniChar:
-            v = hop.genop('cast_unichar_to_int', [v], resulttype=lltype.Signed)
-        else:
-            assert 0, v.concretetype
+        v = hop.genop('cast_unichar_to_int', [v], resulttype=lltype.Signed)
         vlist2.append(v)
     return hop.genop('int_' + func, vlist2, resulttype=Bool)
 
@@ -767,11 +760,6 @@
             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)
-
 # ____________________________________________________________
 #
 #  Iteration.
diff --git a/rpython/rtyper/test/test_rlist.py 
b/rpython/rtyper/test/test_rlist.py
--- a/rpython/rtyper/test/test_rlist.py
+++ b/rpython/rtyper/test/test_rlist.py
@@ -1321,7 +1321,7 @@
 
     def test_unicharlist_extension_1(self):
         def f(n):
-            s = 'hello%d' % n
+            s = u'hello%d' % n
             l = [u'a', u'b']
             l += s
             return ''.join([chr(ord(c)) for c in l])
@@ -1348,7 +1348,7 @@
 
     def test_unicharlist_extension_2(self):
         def f(n, i):
-            s = 'hello%d' % n
+            s = u'hello%d' % n
             assert 0 <= i <= len(s)
             l = [u'a', u'b']
             l += s[i:]
@@ -1377,7 +1377,7 @@
 
     def test_unicharlist_extension_3(self):
         def f(n, i, j):
-            s = 'hello%d' % n
+            s = u'hello%d' % n
             assert 0 <= i <= j <= len(s)
             l = [u'a', u'b']
             l += s[i:j]
@@ -1396,7 +1396,7 @@
 
     def test_unicharlist_extension_4(self):
         def f(n):
-            s = 'hello%d' % n
+            s = u'hello%d' % n
             l = [u'a', u'b']
             l += s[:-1]
             return ''.join([chr(ord(c)) for c in l])
@@ -1416,7 +1416,7 @@
     def test_unicharlist_extension_5(self):
         def f(count):
             l = [u'a', u'b']
-            l += '.' * count     # NON-UNICODE-char * count
+            l += u'.' * count
             return ''.join([chr(ord(c)) for c in l])
         res = self.interpret(f, [7])
         assert self.ll_to_string(res) == 'ab.......'
diff --git a/rpython/rtyper/test/test_rstr.py b/rpython/rtyper/test/test_rstr.py
--- a/rpython/rtyper/test/test_rstr.py
+++ b/rpython/rtyper/test/test_rstr.py
@@ -1120,7 +1120,7 @@
             for i, x in enumerate(s):
                 if i == n:
                     return x
-            return 'x'
+            return const('x')
         res = self.interpret(fn, [2])
         assert res == 'c'
 
diff --git a/rpython/rtyper/test/test_runicode.py 
b/rpython/rtyper/test/test_runicode.py
--- a/rpython/rtyper/test/test_runicode.py
+++ b/rpython/rtyper/test/test_runicode.py
@@ -216,33 +216,6 @@
             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_char_unichar_eq(self):
-        def fn(c1, c2):
-            return c1 == c2
-        assert self.interpret(fn, [u'(', '(']) == True
-        assert self.interpret(fn, [u'\u1028', '(']) == False
-        assert self.interpret(fn, ['(', u'(']) == True
-        assert self.interpret(fn, ['(', u'\u1028']) == False
-
-    def test_char_unichar_eq_2(self):
-        def fn(c1):
-            return c1 == 'X'
-        assert self.interpret(fn, [u'(']) == False
-        assert self.interpret(fn, [u'\u1058']) == False
-        assert self.interpret(fn, [u'X']) == True
-
     def test_strformat_unicode_arg(self):
         const = self.const
         def percentS(s, i):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to