[pypy-commit] pypy dynamic-specialized-tuple: added support for unicode. (thanks to amaury for reminding me that py3k needs this to be useful)

alex_gaynor noreply at buildbot.pypy.org
Wed Mar 14 20:45:10 CET 2012


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: dynamic-specialized-tuple
Changeset: r53596:dc2074d97770
Date: 2012-03-14 12:44 -0700
http://bitbucket.org/pypy/pypy/changeset/dc2074d97770/

Log:	added support for unicode. (thanks to amaury for reminding me that
	py3k needs this to be useful)

diff --git a/pypy/objspace/std/test/test_tupleobject.py b/pypy/objspace/std/test/test_tupleobject.py
--- a/pypy/objspace/std/test/test_tupleobject.py
+++ b/pypy/objspace/std/test/test_tupleobject.py
@@ -147,6 +147,10 @@
         t = ("a", "b", "c")
         assert self.get_specialization(t) == "sss"
 
+    def test_unicodes(self):
+        t = (u"a", "b", u"c")
+        assert self.get_specialization(t) == "usu"
+
     def test_mixed(self):
         t = (1, True, "a")
         assert self.get_specialization(t) == "ibs"
diff --git a/pypy/objspace/std/tupletype.py b/pypy/objspace/std/tupletype.py
--- a/pypy/objspace/std/tupletype.py
+++ b/pypy/objspace/std/tupletype.py
@@ -5,7 +5,7 @@
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
 from pypy.rlib.rerased_raw import (UntypedStorage, INT, BOOL, FLOAT, INSTANCE,
-    STRING)
+    STRING, UNICODE)
 from pypy.rlib.unroll import unrolling_iterable
 
 
@@ -46,6 +46,13 @@
 def _get_str(space, storage, idx):
     return space.wrap(storage.getstr(idx))
 
+def _check_unicode(space, w_obj):
+    return space.is_w(space.type(w_obj), space.w_unicode)
+def _store_unicode(space, storage, idx, w_obj):
+    storage.setunicode(idx, space.unicode_w(w_obj))
+def _get_unicode(space, storage, idx):
+    return space.wrap(storage.getunicode(idx))
+
 def _check_instance(space, w_obj):
     return True
 def _store_instance(space, storage, idx, w_obj):
@@ -58,6 +65,7 @@
     (BOOL, _check_bool, _store_bool, _get_bool),
     (FLOAT, _check_float, _store_float, _get_float),
     (STRING, _check_str, _store_str, _get_str),
+    (UNICODE, _check_unicode, _store_unicode, _get_unicode),
     (INSTANCE, _check_instance, _store_instance, _get_instance)
 ])
 
diff --git a/pypy/rlib/rerased_raw.py b/pypy/rlib/rerased_raw.py
--- a/pypy/rlib/rerased_raw.py
+++ b/pypy/rlib/rerased_raw.py
@@ -11,7 +11,7 @@
 from pypy.rpython.rclass import getinstancerepr
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem import rffi, lltype, llmemory
-from pypy.rpython.lltypesystem.rstr import STR, string_repr
+from pypy.rpython.lltypesystem.rstr import STR, string_repr, unicode_repr
 from pypy.rpython.rmodel import Repr
 from pypy.tool.pairtype import pairtype
 
@@ -21,6 +21,7 @@
 FLOAT = "f"
 INSTANCE = "o"
 STRING = "s"
+UNICODE = "u"
 
 class UntypedStorage(object):
     def __init__(self, shape):
@@ -49,6 +50,7 @@
     getbool, setbool = _typed_getset(BOOL, bool)
     getfloat, setfloat = _typed_getset(FLOAT, float)
     getstr, setstr = _typed_getset(STRING, str)
+    getunicode, setunicode = _typed_getset(UNICODE, unicode)
 
     def getinstance(self, idx, cls):
         obj = self.storage[idx]
@@ -133,6 +135,14 @@
         self._check_idx(s_idx)
         assert annmodel.SomeString().contains(s_s)
 
