[pypy-commit] pypy translation-cleanup: Simplify FrameState constructor

rlamy noreply at buildbot.pypy.org
Fri Aug 10 10:03:26 CEST 2012


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: translation-cleanup
Changeset: r56673:a86581ff1066
Date: 2012-08-08 02:49 +0100
http://bitbucket.org/pypy/pypy/changeset/a86581ff1066/

Log:	Simplify FrameState constructor

	Use frame.getstate() to save the state of the frame instead of the
	ctor.

diff --git a/pypy/objspace/flow/flowcontext.py b/pypy/objspace/flow/flowcontext.py
--- a/pypy/objspace/flow/flowcontext.py
+++ b/pypy/objspace/flow/flowcontext.py
@@ -8,7 +8,8 @@
 from pypy.interpreter.pycode import PyCode, cpython_code_signature
 from pypy.objspace.flow import operation
 from pypy.objspace.flow.model import *
-from pypy.objspace.flow.framestate import FrameState, recursively_unflatten
+from pypy.objspace.flow.framestate import (FrameState, recursively_unflatten,
+        recursively_flatten)
 from pypy.rlib import jit
 from pypy.tool.stdlib_opcode import host_bytecode_spec
 
@@ -112,7 +113,7 @@
             # the same block.  We will continue, to figure out where the next
             # such operation *would* appear, and we make a join point just
             # before.
-            self.last_join_point = FrameState(frame)
+            self.last_join_point = frame.getstate()
 
     def guessbool(self, ec, w_condition, cases=[False,True],
                   replace_last_variable_except_in_first_case = None):
@@ -208,7 +209,7 @@
             arg_list[position] = Constant(value)
         frame.setfastscope(arg_list)
         self.joinpoints = {}
-        initialblock = SpamBlock(FrameState(frame).copy())
+        initialblock = SpamBlock(frame.getstate().copy())
         self.pendingblocks = collections.deque([initialblock])
 
         # CallableFactory.pycall may add class_ to functions that are methods
@@ -437,6 +438,21 @@
 
 class FlowSpaceFrame(pyframe.CPythonFrame):
 
+    def getstate(self):
+        # getfastscope() can return real None, for undefined locals
+        data = self.save_locals_stack()
+        if self.last_exception is None:
+            data.append(Constant(None))
+            data.append(Constant(None))
+        else:
+            data.append(self.last_exception.w_type)
+            data.append(self.last_exception.get_w_value(self.space))
+        recursively_flatten(self.space, data)
+        nonmergeable = (self.get_blocklist(),
+            self.last_instr,   # == next_instr when between bytecodes
+            self.w_locals,)
+        return FrameState(data, nonmergeable)
+
     def setstate(self, state):
         """ Reset the frame to the given state. """
         data = state.mergeable[:]
diff --git a/pypy/objspace/flow/framestate.py b/pypy/objspace/flow/framestate.py
--- a/pypy/objspace/flow/framestate.py
+++ b/pypy/objspace/flow/framestate.py
@@ -1,34 +1,11 @@
-from pypy.interpreter.pyframe import PyFrame
 from pypy.interpreter.pyopcode import SuspendedUnroller
-from pypy.interpreter.error import OperationError
 from pypy.rlib.unroll import SpecTag
 from pypy.objspace.flow.model import *
 
 class FrameState:
