Author: Carl Friedrich Bolz <[email protected]>
Branch: faster-set-of-iterator
Changeset: r65398:4b0e328a1aa0
Date: 2013-07-11 19:58 +0200
http://bitbucket.org/pypy/pypy/changeset/4b0e328a1aa0/

Log:    test tracing of unpacking independently of the rest of pypy. fix
        some of the bugs discovered while doing that.

diff --git a/pypy/interpreter/unpack.py b/pypy/interpreter/unpack.py
--- a/pypy/interpreter/unpack.py
+++ b/pypy/interpreter/unpack.py
@@ -26,33 +26,37 @@
 
 class FixedSizeUnpackTarget(UnpackTarget):
     def __init__(self, space, expected_size):
+        self.space = space
         self.items_w = [None] * expected_size
         self.index = 0
 
     def append(self, w_obj):
         if self.index == len(self.items_w):
-            raise OperationError(self.w_ValueError,
-                                self.wrap("too many values to unpack"))
-        self.items_w[self.index] = w_item
+            raise OperationError(self.space.w_ValueError,
+                                self.space.wrap("too many values to unpack"))
+        self.items_w[self.index] = w_obj
         self.index += 1
 
 
 
 unpack_into_driver = jit.JitDriver(name='unpack_into',
-                                   greens=['unroll', 'w_type'],
+                                   greens=['unroll', 'unpackcls', 'w_type'],
                                    reds=['unpack_target', 'w_iterator'])
 
 def generic_unpack_into(w_iterable, space, unpack_target, unroll=False):
     w_iterator = space.iter(w_iterable)
     w_type = space.type(w_iterator)
+    unpackcls = type(unpack_target)
     while True:
         if not unroll:
             unpack_into_driver.can_enter_jit(w_type=w_type, unroll=unroll,
                                              w_iterator=w_iterator,
-                                             unpack_target=unpack_target)
+                                             unpack_target=unpack_target,
+                                             unpackcls=unpackcls)
         unpack_into_driver.jit_merge_point(w_type=w_type, unroll=unroll,
                                            w_iterator=w_iterator,
-                                           unpack_target=unpack_target)
+                                           unpack_target=unpack_target,
+                                           unpackcls=unpackcls)
         try:
             w_item = space.next(w_iterator)
         except OperationError, e:
diff --git a/pypy/module/pypyjit/test/test_unpacktracing.py 
b/pypy/module/pypyjit/test/test_unpacktracing.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/pypyjit/test/test_unpacktracing.py
@@ -0,0 +1,107 @@
+from rpython.jit.metainterp.test.support import LLJitMixin
+from rpython.rlib import jit
+
+from pypy.interpreter import unpack, error
+
+class W_Root(object):
+    type = None
+
+    from pypy.interpreter.unpack import generic_unpack_into as unpack_into
+
+    def next(self):
+        raise NotImplementedError
+    def iter(self):
+        raise NotImplementedError
+
+class W_Type(W_Root):
+    pass
+W_Type.type = W_Type()
+
+class W_List(W_Root):
+    type = W_Type()
+    def __init__(self, l):
+        self.l = l
+
+    def iter(self):
+        return W_Iter(self.l)
+
+class W_Iter(W_Root):
+    type = W_Type()
+    def __init__(self, l):
+        self.l = l
+        self.i = 0
+
+    def next(self):
+        i = self.i
+        if i >= len(self.l):
+            raise error.OperationError(StopIteration, None)
+        self.i += 1
+        return self.l[i]
+
+class W_Int(W_Root):
+    type = W_Type()
+
+    def __init__(self, value):
+        self.value = value
+
+class W_String(W_Root):
+    type = W_Type()
+
+    def __init__(self, value):
+        self.value = value
+
+class FakeSpace(object):
+    w_StopIteration = StopIteration
+    w_ValueError = ValueError
+
+    def iter(self, w_obj):
+        return w_obj.iter()
+
+    def next(self, w_obj):
+        return w_obj.next()
+
+    def type(self, w_obj):
+        return w_obj.type
+
+    def length_hint(self, w_obj, x):
+        return 7
+
+    def exception_match(self, w_obj, w_cls):
+        return w_obj is w_cls
+
+    def wrap(self, string):
+        return W_String(string)
+
+    def _freeze_(self):
+        return True
+
+space = FakeSpace()
+
+
+class TestUnpackJIT(LLJitMixin):
+    def test_jit_unpack(self):
+        def f(i):
+            l = [W_Int(x) for x in range(100 + i)]
+            l.append(W_Int(i))
+
+            w_l = W_List(l)
+            res = 0
+            target = unpack.FixedSizeUnpackTarget(space, len(l))
+            if i < 0:
+                w_l.unpack_into(space, target, unroll=True)
+            else:
+                w_l.unpack_into(space, target)
+            res += len(target.items_w)
+            target = unpack.InterpListUnpackTarget(space, w_l)
+            w_l.unpack_into(space, target)
+            return len(target.items_w) + res
+        assert f(4) == 210
+
+        # hack
+        from rpython.jit.metainterp import compile
+        class A(object):
+            view = viewloops = False
+        compile.option = A()
+        result = self.meta_interp(f, [4], listops=True, backendopt=True, 
listcomp=True)
+        assert result == 210
+        self.check_trace_count(2)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to