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

nik at codespeak.net nik at codespeak.net
Sat Apr 29 06:14:49 CEST 2006


Author: nik
Date: Sat Apr 29 06:14:44 2006
New Revision: 26551

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rstr.py
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rstr.py
Log:
pasing test for string constants in ootypesystem.


Modified: pypy/dist/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rstr.py	Sat Apr 29 06:14:44 2006
@@ -1,6 +1,9 @@
+from weakref import WeakValueDictionary
 from pypy.rpython.rstr import AbstractStringRepr, char_repr, STR, AbstractStringIteratorRepr, \
-        ll_strconcat, do_stringformat
-from pypy.rpython.lltypesystem.lltype import malloc, GcStruct, Ptr, Signed
+        ll_strconcat, do_stringformat, ll_strhash
+from pypy.rpython.lltypesystem.lltype import malloc, GcStruct, Ptr, nullptr, Signed
+
+CONST_STR_CACHE = WeakValueDictionary()
 
 class StringRepr(AbstractStringRepr):
 
@@ -13,6 +16,22 @@
         self.ll_lower = ll_lower
         self.ll_join = ll_join
 
+    def convert_const(self, value):
+        if value is None:
+            return nullptr(STR)
+        #value = getattr(value, '__self__', value)  # for bound string methods
+        if not isinstance(value, str):
+            raise TyperError("not a str: %r" % (value,))
+        try:
+            return CONST_STR_CACHE[value]
+        except KeyError:
+            p = malloc(STR, len(value))
+            for i in range(len(value)):
+                p.chars[i] = value[i]
+            ll_strhash(p)   # precompute the hash
+            CONST_STR_CACHE[value] = p
+            return p
+
     def make_iterator_repr(self):
         return string_iterator_repr
 

Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Sat Apr 29 06:14:44 2006
@@ -181,6 +181,13 @@
         StaticMethod.__init__(self, args, result)
 
 
+class String(OOType):
+
+    def _defl(self):
+        return ""
+
+String = String()
+
 class BuiltinType(OOType):
 
     def _example(self):
@@ -642,6 +649,10 @@
 else:
     instance_impl = _instance
 
+def make_string(value):
+    assert isinstance(value, str)
+    return _string(value)
+
 def make_instance(INSTANCE):
     inst = _instance(INSTANCE)
     if STATICNESS:
@@ -724,6 +735,9 @@
        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):
         TYPE = object.__getattribute__(self, "_TYPE")

Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py	Sat Apr 29 06:14:44 2006
@@ -1,10 +1,18 @@
 from pypy.rpython.rstr import AbstractStringRepr, STR, AbstractStringIteratorRepr
 from pypy.rpython.lltypesystem.lltype import Ptr
-from pypy.rpython.ootypesystem.ootype import Signed, Record
+from pypy.rpython.ootypesystem.ootype import Signed, Record, String, make_string
 
 class StringRepr(AbstractStringRepr):
 
-    lowleveltype = Ptr(STR)
+    lowleveltype = String
+
+    def convert_const(self, value):
+        # XXX what do we do about null strings?
+        #if value is None:
+        #    return nullptr(STR)
+        if not isinstance(value, str):
+            raise TyperError("not a str: %r" % (value,))
+        return make_string(value)
 
     def make_iterator_repr(self):
         return string_iterator_repr



More information about the Pypy-commit mailing list