[pypy-commit] pypy py3k: (arigo, romain, antocuni) aaargh. enumerate did not convert the repr of the items, resulting in a lot of confusion if you enumerate a list of instances, leading to incorrect C code. Fix it, and add a sanity check inside rtuple.newtuple

antocuni noreply at buildbot.pypy.org
Wed Jan 18 20:05:00 CET 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r51460:8df1c2074547
Date: 2012-01-18 20:03 +0100
http://bitbucket.org/pypy/pypy/changeset/8df1c2074547/

Log:	(arigo, romain, antocuni) aaargh. enumerate did not convert the repr
	of the items, resulting in a lot of confusion if you enumerate a
	list of instances, leading to incorrect C code. Fix it, and add a
	sanity check inside rtuple.newtuple

diff --git a/pypy/rpython/lltypesystem/rtuple.py b/pypy/rpython/lltypesystem/rtuple.py
--- a/pypy/rpython/lltypesystem/rtuple.py
+++ b/pypy/rpython/lltypesystem/rtuple.py
@@ -27,6 +27,10 @@
 
     def newtuple(cls, llops, r_tuple, items_v):
         # items_v should have the lowleveltype of the internal reprs
+        assert len(r_tuple.items_r) == len(items_v)
+        for r_item, v_item in zip(r_tuple.items_r, items_v):
+            assert r_item.lowleveltype == v_item.concretetype
+        #
         if len(r_tuple.items_r) == 0:
             return inputconst(Void, ())    # a Void empty tuple
         c1 = inputconst(Void, r_tuple.lowleveltype.TO)
diff --git a/pypy/rpython/rrange.py b/pypy/rpython/rrange.py
--- a/pypy/rpython/rrange.py
+++ b/pypy/rpython/rrange.py
@@ -204,7 +204,10 @@
         v_index = hop.gendirectcall(self.ll_getnextindex, v_enumerate)
         hop2 = hop.copy()
         hop2.args_r = [self.r_baseiter]
+        r_item_src = self.r_baseiter.r_list.external_item_repr
+        r_item_dst = hop.r_result.items_r[1]
         v_item = self.r_baseiter.rtype_next(hop2)
+        v_item = hop.llops.convertvar(v_item, r_item_src, r_item_dst)
         return hop.r_result.newtuple(hop.llops, hop.r_result,
                                      [v_index, v_item])
 
diff --git a/pypy/rpython/test/test_rrange.py b/pypy/rpython/test/test_rrange.py
--- a/pypy/rpython/test/test_rrange.py
+++ b/pypy/rpython/test/test_rrange.py
@@ -169,6 +169,22 @@
         res = self.interpret(fn, [2])
         assert res == 789
 
+    def test_enumerate_instances(self):
+        class A:
+            pass
+        def fn(n):
+            a = A()
+            b = A()
+            a.k = 10
+            b.k = 20
+            for i, x in enumerate([a, b]):
+                if i == n:
+                    return x.k
+            return 5
+        res = self.interpret(fn, [1])
+        assert res == 20
+
+
 
 class TestLLtype(BaseTestRrange, LLRtypeMixin):
     from pypy.rpython.lltypesystem import rrange 


More information about the pypy-commit mailing list