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

fijal at codespeak.net fijal at codespeak.net
Mon Nov 5 11:26:41 CET 2007


Author: fijal
Date: Mon Nov  5 11:26:41 2007
New Revision: 48312

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rstr.py
   pypy/dist/pypy/rpython/ootypesystem/rstr.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/test/test_llinterp.py
Log:
Some unicode support (constants & getitem notably). More support needs fixes
here and there.


Modified: pypy/dist/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rstr.py	Mon Nov  5 11:26:41 2007
@@ -12,7 +12,7 @@
 from pypy.rpython.lltypesystem import ll_str
 from pypy.rpython.lltypesystem.lltype import \
      GcStruct, Signed, Array, Char, UniChar, Ptr, malloc, \
-     Bool, Void, GcArray, nullptr, pyobjectptr
+     Bool, Void, GcArray, nullptr, pyobjectptr, cast_primitive
 
 
 # ____________________________________________________________
@@ -27,29 +27,28 @@
 STR = GcStruct('rpy_string', ('hash',  Signed),
                              ('chars', Array(Char, hints={'immutable': True,
                                                           'isrpystring': True})))
+UNICODE = GcStruct('rpy_unicode', ('hash', Signed),
+                   ('chars', Array(UniChar, hints={'immutable': True})))
 SIGNED_ARRAY = GcArray(Signed)
 CONST_STR_CACHE = WeakValueDictionary()
 
-class StringRepr(AbstractStringRepr):
-
-    lowleveltype = Ptr(STR)
-
+class BaseStringRepr(AbstractStringRepr):
     def __init__(self, *args):
         AbstractStringRepr.__init__(self, *args)
         self.ll = LLHelpers
 
     def convert_const(self, value):
         if value is None:
-            return nullptr(STR)
+            return nullptr(self.lowleveltype.TO)
         #value = getattr(value, '__self__', value)  # for bound string methods
-        if not isinstance(value, str):
+        if not isinstance(value, self.basetype):
             raise TyperError("not a str: %r" % (value,))
         try:
             return CONST_STR_CACHE[value]
         except KeyError:
-            p = mallocstr(len(value))
+            p = self.malloc(len(value))
             for i in range(len(value)):
-                p.chars[i] = value[i]
+                p.chars[i] = cast_primitive(self.base, value[i])
             p.hash = 0
             self.ll.ll_strhash(p)   # precompute the hash
             CONST_STR_CACHE[value] = p
@@ -72,6 +71,23 @@
         v_items = hop.gendirectcall(LIST.ll_items, v_lst)
         return v_length, v_items
 
+class StringRepr(BaseStringRepr):
+    lowleveltype = Ptr(STR)
+    basetype = str
+    base = Char
+
+    def __init__(self, *args):
+        BaseStringRepr.__init__(self, *args)
+        self.malloc = mallocstr
+    
+class UnicodeRepr(BaseStringRepr):
+    lowleveltype = Ptr(UNICODE)
+    basetype = basestring
+    base = UniChar
+
+    def __init__(self, *args):
+        BaseStringRepr.__init__(self, *args)
+        self.malloc = mallocunicode
 
 class CharRepr(AbstractCharRepr, StringRepr):
     lowleveltype = Char
@@ -79,7 +95,6 @@
 class UniCharRepr(AbstractUniCharRepr):
     lowleveltype = UniChar
 
-
 class __extend__(pairtype(PyObjRepr, AbstractStringRepr)):
     def convert_from_to((r_from, r_to), v, llops):
         v_len = llops.gencapicall('PyString_Size', [v], resulttype=Signed)
@@ -104,13 +119,18 @@
                                  resulttype=pyobj_repr,
                                  _callable= lambda v: pyobjectptr(''.join(v.chars)))
 
