[pypy-commit] pypy py3.5: Test and fix: the order of BUILD_MAP was wrong

arigo pypy.commits at gmail.com
Fri Oct 14 05:45:08 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r87774:59503d6feb4b
Date: 2016-10-14 11:43 +0200
http://bitbucket.org/pypy/pypy/changeset/59503d6feb4b/

Log:	Test and fix: the order of BUILD_MAP was wrong

diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -1333,10 +1333,11 @@
     @jit.unroll_safe
     def BUILD_MAP(self, itemcount, next_instr):
         w_dict = self.space.newdict()
-        for i in range(itemcount):
-            w_key = self.popvalue()
-            w_value = self.popvalue()
+        for i in range(itemcount-1, -1, -1):
+            w_key = self.peekvalue(2 * i)
+            w_value = self.peekvalue(2 * i + 1)
             self.space.setitem(w_dict, w_key, w_value)
+        self.popvalues(2 * itemcount)
         self.pushvalue(w_dict)
 
     @jit.unroll_safe
diff --git a/pypy/interpreter/test/test_interpreter.py b/pypy/interpreter/test/test_interpreter.py
--- a/pypy/interpreter/test/test_interpreter.py
+++ b/pypy/interpreter/test/test_interpreter.py
@@ -1,6 +1,7 @@
 import py 
 import sys
 from pypy.interpreter import gateway, module, error
+from hypothesis import given, strategies
 
 class TestInterpreter: 
 
@@ -298,7 +299,31 @@
         res = self.codetest(code, 'g', [])
         assert "TypeError:" in res
         assert "'tuple' object is not a mapping" in res
-    
+
+    @given(strategies.lists(strategies.one_of(strategies.none(),
+                                 strategies.lists(strategies.none()))))
+    def test_build_map_order(self, shape):
+        value = [10]
+        def build_expr(shape):
+            if shape is None:
+                value[0] += 1
+                return '0: %d' % value[0]
+            else:
+                return '**{%s}' % (', '.join(
+                    [build_expr(shape1) for shape1 in shape]),)
+
+        expr = build_expr(shape)[2:]
+        code = """
+        def f():
+            return %s
+        """ % (expr, )
+        res = self.codetest(code, 'f', [])
+        if value[0] == 10:
+            expected = {}
+        else:
+            expected = {0: value[0]}
+        assert res == expected, "got %r for %r" % (res, expr)
+
     def test_build_map_unpack_with_call(self):
         code = """
         def f(a,b,c,d):


More information about the pypy-commit mailing list