[pypy-svn] r13564 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Fri Jun 17 23:58:18 CEST 2005


Author: arigo
Date: Fri Jun 17 23:58:16 2005
New Revision: 13564

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rlist.py
Log:
Prebuilt lists.


Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Fri Jun 17 23:58:16 2005
@@ -44,6 +44,7 @@
         else:
             self.item_repr = item_repr
         self.listitem = listitem
+        self.list_cache = {}
         # setup() needs to be called to finish this initialization
 
     def setup(self):
@@ -54,6 +55,22 @@
             ITEMARRAY = GcArray(ITEM)
             self.LIST.become(GcStruct("list", ("items", Ptr(ITEMARRAY))))
 
+    def convert_const(self, listobj):
+        if not isinstance(listobj, list):
+            raise TyperError("expected a list: %r" % (listobj,))
+        try:
+            return self.list_cache[id(listobj)][1]
+        except KeyError:
+            self.setup()
+            result = malloc(self.LIST, immortal=True)
+            self.list_cache[id(listobj)] = listobj, result
+            result.items = malloc(self.LIST.items.TO, len(listobj))
+            r_item = self.item_repr
+            for i in range(len(listobj)):
+                x = listobj[i]
+                result.items[i] = r_item.convert_const(x)
+            return result
+
     def rtype_len(self, hop):
         v_lst, = hop.inputargs(self)
         return hop.gendirectcall(ll_len, v_lst)

Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Fri Jun 17 23:58:16 2005
@@ -4,6 +4,7 @@
 from pypy.rpython.rlist import *
 from pypy.rpython.rslice import ll_newslice
 from pypy.rpython.rint import signed_repr
+from pypy.rpython.test.test_llinterp import interpret
 
 
 def sample_list():
@@ -151,3 +152,14 @@
         del l[-1]
         del l[:]
     rtype(dummyfn)
+
+def test_prebuilt_list():
+    klist = ['a', 'd', 'z', 'k']
+    def dummyfn(n):
+        return klist[n]
+    res = interpret(dummyfn, [0])
+    assert res == 'a'
+    res = interpret(dummyfn, [3])
+    assert res == 'k'
+    res = interpret(dummyfn, [-2])
+    assert res == 'z'



More information about the Pypy-commit mailing list