-def mallocstr(length):
-    debug_assert(length >= 0, "negative string length")
-    r = malloc(STR, length)
-    if not we_are_translated() or not malloc_zero_filled:
-        r.hash = 0
-    return r
-mallocstr._annspecialcase_ = 'specialize:semierased'
+def new_malloc(TP):
+    def mallocstr(length):
+        debug_assert(length >= 0, "negative string length")
+        r = malloc(TP, length)
+        if not we_are_translated() or not malloc_zero_filled:
+            r.hash = 0
+        return r
+    mallocstr._annspecialcase_ = 'specialize:semierased'
+    return mallocstr
+
+mallocstr = new_malloc(STR)
+mallocunicode = new_malloc(UNICODE)
 
 # ____________________________________________________________
 #
@@ -760,8 +780,11 @@
 unichar_repr = UniCharRepr()
 char_repr.ll = LLHelpers
 unichar_repr.ll = LLHelpers
+unicode_repr = UnicodeRepr()
 emptystr = string_repr.convert_const("")
 
+StringRepr.repr = string_repr
+UnicodeRepr.repr = unicode_repr
 
 class StringIteratorRepr(AbstractStringIteratorRepr):
 

Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py	Mon Nov  5 11:26:41 2007
@@ -300,6 +300,7 @@
 char_repr.ll = LLHelpers
 unichar_repr.ll = LLHelpers
 emptystr = string_repr.convert_const("")
+StringRepr.repr = string_repr
 
 class StringIteratorRepr(AbstractStringIteratorRepr):
     lowleveltype = ootype.Record({'string': string_repr.lowleveltype,

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Mon Nov  5 11:26:41 2007
@@ -7,7 +7,8 @@
 from pypy.rpython.rtuple import AbstractTupleRepr
 from pypy.rpython.rslice import AbstractSliceRepr
 from pypy.rpython import rint
-from pypy.rpython.lltypesystem.lltype import Signed, Bool, Void, UniChar
+from pypy.rpython.lltypesystem.lltype import Signed, Bool, Void, UniChar,\
+     cast_primitive
 
 class AbstractStringRepr(Repr):
     pass
@@ -18,13 +19,19 @@
 class AbstractUniCharRepr(Repr):
     pass
 
-
 class __extend__(annmodel.SomeString):
     def rtyper_makerepr(self, rtyper):
         return rtyper.type_system.rstr.string_repr
     def rtyper_makekey(self):
         return self.__class__,
 
+class __extend__(annmodel.SomeUnicodeString):
+    def rtyper_makerepr(self, rtyper):
+        return rtyper.type_system.rstr.unicode_repr
+    
+    def rtyper_makekey(self):
+        return self.__class__,
+
 class __extend__(annmodel.SomeChar):
     def rtyper_makerepr(self, rtyper):
         return rtyper.type_system.rstr.char_repr
@@ -50,7 +57,7 @@
         return self.ll.ll_strfasthash
 
     def rtype_len(self, hop):
-        string_repr = hop.rtyper.type_system.rstr.string_repr
+        string_repr = self.repr
         v_str, = hop.inputargs(string_repr)
         return hop.gendirectcall(self.ll.ll_strlen, v_str)
 
@@ -244,7 +251,7 @@
 
 class __extend__(pairtype(AbstractStringRepr, IntegerRepr)):
     def rtype_getitem((r_str, r_int), hop, checkidx=False):
-        string_repr = hop.rtyper.type_system.rstr.string_repr
+        string_repr = r_str.repr
         v_str, v_index = hop.inputargs(string_repr, Signed)
         if checkidx:
             if hop.args_s[1].nonneg:

Modified: pypy/dist/pypy/rpython/test/test_llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_llinterp.py	(original)
+++ pypy/dist/pypy/rpython/test/test_llinterp.py	Mon Nov  5 11:26:41 2007
@@ -257,12 +257,12 @@
     res = interpret(f, [3])
     assert res == 3
 
-def test_unicode():
-    def f():
-        return u'Hello world'
-    res = interpret(f, [], someobjects=True)
+##def test_unicode():
+##    def f():
+##        return u'Hello world'
+##    res = interpret(f, [], someobjects=True)
     
-    assert res._obj.value == u'Hello world'
+##    assert res._obj.value == u'Hello world'
     
 ##def test_unicode_split():
 ##    def f():



More information about the Pypy-commit mailing list