[pypy-commit] pypy translation-cleanup: Implement FlowSpaceFrame.cells initialisation in HostCode

rlamy noreply at buildbot.pypy.org
Sun Sep 30 01:20:30 CEST 2012


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: translation-cleanup
Changeset: r57684:c456e79c3e3b
Date: 2012-09-29 19:08 +0100
http://bitbucket.org/pypy/pypy/changeset/c456e79c3e3b/

Log:	Implement FlowSpaceFrame.cells initialisation in HostCode

diff --git a/pypy/objspace/flow/bytecode.py b/pypy/objspace/flow/bytecode.py
--- a/pypy/objspace/flow/bytecode.py
+++ b/pypy/objspace/flow/bytecode.py
@@ -5,7 +5,9 @@
         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):
     """
@@ -64,6 +66,21 @@
                             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``.
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
@@ -2,7 +2,6 @@
 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,
@@ -244,12 +243,7 @@
         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
 
@@ -263,15 +257,6 @@
         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


More information about the pypy-commit mailing list