[pypy-svn] r15124 - in pypy/dist/pypy: interpreter module/__builtin__ module/__builtin__/test module/marshal

tismer at codespeak.net tismer at codespeak.net
Tue Jul 26 15:28:11 CEST 2005


Author: tismer
Date: Tue Jul 26 15:28:08 2005
New Revision: 15124

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/module/__builtin__/importing.py
   pypy/dist/pypy/module/__builtin__/test/test_import.py
   pypy/dist/pypy/module/marshal/app_marshal.py
Log:
Richard & Chris:

added getbuiltinmodule to objspace.

changed marshal's r_long() to always return an int

added basic .pyc file check to importing.

Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Tue Jul 26 15:28:08 2005
@@ -128,6 +128,11 @@
         w_modules = self.sys.get('modules')
         self.setitem(w_modules, w_name, w_mod) 
 
+    def getbuiltinmodule(self, name):
+        w_name = self.wrap(name)
+        w_modules = self.sys.get('modules')
+        return self.getitem(w_modules, w_name)
+
     def make_builtins(self):
         "NOT_RPYTHON: only for initializing the 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 Jul 26 15:28:08 2005
@@ -3,7 +3,6 @@
 """
 
 import sys, os
-import marshal
 
 from pypy.interpreter.module import Module
 from pypy.interpreter.error import OperationError
@@ -309,6 +308,17 @@
 
     return w_mod
 
+# helper, to avoid exposing internals ofmarshal
+def r_long(fd):
+    a = ord(os.read(fd, 1))
+    b = ord(os.read(fd, 1))
+    c = ord(os.read(fd, 1))
+    d = ord(os.read(fd, 1))
+    x = a | (b<<8) | (c<<16) | (d<<24)
+    if d & 0x80 and x > 0:
+        x = -((1L<<32) - x)
+    return int(x)
+
 def check_compiled_module(space, pathname, mtime, cpathname):
     """
     Given a pathname for a Python source file, its time of last
@@ -318,24 +328,22 @@
     the header; if not, return NULL.
     Doesn't set an exception.
     """
-
+    #w_marshal = space.getbuiltinmodule('marshal')
     fd = os.open(cpathname, os.O_BINARY | os.O_RDONLY, 0777) # using no defaults
-    um = marshal.Unmarshaller(fd)
-    
-    magic = um.load_int()
+    magic = r_long(fd)
     if magic != pyc_magic:
         # XXX what to do about Py_VerboseFlag ?
         # PySys_WriteStderr("# %s has bad magic\n", cpathname);
-        os.close(fp)
-        return
-    pyc_mtime = um.load_int()
+        os.close(fd)
+        return -1
+    pyc_mtime = r_long(fd)
     if pyc_mtime != mtime:
         # PySys_WriteStderr("# %s has bad mtime\n", cpathname);
-        os.close(fp)
-        return
+        os.close(fd)
+        return -1
     # if (Py_VerboseFlag)
         # PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
-    return fp
+    return fd
 
 def load_compiled_module(space, name, cpathname, fd):
     """

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 Jul 26 15:28:08 2005
@@ -27,12 +27,12 @@
         cls.saved_modules = _setup(cls.space)
 
     def teardown_class(cls): # interpreter-level
-        _teardown(cls.space,cls.saved_modules)
+        _teardown(cls.space, cls.saved_modules)
 
     def test_import_bare_dir_fails(self):
         def imp():
             import notapackage
-        raises(ImportError,imp)
+        raises(ImportError, imp)
 
     def test_import_sys(self):
         import sys
@@ -152,7 +152,45 @@
         def imp_b():
             import pkg.pkg2.b
         raises(ImportError,imp_b)
-       
+
+from pypy.module.__builtin__ import importing
+
+class TestPycStuff:
+    # ___________________ .pyc related stuff _________________
+
+    def test_check_compiled_module(self):
+        import tempfile, marshal
+
+        def getlong(data):
+            x = marshal.dumps(data)
+            return x[-4:]
+
+        def testfile(magic, mtime):
+            fd, cpathname = tempfile.mkstemp()
+            os.close(fd)
+            f = file(cpathname, "wb")
+            f.write(getlong(magic))
+            f.write(getlong(mtime))
+            f.close()
+            return cpathname
+
+        pathname = "whatever"
+        mtime = 12345
+        cpathname = testfile(importing.pyc_magic, mtime)
+        ret = importing.check_compiled_module(self.space, pathname, mtime, cpathname)
+        assert ret >= 0
+        assert os.lseek(ret, 0, 1) == 8
+        os.close(ret)
+        # check for wrong mtime
+        ret = importing.check_compiled_module(self.space, pathname, mtime+1, cpathname)
+        assert ret < 0
+        os.remove(cpathname)
+        # check for wrong version
+        cpathname = testfile(importing.pyc_magic+1, mtime)
+        ret = importing.check_compiled_module(self.space, pathname, mtime, cpathname)
+        assert ret < 0
+        os.remove(cpathname)
+
 def test_PYTHONPATH_takes_precedence(space): 
     if sys.platform == "win32":
         py.test.skip("unresolved issues with win32 shell quoting rules")

Modified: pypy/dist/pypy/module/marshal/app_marshal.py
==============================================================================
--- pypy/dist/pypy/module/marshal/app_marshal.py	(original)
+++ pypy/dist/pypy/module/marshal/app_marshal.py	Tue Jul 26 15:28:08 2005
@@ -281,7 +281,7 @@
         x = a | (b<<8) | (c<<16) | (d<<24)
         if d & 0x80 and x > 0:
             x = -((1L<<32) - x)
-        return x
+        return int(x)
 
     def r_long64(self):
         a = ord(self._read())



More information about the Pypy-commit mailing list