[pypy-svn] r27128 - in pypy/dist/pypy/rpython: lltypesystem ootypesystem ootypesystem/test test

antocuni at codespeak.net antocuni at codespeak.net
Fri May 12 14:29:50 CEST 2006


Author: antocuni
Date: Fri May 12 14:29:41 2006
New Revision: 27128

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rstr.py
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rstr.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
More work on ootype.String:

  - String has been made a subclass of BuiltinADTType instead of
    OOType, so it can be treated as List and Dict; String will have a
    set of _METHODS corresponding to those of the RPythonic str;
    methods' name will start with 'll_' (e.g., ll_join instead of
    join);

  - _string is no longer a subclass of str; when an attribute whose
    name starts with 'll_' is accesed _string string delegates the
    implementation to its member '_str', thus avoiding writing a lot
    of boilerplate.

  - rstr.StringRepr has been enhanced; a test for ootypesystem has
    been written in test/test_rstr.py.



Modified: pypy/dist/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rstr.py	Fri May 12 14:29:41 2006
@@ -108,8 +108,6 @@
 #  get flowed and annotated, mostly with SomePtr.
 #
 
-# TODO: move it to a better place
-
 class LLHelpers:
     __metaclass__ = StaticMethods
 

Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Fri May 12 14:29:41 2006
@@ -181,16 +181,6 @@
         StaticMethod.__init__(self, args, result)
 
 
-class String(OOType):
-
-    def _defl(self):
-        return make_string("")
-    
-    def _example(self):
-        return self._defl()
-
-String = String()
-
 class BuiltinType(OOType):
 
     def _example(self):
@@ -276,6 +266,30 @@
         return self, meth
 
 
+# WARNING: the name 'String' is rebound at the end of file
+class String(BuiltinADTType):
+
+    def __init__(self):
+        self._GENERIC_METHODS = frozendict({
+            "ll_stritem_nonneg": Meth([Signed], Char),
+            "ll_stritem": Meth([Signed], Char),
+        })
+
+        self._setup_methods({})
+
+    def _defl(self):
+        return make_string("")
+    
+    def _example(self):
+        return self._defl()
+
+    def _get_interp_class(self):
+        return _string
+
+    def _specialize(self, generic_types):
+        return self
+
+
 class List(BuiltinADTType):
     # placeholders for types
     # make sure that each derived class has his own SELFTYPE_T
@@ -654,7 +668,7 @@
 
 def make_string(value):
     assert isinstance(value, str)
-    return _string(value)
+    return _string(String, value)
 
 def make_instance(INSTANCE):
     inst = _instance(INSTANCE)
@@ -738,8 +752,6 @@
        callb, checked_args = self.meth._checkargs(args)
        return callb(self.inst, *checked_args)
 
-class _string(str):
-    _TYPE = String
 
 class _builtin_type(object):
     def __getattribute__(self, name):
@@ -751,6 +763,46 @@
         return object.__getattribute__(self, name)
 
 
+
+class _string(_builtin_type):
+
+    def __init__(self, STRING, value = ''):
+        self._str = value
+        self._TYPE = STRING
+
+    def ll_stritem(self, i):
+        # NOT_RPYTHON
+        return self._str[i]
+
+    def ll_stritem_nonneg(self, i):
+        # NOT_RPYTHON
+        assert i >= 0
+        return self._str[i]
+
+    # delegate missing ll_* methods to self._str
+    def __getattr__(self, attr):
+        if attr.startswith('ll_'):
+            return self.wrapper(getattr(self._str, attr[3:]))
+        else:
+            raise AttributeError, attr
+
+    @staticmethod
+    def wrapper(fn):
+        def f(*args, **kwds):
+            res = fn(*args, **kwds)
+            if isinstance(res, str):
+                return make_string(res)
+            elif isinstance(res, list):
+                # it must be a list of strings
+                for i, item in enumerate(res):
+                    res[i] = make_string(item)
+                lst = _list(List(String))
+                lst._list = res
+                return lst
+            else:
+                return res
+        return f
+
 class _list(_builtin_type):
 
     def __init__(self, LIST):
@@ -1004,5 +1056,5 @@
 def hasDictTypes(DICT):
     return DICT._is_initialized()
 
-
+String = String()
 ROOT = Instance('Root', None, _is_root=True)

Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py	Fri May 12 14:29:41 2006
@@ -1,3 +1,4 @@
+from pypy.tool.staticmethods import StaticMethods
 from pypy.rpython.rstr import AbstractStringRepr, \
      AbstractCharRepr, AbstractUniCharRepr, AbstractStringIteratorRepr
 from pypy.rpython.lltypesystem.lltype import Ptr, Char, UniChar
@@ -23,6 +24,10 @@
 
     lowleveltype = String
 
+    def __init__(self, *args):
+        AbstractStringRepr.__init__(self, *args)
+        self.ll = LLHelpers
+
     def convert_const(self, value):
         # XXX what do we do about null strings?
         #if value is None:
@@ -41,6 +46,15 @@
 class UniCharRepr(AbstractUniCharRepr):
     lowleveltype = UniChar
 
+class LLHelpers:
+    __metaclass__ = StaticMethods
+
+    def ll_stritem_nonneg(s, i):
+        return s.ll_stritem_nonneg(i)
+
+    def ll_stritem(s, i):
+        return s.ll_stritem(i)
+
 
 string_repr = StringRepr()
 char_repr = CharRepr()

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py	Fri May 12 14:29:41 2006
@@ -1,8 +1,20 @@
+from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.test.test_llinterp import interpret 
 
 def test_constant_string():
     def f():
         return "foo"
     res = interpret(f, [], type_system="ootype")
-    assert res == "foo"
+    assert res._str == "foo"
 
+def test_builtin_method():
+    s = ootype.make_string('foo bar')
+    assert s.ll_startswith('foo') == True
+    assert s.ll_upper()._str == 'FOO BAR'
+
+def test_split():
+    s = ootype.make_string('foo bar')
+    res = s.ll_split()
+    assert isinstance(res, ootype._list)
+    assert res.ll_getitem_fast(0)._str == 'foo'
+    assert res.ll_getitem_fast(1)._str == 'bar'

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Fri May 12 14:29:41 2006
@@ -22,13 +22,26 @@
         res = LLHelpers.ll_rfind(llstr(s1), llstr(s2), 0, n1)
         assert res == s1.rfind(s2)
 
-def test_simple():
-    def fn(i):
-        s = 'hello'
-        return s[i]
-    for i in range(5):
-        res = interpret(fn, [i])
-        assert res == 'hello'[i]
+
+class AbstractTestRstr:
+
+    def interpret(self, fn, args):
+        return interpret(fn, args, type_system=self.ts)
+
+    def interpret_raises(self, exc, fn, args):
+        return interpret_raises(exc, fn, args, type_system=self.ts)
+
+    def _skip_oo(self, reason):
+        if self.ts == 'ootype':
+            py.test.skip("ootypesystem doesn't support %s, yet" % reason)
+
+    def test_simple(self):
+        def fn(i):
+            s = 'hello'
+            return s[i]
+        for i in range(5):
+            res = self.interpret(fn, [i])
+            assert res == 'hello'[i]
 
 def test_implicit_index_error():
     def fn(i):
@@ -564,3 +577,9 @@
     assert res._obj.value == "ello"
     res = interpret(g, [-2])
     assert res._obj.value == 42
+
+class TestLltype(AbstractTestRstr):
+    ts = "lltype"
+
+class TestOotype(AbstractTestRstr):
+    ts = "ootype"



More information about the Pypy-commit mailing list