[pypy-svn] r18819 - pypy/dist/pypy/interpreter

arigo at codespeak.net arigo at codespeak.net
Fri Oct 21 13:56:10 CEST 2005


Author: arigo
Date: Fri Oct 21 13:56:09 2005
New Revision: 18819

Modified:
   pypy/dist/pypy/interpreter/nestedscope.py
   pypy/dist/pypy/interpreter/pycode.py
Log:
We need a way to cope with changes of bytecode in CPython -- here MAKE_CLOSURE.
Semi-hackishly stick the magic of CPython on the PyCode object if it is built
from a CPython-compiled code object, and do the right thing depending on this
magic number.  We obviously need a better approach for flexible handling of
bytecodes...



Modified: pypy/dist/pypy/interpreter/nestedscope.py
==============================================================================
--- pypy/dist/pypy/interpreter/nestedscope.py	(original)
+++ pypy/dist/pypy/interpreter/nestedscope.py	Fri Oct 21 13:56:09 2005
@@ -168,14 +168,18 @@
         w_codeobj = f.valuestack.pop()
         codeobj = f.space.interpclass_w(w_codeobj)
         assert isinstance(codeobj, pycode.PyCode)
-        nfreevars = len(codeobj.co_freevars)
-        freevars = []
-        for i in range(nfreevars):
-            cell = f.space.interpclass_w(f.valuestack.pop())
-            if not isinstance(cell, Cell):
-                raise pyframe.BytecodeCorruption
-            freevars.append(cell)
-        freevars.reverse()
+        if codeobj.magic >= 0xa0df281:    # CPython 2.5 AST branch merge
+            w_freevarstuple = f.valuestack.pop()
+            freevars = f.space.unpacktuple(w_freevarstuple)
+        else:
+            nfreevars = len(codeobj.co_freevars)
+            freevars = []
+            for i in range(nfreevars):
+                cell = f.space.interpclass_w(f.valuestack.pop())
+                if not isinstance(cell, Cell):
+                    raise pyframe.BytecodeCorruption
+                freevars.append(cell)
+            freevars.reverse()
         defaultarguments = [f.valuestack.pop() for i in range(numdefaults)]
         defaultarguments.reverse()
         fn = function.Function(f.space, codeobj, f.w_globals,

Modified: pypy/dist/pypy/interpreter/pycode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycode.py	(original)
+++ pypy/dist/pypy/interpreter/pycode.py	Fri Oct 21 13:56:09 2005
@@ -80,6 +80,8 @@
 
 class PyCode(eval.Code):
     "CPython-style code objects."
+
+    magic = 62061 | 0x0a0d0000       # value for Python 2.4.1
     
     def __init__(self, space, co_name=''):
         self.space = space
@@ -105,7 +107,7 @@
     def _code_new( self, argcount, nlocals, stacksize, flags,
                    code, consts, names, varnames, filename,
                    name, firstlineno, lnotab, freevars, cellvars,
-                   hidden_applevel=False):
+                   hidden_applevel=False, from_cpython=False):
         """Initialize a new code objects from parameters from new.code"""
         # simply try to suck in all attributes we know of
         # with a lot of boring asserts to enforce type knowledge
@@ -143,12 +145,20 @@
         newconsts_w = []
         for const in consts:
             if isinstance(const, types.CodeType): # from stable compiler
-                const = PyCode(space)._from_code(const, hidden_applevel=hidden_applevel)
+                const = PyCode(space)._from_code(const, hidden_applevel=hidden_applevel,
+                                                        from_cpython=from_cpython)
             newconsts_w.append(space.wrap(const))
         self.co_consts_w = newconsts_w
+        # stick the underlying CPython magic value, if the code object
+        # comes from there
+        if from_cpython:
+            import imp, struct
+            magic, = struct.unpack("<i", imp.get_magic())
+            if magic != self.magic:
+                self.magic = magic
         return self
 
-    def _from_code(self, code, hidden_applevel=False):
+    def _from_code(self, code, hidden_applevel=False, from_cpython=True):
         """ Initialize the code object from a real (CPython) one.
             This is just a hack, until we have our own compile.
             At the moment, we just fake this.
@@ -171,7 +181,8 @@
                         code.co_lnotab,
                         code.co_freevars,
                         code.co_cellvars,
-                        hidden_applevel )
+                        hidden_applevel,
+                        from_cpython )
         return self
 
 



More information about the Pypy-commit mailing list