Author: Philip Jenvey <[email protected]>
Branch: 
Changeset: r65726:330436db9f45
Date: 2013-07-27 15:53 -0700
http://bitbucket.org/pypy/pypy/changeset/330436db9f45/

Log:    have BUILD_LIST_FROM_ARG utilize length_hint

diff --git a/pypy/interpreter/astcompiler/test/test_compiler.py 
b/pypy/interpreter/astcompiler/test/test_compiler.py
--- a/pypy/interpreter/astcompiler/test/test_compiler.py
+++ b/pypy/interpreter/astcompiler/test/test_compiler.py
@@ -896,6 +896,21 @@
             assert False, (3,)
         except AssertionError, e:
             assert str(e) == "(3,)"
+
+    # BUILD_LIST_FROM_ARG is PyPy specific
+    @py.test.mark.skipif('config.option.runappdirect')
+    def test_build_list_from_arg_length_hint(self):
+        hint_called = [False]
+        class Foo(object):
+            def __length_hint__(self):
+                hint_called[0] = True
+                return 5
+            def __iter__(self):
+                for i in range(5):
+                    yield i
+        l = [a for a in Foo()]
+        assert hint_called[0]
+        assert l == list(range(5))
         
 
 class TestOptimizations:
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -707,16 +707,17 @@
         self.pushvalue(w_list)
 
     def BUILD_LIST_FROM_ARG(self, _, next_instr):
+        space = self.space
         # this is a little dance, because list has to be before the
         # value
         last_val = self.popvalue()
+        length_hint = 0
         try:
-            lgt = self.space.len_w(last_val)
-        except OperationError, e:
-            if e.async(self.space):
+            length_hint = space.length_hint(last_val, length_hint)
+        except OperationError as e:
+            if e.async(space):
                 raise
-            lgt = 0 # oh well
-        self.pushvalue(self.space.newlist([], sizehint=lgt))
+        self.pushvalue(space.newlist([], sizehint=length_hint))
         self.pushvalue(last_val)
 
     def LOAD_ATTR(self, nameindex, next_instr):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to