+    def method_getunicode(self, s_idx):
+        self._check_idx(s_idx)
+        return annmodel.SomeUnicodeString()
+
+    def method_setunicode(self, s_idx, s_u):
+        self._check_idx(s_idx)
+        assert annmodel.SomeUnicodeString().contains(s_u)
+
 
 class __extend__(pairtype(SomeUntypedStorage, SomeUntypedStorage)):
     def union((self, other)):
@@ -184,8 +194,8 @@
         char = (shape + llmemory.offsetof(STR, "chars") +
                 llmemory.itemoffsetof(STR.chars, 0) +
                 (llmemory.sizeof(STR.chars.OF) * i)).char[0]
-        # If it's an instance or string then we've found a GC pointer.
-        if char == INSTANCE or char == STRING:
+        # If it's any type of GC pointer, return it.
+        if char == INSTANCE or char == STRING or char == UNICODE:
             return data_ptr
         i += 1
     # If we've gotten to here, there are no GC-pointers left, return NULL to
@@ -295,7 +305,14 @@
 
     def rtype_method_setstr(self, hop):
         v_value = hop.inputarg(string_repr, arg=2)
+        self._write_index_gc(hop, v_value)
 
+    def rtype_method_getunicode(self, hop):
+        v_addr = self._read_index(hop)
+        return hop.genop("cast_adr_to_ptr", [v_addr], resulttype=unicode_repr)
+
+    def rtype_method_setunicode(self, hop):
+        v_value = hop.inputarg(unicode_repr, arg=2)
         self._write_index_gc(hop, v_value)
 
     @classmethod
diff --git a/pypy/rlib/test/test_rerased_raw.py b/pypy/rlib/test/test_rerased_raw.py
--- a/pypy/rlib/test/test_rerased_raw.py
+++ b/pypy/rlib/test/test_rerased_raw.py
@@ -43,6 +43,12 @@
 
         assert storage.getstr(0) == "abc"
 
+    def test_unicode(self):
+        storage= rerased_raw.UntypedStorage("u")
+        storage.setunicode(0, u"abc")
+
+        assert storage.getunicode(0) == u"abc"
+
     def test_getlength(self):
         storage = rerased_raw.UntypedStorage("ooi")
         assert storage.getlength() == 3
@@ -103,6 +109,17 @@
         res = self.interpret(f, [0])
         assert self.ll_to_string(res) == "abc"
 
+    def test_unicode(self):
+        data = [u"abc"]
+        def f(i):
+            storage = rerased_raw.UntypedStorage("u")
+            storage.setunicode(0, data[i])
+            return storage.getunicode(0)
+
+        res = self.interpret(f, [0])
+        assert self.ll_to_string(res) == "abc"
+
+
     def test_exception_catching(self):
         class A(object):
             def __init__(self, v):
diff --git a/pypy/rpython/memory/test/test_transformed_gc.py b/pypy/rpython/memory/test/test_transformed_gc.py
--- a/pypy/rpython/memory/test/test_transformed_gc.py
+++ b/pypy/rpython/memory/test/test_transformed_gc.py
@@ -935,18 +935,20 @@
                 self.v = v
 
         def fn():
-            s = UntypedStorage("ios")
+            s = UntypedStorage("iosu")
             s.setint(0, 10)
             s.setinstance(1, A(10))
             s.setstr(2, str(A(3))[:3])
+            s.setunicode(3, unicode(s.getshape()))
             rgc.collect()
-            return s.getint(0) + s.getinstance(1, A).v + len(s.getstr(2))
+            return (s.getint(0) + s.getinstance(1, A).v + len(s.getstr(2)) +
+                len(s.getunicode(3)))
         return fn
 
     def test_untyped_storage(self):
         run = self.runner("untyped_storage")
         res = run([])
-        assert res == 23
+        assert res == 27
 
     def define_untyped_storage_multiple_objects(cls):
         class A(object):


More information about the pypy-commit mailing list