Commits

Alex Gaynor committed 5716a96

consts with more types

Comments (0)

Files changed (2)

pypy/rlib/rerased_raw.py

 from pypy.annotation import model as annmodel
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.rlib import longlong2float
-from pypy.rpython.annlowlevel import (hlstr, llstr, llhelper,
+from pypy.rpython.annlowlevel import (hlstr, llstr, llunicode, llhelper,
     cast_instance_to_base_ptr)
 from pypy.rpython.rclass import getinstancerepr
 from pypy.rpython.extregistry import ExtRegistryEntry
         for idx, (char, obj) in enumerate(zip(value.shape, value.storage)):
             if char == INT:
                 storage.data[idx] = rffi.cast(llmemory.Address, obj)
+            elif char == BOOL:
+                storage.data[idx] = rffi.cast(llmemory.Address, obj)
+            elif char == FLOAT:
+                storage.data[idx] = rffi.cast(llmemory.Address, longlong2float.float2longlong(obj))
+            elif char == STRING:
+                storage.data[idx] = llmemory.cast_ptr_to_adr(llstr(obj))
+            elif char == UNICODE:
+                storage.data[idx] = llmemory.cast_ptr_to_adr(llunicode(obj))
             elif char == INSTANCE:
                 bk = self.rtyper.annotator.bookkeeper
                 classdef = bk.getuniqueclassdef(type(obj))
         elif elem == INT:
             yield i, rffi.cast(lltype.Signed, storage.data.items[i])
         elif elem == FLOAT:
-            yield i, longlong2float.longlong2float(rffi.cast(lltype.Signed, storage.data.items[i]))
+            yield i, rffi.cast(lltype.Signed, storage.data.items[i])
         elif elem == BOOL:
             yield i, rffi.cast(lltype.Bool, storage.data.items[i])
         else:

pypy/rlib/test/test_rerased_raw.py

 import py
 
-from pypy.rlib import rerased_raw
+from pypy.rlib import rerased_raw, longlong2float
 from pypy.rpython.annlowlevel import hlstr
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin
 
         res = self.interpret(f, [0])
         assert res == 21
 
+    def test_const_types(self):
+        storage = rerased_raw.UntypedStorage("bfsu")
+        storage.setbool(0, True)
+        storage.setfloat(1, 2.5)
+        storage.setstr(2, "hello")
+        storage.setunicode(3, u"world!")
+
+        def f(i):
+            if i:
+                local_storage = rerased_raw.UntypedStorage("o")
+            else:
+                local_storage = storage
+            return (local_storage.getbool(0) + local_storage.getfloat(1) +
+                    len(local_storage.getstr(2)) + len(local_storage.getunicode(3)))
+
+        res = self.interpret(f, [0])
+        assert res == 14.5
+
     def test_enumerate_elements(self):
         def f():
             storage = rerased_raw.UntypedStorage("sibf")
         lst = list(rerased_raw.ll_enumerate_elements(llres))
         assert hlstr(lst[0][1]) == "abc"
         assert lst[0][0] == 0
-        assert lst[1:] == [(1, 13), (2, True), (3, 3.5)]
+        assert lst[1:3] == [(1, 13), (2, True)]
+        assert lst[3][0] == 3
+        assert longlong2float.longlong2float(lst[3][1]) == 3.5
 
 class TestUntypedStorageLLtype(LLRtypeMixin, BaseTestUntypedStorage):
     pass