-    # XXX this class depends on the internal state of PyFrame objects
-
-    def __init__(self, state):
-        if isinstance(state, PyFrame):
-            # getfastscope() can return real None, for undefined locals
-            data = state.save_locals_stack()
-            if state.last_exception is None:
-                data.append(Constant(None))
-                data.append(Constant(None))
-            else:
-                data.append(state.last_exception.w_type)
-                data.append(state.last_exception.get_w_value(state.space))
-            recursively_flatten(state.space, data)
-            self.mergeable = data
-            self.nonmergeable = (
-                state.get_blocklist(),
-                state.last_instr,   # == next_instr when between bytecodes
-                state.w_locals,
-            )
-        elif isinstance(state, tuple):
-            self.mergeable, self.nonmergeable = state
-        else:
-            raise TypeError("can't get framestate for %r" %
-                            state.__class__.__name__)
+    def __init__(self, mergeable, nonmergeable):
+        self.mergeable = mergeable
+        self.nonmergeable = nonmergeable
         self.next_instr = self.nonmergeable[1]
         for w1 in self.mergeable:
             assert isinstance(w1, (Variable, Constant)) or w1 is None, (
@@ -41,7 +18,7 @@
             if isinstance(w, Variable):
                 w = Variable()
             newstate.append(w)
-        return FrameState((newstate, self.nonmergeable))
+        return FrameState(newstate, self.nonmergeable)
 
     def getvariables(self):
         return [w for w in self.mergeable if isinstance(w, Variable)]
@@ -74,7 +51,7 @@
                 newstate.append(union(w1, w2))
         except UnionError:
             return None
-        return FrameState((newstate, self.nonmergeable))
+        return FrameState(newstate, self.nonmergeable)
 
     def getoutputargs(self, targetstate):
         "Return the output arguments needed to link self to targetstate."
diff --git a/pypy/objspace/flow/test/test_framestate.py b/pypy/objspace/flow/test/test_framestate.py
--- a/pypy/objspace/flow/test/test_framestate.py
+++ b/pypy/objspace/flow/test/test_framestate.py
@@ -1,9 +1,7 @@
-
-
 from py.test import raises
 from pypy.objspace.flow.model import *
-from pypy.objspace.flow.framestate import *
 from pypy.interpreter.pycode import PyCode
+from pypy.rlib.unroll import SpecTag
 from pypy.objspace.flow.objspace import FlowObjSpace
 
 class TestFrameState:
@@ -35,55 +33,55 @@
 
     def test_eq_framestate(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
-        fs2 = FrameState(frame)
+        fs1 = frame.getstate()
+        fs2 = frame.getstate()
         assert fs1 == fs2
 
     def test_neq_hacked_framestate(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Variable()
-        fs2 = FrameState(frame)
+        fs2 = frame.getstate()
         assert fs1 != fs2
 
     def test_union_on_equal_framestates(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
-        fs2 = FrameState(frame)
+        fs1 = frame.getstate()
+        fs2 = frame.getstate()
         assert fs1.union(fs2) == fs1
 
     def test_union_on_hacked_framestates(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Variable()
-        fs2 = FrameState(frame)
+        fs2 = frame.getstate()
         assert fs1.union(fs2) == fs2  # fs2 is more general
         assert fs2.union(fs1) == fs2  # fs2 is more general
 
     def test_restore_frame(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Variable()
         frame.setstate(fs1)
-        assert fs1 == FrameState(frame)
+        assert fs1 == frame.getstate()
 
     def test_copy(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         fs2 = fs1.copy()
         assert fs1 == fs2
 
     def test_getvariables(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         vars = fs1.getvariables()
         assert len(vars) == 1
 
     def test_getoutputargs(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Variable()
-        fs2 = FrameState(frame)
+        fs2 = frame.getstate()
         outputargs = fs1.getoutputargs(fs2)
         # 'x' -> 'x' is a Variable
         # locals_w[n-1] -> locals_w[n-1] is Constant(None)
@@ -91,9 +89,9 @@
 
     def test_union_different_constants(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Constant(42)
-        fs2 = FrameState(frame)
+        fs2 = frame.getstate()
         fs3 = fs1.union(fs2)
         frame.setstate(fs3)
         assert isinstance(frame.locals_stack_w[frame.pycode.co_nlocals-1],
@@ -101,7 +99,7 @@
 
     def test_union_spectag(self):
         frame = self.getframe(self.func_simple)
-        fs1 = FrameState(frame)
+        fs1 = frame.getstate()
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Constant(SpecTag())
-        fs2 = FrameState(frame)
+        fs2 = frame.getstate()
         assert fs1.union(fs2) is None   # UnionError


More information about the pypy-commit mailing list