1. cbjadwani
  2. pypy

Commits

Ronan Lamy  committed 08e6580 Draft

Replace FrameState.restoreframe() with FlowSpaceFrame.setstate()

  • Participants
  • Parent commits ecf2712
  • Branches translation-cleanup

Comments (0)

Files changed (3)

File pypy/objspace/flow/flowcontext.py

View file
 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
+from pypy.objspace.flow.framestate import FrameState, recursively_unflatten
 from pypy.rlib import jit
 from pypy.tool.stdlib_opcode import host_bytecode_spec
 
     def patchframe(self, frame):
         if self.dead:
             raise StopFlowing
-        self.framestate.restoreframe(frame)
+        frame.setstate(self.framestate)
         return BlockRecorder(self)
 
 
 
 class FlowSpaceFrame(pyframe.CPythonFrame):
 
+    def setstate(self, state):
+        """ Reset the frame to the given state. """
+        data = state.mergeable[:]
+        recursively_unflatten(self.space, data)
+        self.restore_locals_stack(data[:-2])  # Nones == undefined locals
+        if data[-2] == Constant(None):
+            assert data[-1] == Constant(None)
+            self.last_exception = None
+        else:
+            self.last_exception = OperationError(data[-2], data[-1])
+        blocklist, self.last_instr, self.w_locals = state.nonmergeable
+        self.set_blocklist(blocklist)
+
     def SETUP_WITH(self, offsettoend, next_instr):
         # A simpler version than the 'real' 2.7 one:
         # directly call manager.__enter__(), don't use special lookup functions

File pypy/objspace/flow/framestate.py

View file
         elif isinstance(state, tuple):
             self.mergeable, self.nonmergeable = state
         else:
-            raise TypeError("can't get framestate for %r" % 
+            raise TypeError("can't get framestate for %r" %
                             state.__class__.__name__)
         self.next_instr = self.nonmergeable[1]
         for w1 in self.mergeable:
             assert isinstance(w1, (Variable, Constant)) or w1 is None, (
                 '%r found in frame state' % w1)
 
-    def restoreframe(self, frame):
-        if isinstance(frame, PyFrame):
-            data = self.mergeable[:]
-            recursively_unflatten(frame.space, data)
-            frame.restore_locals_stack(data[:-2])  # Nones == undefined locals
-            if data[-2] == Constant(None):
-                assert data[-1] == Constant(None)
-                frame.last_exception = None
-            else:
-                frame.last_exception = OperationError(data[-2], data[-1])
-            (
-                blocklist,
-                frame.last_instr,
-                frame.w_locals,
-            ) = self.nonmergeable
-            frame.set_blocklist(blocklist)
-        else:
-            raise TypeError("can't set framestate for %r" % 
-                            frame.__class__.__name__)
-
     def copy(self):
         "Make a copy of this state in which all Variables are fresh."
         newstate = []

File pypy/objspace/flow/test/test_framestate.py

View file
 
 class TestFrameState:
     def setup_class(cls):
-        cls.space = FlowObjSpace() 
+        cls.space = FlowObjSpace()
 
     def getframe(self, func):
         space = self.space
         frame = self.getframe(self.func_simple)
         fs1 = FrameState(frame)
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Variable()
-        fs1.restoreframe(frame)
+        frame.setstate(fs1)
         assert fs1 == FrameState(frame)
 
     def test_copy(self):
         frame = self.getframe(self.func_simple)
         fs1 = FrameState(frame)
         vars = fs1.getvariables()
-        assert len(vars) == 1 
+        assert len(vars) == 1
 
     def test_getoutputargs(self):
         frame = self.getframe(self.func_simple)
         frame.locals_stack_w[frame.pycode.co_nlocals-1] = Constant(42)
         fs2 = FrameState(frame)
         fs3 = fs1.union(fs2)
-        fs3.restoreframe(frame)
+        frame.setstate(fs3)
         assert isinstance(frame.locals_stack_w[frame.pycode.co_nlocals-1],
                           Variable)   # generalized