Commits

Ronan Lamy committed c456e79

Implement FlowSpaceFrame.cells initialisation in HostCode

  • Participants
  • Parent commits ff0274f
  • Branches translation-cleanup

Comments (0)

Files changed (2)

pypy/objspace/flow/bytecode.py

         cpython_code_signature)
 from pypy.tool.stdlib_opcode import (host_bytecode_spec, EXTENDED_ARG,
         HAVE_ARGUMENT)
-from pypy.interpreter.astcompiler.consts import CO_GENERATOR
+from pypy.interpreter.astcompiler.consts import CO_GENERATOR, CO_NEWLOCALS
+from pypy.interpreter.nestedscope import Cell
+from pypy.objspace.flow.model import Constant
 
 class HostCode(PyCode):
     """
                             self._args_as_cellvars.append(-1)   # pad
                         self._args_as_cellvars[i] = j
 
+    def make_cells(self, closure):
+        """Convert a Python closure object into a list of Cells"""
+        if closure is not None:
+            closure = [Cell(Constant(c.cell_contents)) for c in closure]
+        else:
+            closure = []
+        if not (self.co_flags & CO_NEWLOCALS):
+            raise ValueError("The code object for a function should have "
+                    "the flag CO_NEWLOCALS set.")
+        if len(closure) != len(self.co_freevars):
+            raise ValueError("code object received a closure with "
+                                 "an unexpected number of free variables")
+        return [Cell() for _ in self.co_cellvars] + closure
+
+
     def read(self, pos):
         """
         Decode the instruction starting at position ``next_instr``.

pypy/objspace/flow/flowcontext.py

 from pypy.tool.error import source_lines
 from pypy.interpreter import pyframe
 from pypy.interpreter.nestedscope import Cell
-from pypy.interpreter.pycode import CO_NEWLOCALS
 from pypy.interpreter.argument import ArgumentsForTranslation
 from pypy.interpreter.pyopcode import Return, BytecodeCorruption
 from pypy.objspace.flow.model import (Constant, Variable, Block, Link,
         self.valuestackdepth = code.co_nlocals
         self.lastblock = None
 
-        if func.func_closure is not None:
-            cl = [c.cell_contents for c in func.func_closure]
-            closure = [Cell(Constant(value)) for value in cl]
-        else:
-            closure = []
-        self.initialize_frame_scopes(closure, code)
+        self.cells = code.make_cells(func.func_closure)
         self.f_lineno = code.co_firstlineno
         self.last_instr = 0
 
         self._init_graph(func)
         self.pendingblocks = collections.deque([self.graph.startblock])
 
-    def initialize_frame_scopes(self, closure, code):
-        if not (code.co_flags & CO_NEWLOCALS):
-            raise ValueError("The code object for a function should have "
-                    "the flag CO_NEWLOCALS set.")
-        if len(closure) != len(code.co_freevars):
-            raise ValueError("code object received a closure with "
-                                 "an unexpected number of free variables")
-        self.cells = [Cell() for _ in code.co_cellvars] + closure
-
     def _init_graph(self, func):
         # CallableFactory.pycall may add class_ to functions that are methods
         name = func.func_name