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

arigo at codespeak.net arigo at codespeak.net
Tue Jun 27 15:03:43 CEST 2006


Author: arigo
Date: Tue Jun 27 15:03:34 2006
New Revision: 29407

Modified:
   pypy/dist/pypy/rpython/callparse.py
   pypy/dist/pypy/rpython/lltypesystem/rtuple.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py
   pypy/dist/pypy/rpython/ootypesystem/rtuple.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/rtuple.py
Log:
In rtuple, renamed the interface "getitem()" to "getitem_internal()" to
make it clear that it returns a value of the internal type-erased repr.
Added a getitem() which returns the externally-expected variable.

The reason is that using the internal repr is a bit dangerous now,
because the root 'object' repr doesn't really handle the objects that
can be tagged pointers.  This caused a crash in string formatting (see
test).



Modified: pypy/dist/pypy/rpython/callparse.py
==============================================================================
--- pypy/dist/pypy/rpython/callparse.py	(original)
+++ pypy/dist/pypy/rpython/callparse.py	Tue Jun 27 15:03:34 2006
@@ -154,7 +154,7 @@
     def _emit(self, repr, hop):
         index = self.index
         r_tup, v_tuple = self.holder.access(hop)
-        v = r_tup.getitem(hop, v_tuple, index)
+        v = r_tup.getitem_internal(hop, v_tuple, index)
         return hop.llops.convertvar(v, r_tup.items_r[index], repr)
 
 # for parsing call arguments

Modified: pypy/dist/pypy/rpython/lltypesystem/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rtuple.py	Tue Jun 27 15:03:34 2006
@@ -56,7 +56,8 @@
             hop.gendirectcall(rlist.ll_setitem_nonneg, v_func, vlist, cindex, vitem)
         return vlist
 
-    def getitem(self, llops, v_tuple, index): # ! returns internal repr lowleveltype
+    def getitem_internal(self, llops, v_tuple, index):
+        """Return the index'th item, in internal repr."""
         name = self.fieldnames[index]
         llresult = self.lltypes[index]
         cname = inputconst(Void, name)

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py	Tue Jun 27 15:03:34 2006
@@ -147,6 +147,18 @@
     res = interpret(fn, [1])
     assert ''.join(res.chars) == '<B object>'
 
+def test_format():
+    def fn(n):
+        if n > 0:
+            x = B(n)
+        else:
+            x = C(n)
+        return '%r' % (x,)
+    res = interpret(fn, [-832])
+    assert ''.join(res.chars) == '<unboxed -832>'
+    res = interpret(fn, [1])
+    assert ''.join(res.chars) == '<B object>'
+
 def test_method():
     def fn(n):
         if n > 0:

Modified: pypy/dist/pypy/rpython/ootypesystem/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rtuple.py	Tue Jun 27 15:03:34 2006
@@ -24,7 +24,8 @@
     def instantiate(self):
         return ootype.new(self.lowleveltype)
 
-    def getitem(self, llops, v_tuple, index): # ! returns internal repr lowleveltype
+    def getitem_internal(self, llops, v_tuple, index):
+        # ! returns internal repr lowleveltype
         name = self.fieldnames[index]
         llresult = self.lltypes[index]
         cname = inputconst(ootype.Void, name)

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Tue Jun 27 15:03:34 2006
@@ -98,7 +98,7 @@
             if not isinstance(r_tuple, AbstractTupleRepr):
                 raise TyperError("*arg must be a tuple")
             for i in range(len(r_tuple.items_r)):
-                v_item = r_tuple.getitem(hop.llops, v_tuple, i)
+                v_item = r_tuple.getitem_internal(hop.llops, v_tuple, i)
                 hop.nb_args += 1
                 hop.args_v.append(v_item)
                 hop.args_s.append(s_tuple.items[i])

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Tue Jun 27 15:03:34 2006
@@ -302,17 +302,10 @@
         r_tuple = hop.args_r[1]
         v_tuple = hop.args_v[1]
 
-        if hop.rtyper.type_system.name == 'ootypesystem':
-            getfield = 'oogetfield'
-        else:
-            getfield = 'getfield'
-
         sourcevars = []
-        for fname, r_arg in zip(r_tuple.fieldnames, r_tuple.items_r):
-            cname = hop.inputconst(Void, fname)
-            vitem = hop.genop(getfield, [v_tuple, cname],
-                              resulttype=r_arg)
-            sourcevars.append((vitem, r_arg))
+        for i, r_arg in enumerate(r_tuple.external_items_r):
+            v_item = r_tuple.getitem(hop.llops, v_tuple, i)
+            sourcevars.append((v_item, r_arg))
 
         return r_str.ll.do_stringformat(hop, sourcevars)
                 

Modified: pypy/dist/pypy/rpython/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/rtuple.py	Tue Jun 27 15:03:34 2006
@@ -88,6 +88,14 @@
         self.lltypes = [r.lowleveltype for r in items_r]
         self.tuple_cache = {}
 
+    def getitem(self, llops, v_tuple, index):
+        """Generate the operations to get the index'th item of v_tuple,
+        in the external repr external_items_r[index]."""
+        v = self.getitem_internal(llops, v_tuple, index)
+        r_item = self.items_r[index]
+        r_external_item = self.external_items_r[index]
+        return llops.convertvar(v, r_item, r_external_item)
+
     def newtuple_cached(cls, hop, items_v):
         r_tuple = hop.r_result
         if hop.s_result.is_constant():
@@ -143,8 +151,7 @@
         if hop.has_implicit_exception(IndexError):
             hop.exception_cannot_occur()
         index = v_index.value
-        v = r_tup.getitem(hop.llops, v_tuple, index)
-        return hop.llops.convertvar(v, r_tup.items_r[index], r_tup.external_items_r[index])
+        return r_tup.getitem(hop.llops, v_tuple, index)
 
 class __extend__(pairtype(AbstractTupleRepr, Repr)): 
     def rtype_contains((r_tup, r_item), hop):
@@ -173,9 +180,9 @@
         v_tuple1, v_tuple2 = hop.inputargs(r_tup1, r_tup2)
         vlist = []
         for i in range(len(r_tup1.items_r)):
-            vlist.append(r_tup1.getitem(hop.llops, v_tuple1, i))
+            vlist.append(r_tup1.getitem_internal(hop.llops, v_tuple1, i))
         for i in range(len(r_tup2.items_r)):
-            vlist.append(r_tup2.getitem(hop.llops, v_tuple2, i))
+            vlist.append(r_tup2.getitem_internal(hop.llops, v_tuple2, i))
         return r_tup1.newtuple_cached(hop, vlist)
     rtype_inplace_add = rtype_add
 
@@ -186,7 +193,7 @@
             n = len(r_from.items_r)
             items_v = []
             for i in range(n):
-                item_v = r_from.getitem(llops, v, i)
+                item_v = r_from.getitem_internal(llops, v, i)
                 item_v = llops.convertvar(item_v,
                                               r_from.items_r[i],
                                               r_to.items_r[i])



More information about the Pypy-commit mailing list