Author: Romain Guillebert <romain...@gmail.com>
Branch: py3k
Changeset: r51500:b3f6b7f219a0
Date: 2012-01-19 18:28 +0100
http://bitbucket.org/pypy/pypy/changeset/b3f6b7f219a0/

Log:    (antocuni, romain) killed the code that checked for version numbers
        of Python 2, changed the way we compute the magic number for pyc
        files

diff --git a/pypy/interpreter/nestedscope.py b/pypy/interpreter/nestedscope.py
--- a/pypy/interpreter/nestedscope.py
+++ b/pypy/interpreter/nestedscope.py
@@ -220,18 +220,9 @@
     def MAKE_CLOSURE(self, numdefaults, next_instr):
         w_codeobj = self.popvalue()
         codeobj = self.space.interp_w(pycode.PyCode, w_codeobj)
-        if codeobj.magic >= 0xa0df281:    # CPython 2.5 AST branch merge
-            w_freevarstuple = self.popvalue()
-            freevars = [self.space.interp_w(Cell, cell)
-                        for cell in self.space.fixedview(w_freevarstuple)]
-        else:
-            n = len(codeobj.co_freevars)
-            freevars = [None] * n
-            while True:
-                n -= 1
-                if n < 0:
-                    break
-                freevars[n] = self.space.interp_w(Cell, self.popvalue())
+        w_freevarstuple = self.popvalue()
+        freevars = [self.space.interp_w(Cell, cell)
+                    for cell in self.space.fixedview(w_freevarstuple)]
         defaultarguments = self.popvalues(numdefaults)
         fn = function.Function(self.space, codeobj, self.w_globals,
                                defaultarguments, freevars)
diff --git a/pypy/interpreter/pycode.py b/pypy/interpreter/pycode.py
--- a/pypy/interpreter/pycode.py
+++ b/pypy/interpreter/pycode.py
@@ -24,12 +24,19 @@
 def unpack_str_tuple(space,w_str_tuple):
     return [space.str_w(w_el) for w_el in space.unpackiterable(w_str_tuple)]
 
-
 # Magic numbers for the bytecode version in code objects.
 # See comments in pypy/module/imp/importing.
 cpython_magic, = struct.unpack("<i", imp.get_magic())   # host magic number
-default_magic = (168686339+2) | 0x0a0d0000              # this PyPy's magic
-                                                        # (from CPython 2.7.0)
+
+# we compute the magic number in a similar way to CPython, but we use a
+# different value for the highest 16 bits. Bump pypy_incremental_magic every
+# time you make pyc files incompatible
+
+pypy_incremental_magic = 0 # bump it by 16
+assert pypy_incremental_magic % 16 == 0
+assert pypy_incremental_magic < 3000 # the magic number of Python 3. There are
+                                     # no known magic numbers below this value
+default_magic = pypy_incremental_magic | (ord('\r')<<16) | (ord('\n')<<24)
 
 # cpython_code_signature helper
 def cpython_code_signature(code):
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -886,23 +886,13 @@
     def WITH_CLEANUP(self, oparg, next_instr):
         # see comment in END_FINALLY for stack state
         # This opcode changed a lot between CPython versions
-        if (self.pycode.magic >= 0xa0df2ef
-            # Implementation since 2.7a0: 62191 (introduce SETUP_WITH)
-            or self.pycode.magic >= 0xa0df2d1):
-            # implementation since 2.6a1: 62161 (WITH_CLEANUP optimization)
-            self.popvalue()
-            self.popvalue()
-            w_unroller = self.popvalue()
-            w_exitfunc = self.popvalue()
-            self.pushvalue(w_unroller)
-            self.pushvalue(self.space.w_None)
-            self.pushvalue(self.space.w_None)
-        elif self.pycode.magic >= 0xa0df28c:
-            # Implementation since 2.5a0: 62092 (changed WITH_CLEANUP opcode)
-            w_exitfunc = self.popvalue()
-            w_unroller = self.peekvalue(2)
-        else:
-            raise NotImplementedError("WITH_CLEANUP for CPython <= 2.4")
+        self.popvalue()
+        self.popvalue()
+        w_unroller = self.popvalue()
+        w_exitfunc = self.popvalue()
+        self.pushvalue(w_unroller)
+        self.pushvalue(self.space.w_None)
+        self.pushvalue(self.space.w_None)
 
         unroller = self.space.interpclass_w(w_unroller)
         is_app_exc = (unroller is not None and
diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -810,16 +810,14 @@
    a .pyc file in text mode the magic number will be wrong; also, the
    Apple MPW compiler swaps their values, botching string constants.
 
-   CPython uses values between 20121 - 62xxx
+   CPython 2 uses values between 20121 - 62xxx
+   CPython 3 uses values greater than 3000
+   PyPy uses values under 3000
 
 """
 
-# picking a magic number is a mess.  So far it works because we
-# have only one extra opcode, which bumps the magic number by +2, and CPython
-# leaves a gap of 10 when it increases
-# its own magic number.  To avoid assigning exactly the same numbers
-# as CPython we always add a +2.  We'll have to think again when we
-# get three more new opcodes
+# Depending on which opcodes are enabled, eg. CALL_METHOD we bump the version
+# number by some constant
 #
 #  * CALL_METHOD            +2
 #
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to