[pypy-svn] r48425 - in pypy/branch/pypy-rpython-unicode/rpython: ootypesystem ootypesystem/test test

antocuni at codespeak.net antocuni at codespeak.net
Thu Nov 8 18:39:24 CET 2007


Author: antocuni
Date: Thu Nov  8 18:39:23 2007
New Revision: 48425

Modified:
   pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/ootype.py
   pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py
   pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/test/test_ooann.py
   pypy/branch/pypy-rpython-unicode/rpython/test/tool.py
Log:
first step to add Unicode support to ootype; it's not completed
because of a blocker I could not solve yet; see the failing test in
ootypesystem/test/test_ooann.py



Modified: pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/ootype.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/ootype.py	Thu Nov  8 18:39:23 2007
@@ -382,7 +382,7 @@
 # WARNING: the name 'Unicode' is rebound at the end of file
 class Unicode(AbstractString):
     SELFTYPE_T = object()
-    Char = UniChar
+    CHAR = UniChar
 
     def _enforce(self, value):
         TYPE = typeOf(value)
@@ -1074,6 +1074,14 @@
     def __cmp__(self, other):
         return cmp(self._str, other._str)
 
+    def make_string(self, value):
+        if self._TYPE is String:
+            return make_string(value)
+        elif self._TYPE is Unicode:
+            return make_unicode(value)
+        else:
+            assert False, 'Unknown type %s' % self._TYPE
+
     def ll_stritem_nonneg(self, i):
         # NOT_RPYTHON
         s = self._str
@@ -1086,7 +1094,7 @@
 
     def ll_strconcat(self, s):
         # NOT_RPYTHON
-        return make_string(self._str + s._str)
+        return self.make_string(self._str + s._str)
 
     def ll_streq(self, s):
         # NOT_RPYTON
@@ -1135,24 +1143,24 @@
             s = s.lstrip(ch)
         if right:
             s = s.rstrip(ch)
-        return make_string(s)
+        return self.make_string(s)
 
     def ll_upper(self):
         # NOT_RPYTHON
-        return make_string(self._str.upper())
+        return self.make_string(self._str.upper())
 
     def ll_lower(self):
         # NOT_RPYTHON
-        return make_string(self._str.lower())
+        return self.make_string(self._str.lower())
 
     def ll_substring(self, start, count):
         # NOT_RPYTHON
-        return make_string(self._str[start:start+count])
+        return self.make_string(self._str[start:start+count])
 
     def ll_split_chr(self, ch):
         # NOT_RPYTHON
         res = _list(List(String))
-        res._list = [make_string(s) for s in self._str.split(ch)]
+        res._list = [self.make_string(s) for s in self._str.split(ch)]
         return res
 
     def ll_contains(self, ch):
@@ -1161,7 +1169,7 @@
 
     def ll_replace_chr_chr(self, ch1, ch2):
         # NOT_RPYTHON
-        return make_string(self._str.replace(ch1, ch2))
+        return self.make_string(self._str.replace(ch1, ch2))
 
 class _null_string(_null_mixin(_string), _string):
     def __init__(self, STRING):
@@ -1573,6 +1581,7 @@
 
 ROOT = Instance('Root', None, _is_root=True)
 String = String()
+Unicode = Unicode()
 StringBuilder = StringBuilder()
 WeakReference = WeakReference()
 dead_wref = new(WeakReference)

Modified: pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/rstr.py	Thu Nov  8 18:39:23 2007
@@ -10,8 +10,7 @@
 # String and a Char directly without passing to Char-->String
 # conversion
 
-class StringRepr(AbstractStringRepr):
-    lowleveltype = ootype.String
+class AbstractOOStringRepr(AbstractStringRepr):
 
     def __init__(self, *args):
         AbstractStringRepr.__init__(self, *args)
@@ -19,13 +18,16 @@
 
     def convert_const(self, value):
         if value is None:
-            return ootype.String._null
-        if not isinstance(value, str):
+            return self.lowleveltype._null
+        if not isinstance(value, self.basetype):
             raise TyperError("not a str: %r" % (value,))
-        return ootype.make_string(value)
+        return self.make_string(value)
+
+    def make_string(self, value):
+        raise NotImplementedError
 
     def make_iterator_repr(self):
