[pypy-svn] r40818 - in pypy/dist/pypy: config module/__builtin__ module/__builtin__/test

arigo at codespeak.net arigo at codespeak.net
Tue Mar 20 11:35:47 CET 2007


Author: arigo
Date: Tue Mar 20 11:35:45 2007
New Revision: 40818

Modified:
   pypy/dist/pypy/config/pypyoption.py
   pypy/dist/pypy/module/__builtin__/importing.py
   pypy/dist/pypy/module/__builtin__/test/test_import.py
Log:
pyc files support for the versions of pypy-c that include new opcodes,
with different magic numbers.  The new test in test_import will fail
if you add a new option in objspace.opcodes.* but forget to have it
influence the magic number.


Modified: pypy/dist/pypy/config/pypyoption.py
==============================================================================
--- pypy/dist/pypy/config/pypyoption.py	(original)
+++ pypy/dist/pypy/config/pypyoption.py	Tue Mar 20 11:35:45 2007
@@ -58,11 +58,9 @@
     OptionDescription("opcodes", "opcodes to enable in the interpreter", [
         BoolOption("CALL_LIKELY_BUILTIN", "emit a special bytecode for likely calls to builtin functions",
                    default=False,
-                   requires=[("objspace.usepycfiles", False),
-                             ("objspace.std.withmultidict", True)]),
+                   requires=[("objspace.std.withmultidict", True)]),
         BoolOption("CALL_METHOD", "emit a special bytecode for expr.name()",
-                   default=False,
-                   requires=[("objspace.usepycfiles", False)]),
+                   default=False),
         ]),
 
     BoolOption("nofaking", "disallow faking in the object space",

Modified: pypy/dist/pypy/module/__builtin__/importing.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/importing.py	(original)
+++ pypy/dist/pypy/module/__builtin__/importing.py	Tue Mar 20 11:35:45 2007
@@ -327,14 +327,27 @@
 """
 
 # we decided to use the magic of 2.4.1
+#
+# In addition, for now, the presence of special bytecodes bumps the
+# magic number:
+#
+#  * CALL_LIKELY_BUILTIN    +2
+#  * CALL_METHOD            +4
+#
+# this is a bit of a hack waiting for a nicer general solution.
+# Adding another bytecode is already a problem: if we bump the
+# number by a total of +10 we collide with CPython's own magic
+# number for 2.5a0.
+#
 MAGIC = 62061 | (ord('\r')<<16) | (ord('\n')<<24)
 
-"""Magic word as global; note that _PyImport_Init() can change the
-   value of this global to accommodate for alterations of how the
-   compiler works which are enabled by command line switches.
-"""
-
-pyc_magic = MAGIC
+def get_pyc_magic(space):
+    result = MAGIC
+    if space.config.objspace.opcodes.CALL_LIKELY_BUILTIN:
+        result += 2
+    if space.config.objspace.opcodes.CALL_METHOD:
+        result += 4
+    return result
 
 
 def parse_source_module(space, pathname, stream):
@@ -408,7 +421,7 @@
     stream = streamio.open_file_as_stream(cpathname, "rb")
     magic = _r_long(stream)
     try:
-        if magic != pyc_magic:
+        if magic != get_pyc_magic(space):
             # XXX what to do about Py_VerboseFlag ?
             # PySys_WriteStderr("# %s has bad magic\n", cpathname);
             return -1
@@ -441,7 +454,7 @@
     """
     w = space.wrap
     magic = _r_long(stream)
-    if magic != pyc_magic:
+    if magic != get_pyc_magic(space):
         raise OperationError(space.w_ImportError, w(
             "Bad magic number in %s" % cpathname))
     _r_long(stream) # skip time stamp
@@ -492,7 +505,7 @@
 
             # should be ok (XXX or should call os.fsync() to be sure?)
             stream.seek(0, 0)
-            _w_long(stream, pyc_magic)
+            _w_long(stream, get_pyc_magic(space))
             _w_long(stream, mtime)
         finally:
             stream.close()

Modified: pypy/dist/pypy/module/__builtin__/test/test_import.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/test/test_import.py	(original)
+++ pypy/dist/pypy/module/__builtin__/test/test_import.py	Tue Mar 20 11:35:45 2007
@@ -4,6 +4,7 @@
 import pypy.interpreter.pycode
 from pypy.tool.udir import udir
 from pypy.rlib import streamio
+from pypy.conftest import gettestobjspace
 import sys, os
 import tempfile, marshal
 
@@ -281,7 +282,7 @@
         space = self.space
         pathname = "whatever"
         mtime = 12345
-        cpathname = _testfile(importing.pyc_magic, mtime)
+        cpathname = _testfile(importing.get_pyc_magic(space), mtime)
         ret = importing.check_compiled_module(space,
                                               pathname,
                                               mtime,
@@ -297,7 +298,7 @@
         os.remove(cpathname)
 
         # check for wrong version
-        cpathname = _testfile(importing.pyc_magic+1, mtime)
+        cpathname = _testfile(importing.get_pyc_magic(space)+1, mtime)
         ret = importing.check_compiled_module(space,
                                               pathname,
                                               mtime,
@@ -319,7 +320,7 @@
         pathname = "whatever"
         mtime = 12345
         co = compile('x = 42', '?', 'exec')
-        cpathname = _testfile(importing.pyc_magic, mtime, co)
+        cpathname = _testfile(importing.get_pyc_magic(space), mtime, co)
         stream = streamio.open_file_as_stream(cpathname, "r")
         try:
             stream.seek(8, 0)
@@ -340,7 +341,7 @@
         pathname = "whatever"
         mtime = 12345
         co = compile('x = 42', '?', 'exec')
-        cpathname = _testfile(importing.pyc_magic, mtime, co)
+        cpathname = _testfile(importing.get_pyc_magic(space), mtime, co)
         w_modulename = space.wrap('somemodule')
         stream = streamio.open_file_as_stream(cpathname, "r")
         try:
@@ -460,6 +461,37 @@
         ret = space.int_w(w_ret)
         assert ret == 42
 
+    def test_pyc_magic_changes(self):
+        # test that the pyc files produced by a space are not reimportable
+        # from another, if they differ in what opcodes they support
+        allspaces = [self.space]
+        for opcodename in self.space.config.objspace.opcodes.getpaths():
+            key = 'objspace.opcodes.' + opcodename
+            space2 = gettestobjspace(**{key: True})
+            allspaces.append(space2)
+        for space1 in allspaces:
+            for space2 in allspaces:
+                if space1 is space2:
+                    continue
+                pathname = "whatever"
+                mtime = 12345
+                co = compile('x = 42', '?', 'exec')
+                cpathname = _testfile(importing.get_pyc_magic(space1),
+                                      mtime, co)
+                w_modulename = space2.wrap('somemodule')
+                stream = streamio.open_file_as_stream(cpathname, "r")
+                try:
+                    w_mod = space2.wrap(Module(space2, w_modulename))
+                    space2.raises_w(space2.w_ImportError,
+                                    importing.load_compiled_module,
+                                    space2,
+                                    w_modulename,
+                                    w_mod,
+                                    cpathname,
+                                    stream)
+                finally:
+                    stream.close()
+
 
 def test_PYTHONPATH_takes_precedence(space): 
     if sys.platform == "win32":



More information about the Pypy-commit mailing list