-        return string_iterator_repr
+        return self.string_iterator_repr
 
     def _list_length_items(self, hop, v_lst, LIST):
         # ootypesystem list has a different interface that
@@ -36,6 +38,21 @@
         return c_length, v_lst
 
 
+class StringRepr(AbstractOOStringRepr):
+    lowleveltype = ootype.String
+    basetype = str
+
+    def make_string(self, value):
+        return ootype.make_string(value)
+
+class UnicodeRepr(AbstractOOStringRepr):
+    lowleveltype = ootype.Unicode
+    basetype = basestring
+
+    def make_string(self, value):
+        return ootype.make_unicode(value)
+
+
 class CharRepr(AbstractCharRepr, StringRepr):
     lowleveltype = Char
 
@@ -277,14 +294,19 @@
 del add_helpers
 
 do_stringformat = LLHelpers.do_stringformat
-string_repr = StringRepr()
 char_repr = CharRepr()
 unichar_repr = UniCharRepr()
 char_repr.ll = LLHelpers
 unichar_repr.ll = LLHelpers
-emptystr = string_repr.convert_const("")
+
+string_repr = StringRepr()
 StringRepr.repr = string_repr
 StringRepr.char_repr = char_repr
+emptystr = string_repr.convert_const("")
+unicode_repr = UnicodeRepr()
+UnicodeRepr.repr = unicode_repr
+UnicodeRepr.char_repr = unichar_repr
+
 
 class StringIteratorRepr(AbstractStringIteratorRepr):
     lowleveltype = ootype.Record({'string': string_repr.lowleveltype,
@@ -294,8 +316,22 @@
         self.ll_striter = ll_striter
         self.ll_strnext = ll_strnext
 
+class UnicodeIteratorRepr(AbstractStringIteratorRepr):
+    lowleveltype = ootype.Record({'string': unicode_repr.lowleveltype,
+                                  'index': ootype.Signed})
+
+    def __init__(self):
+        self.ll_striter = ll_unicodeiter
+        self.ll_strnext = ll_strnext
+
 def ll_striter(string):
-    iter = ootype.new(string_iterator_repr.lowleveltype)
+    iter = ootype.new(string_repr.string_iterator_repr.lowleveltype)
+    iter.string = string
+    iter.index = 0
+    return iter
+
+def ll_unicodeiter(string):
+    iter = ootype.new(unicode_repr.string_iterator_repr.lowleveltype)
     iter.string = string
     iter.index = 0
     return iter
@@ -308,8 +344,9 @@
     iter.index = index + 1
     return string.ll_stritem_nonneg(index)
 
-string_iterator_repr = StringIteratorRepr()
 
+StringRepr.string_iterator_repr = StringIteratorRepr()
+UnicodeRepr.string_iterator_repr = UnicodeIteratorRepr()
 
 # these should be in rclass, but circular imports prevent (also it's
 # not that insane that a string constant is built in this file).

Modified: pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/test/test_ooann.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/test/test_ooann.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/ootypesystem/test/test_ooann.py	Thu Nov  8 18:39:23 2007
@@ -324,3 +324,18 @@
         return c.foo(c)
     a = RPythonAnnotator()
     py.test.raises(TypeError, a.build_types, f, [])
+
+def test_unicode_iterator():
+    py.test.skip("That's a nonsense!")
+    from pypy.rpython.ootypesystem import rstr
+    ITER = rstr.UnicodeRepr.string_iterator_repr.lowleveltype
+
+    def fn():
+        it = new(ITER)
+        return it.string
+    a = RPythonAnnotator()
+    res = a.build_types(fn, [])
+
+    assert ITER._field_type("string") is Unicode
+    assert isinstance(res, annmodel.SomeOOInstance)
+    assert res.ootype is Unicode

Modified: pypy/branch/pypy-rpython-unicode/rpython/test/tool.py
==============================================================================
--- pypy/branch/pypy-rpython-unicode/rpython/test/tool.py	(original)
+++ pypy/branch/pypy-rpython-unicode/rpython/test/tool.py	Thu Nov  8 18:39:23 2007
@@ -82,6 +82,8 @@
     def ll_to_string(self, s):
         return s._str
 
+    ll_to_unicode = ll_to_string
+
     def string_to_ll(self, s):
         from pypy.rpython.module.support import OOSupport        
         return OOSupport.to_rstr(s)



More information about the Pypy-commit mailing list