[pypy-svn] r15130 - in pypy/dist/pypy: module/__builtin__ module/__builtin__/test module/marshal tool

tismer at codespeak.net tismer at codespeak.net
Tue Jul 26 17:38:26 CEST 2005


Author: tismer
Date: Tue Jul 26 17:38:23 2005
New Revision: 15130

Modified:
   pypy/dist/pypy/module/__builtin__/importing.py
   pypy/dist/pypy/module/__builtin__/test/test_import.py
   pypy/dist/pypy/module/marshal/__init__.py
   pypy/dist/pypy/module/marshal/app_marshal.py
   pypy/dist/pypy/tool/osfilewrapper.py
Log:
Richard & Chris:

finished the binary loading support functions and tests.

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 17:38:23 2005
@@ -7,6 +7,7 @@
 from pypy.interpreter.module import Module
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.baseobjspace import W_Root, ObjSpace
+from pypy.tool.osfilewrapper import OsFileWrapper
 
 # XXX this uses the os.path module at interp-level, which means
 # XXX that translate_pypy will produce a translated version of
@@ -308,12 +309,12 @@
 
     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))
+# helper, to avoid exposing internals of marshal
+def _r_long(osfile):
+    a = ord(osfile.read(1))
+    b = ord(osfile.read(1))
+    c = ord(osfile.read(1))
+    d = ord(osfile.read(1))
     x = a | (b<<8) | (c<<16) | (d<<24)
     if d & 0x80 and x > 0:
         x = -((1L<<32) - x)
@@ -328,15 +329,15 @@
     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
-    magic = r_long(fd)
+    osfile = OsFileWrapper(fd)
+    magic = _r_long(osfile)
     if magic != pyc_magic:
         # XXX what to do about Py_VerboseFlag ?
         # PySys_WriteStderr("# %s has bad magic\n", cpathname);
         os.close(fd)
         return -1
-    pyc_mtime = r_long(fd)
+    pyc_mtime = _r_long(osfile)
     if pyc_mtime != mtime:
         # PySys_WriteStderr("# %s has bad mtime\n", cpathname);
         os.close(fd)
@@ -345,14 +346,38 @@
         # PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
     return fd
 
-def load_compiled_module(space, name, cpathname, fd):
+def read_compiled_module(space, cpathname, fd):
+    """ Read a code object from a file and check it for validity """
+
+    w_marshal = space.getbuiltinmodule('marshal')
+    w_M = space.getattr(w_marshal, space.wrap('_Unmarshaller'))
+    w_unmarshaller = space.call_function(w_M, space.wrap(fd))
+    w_code = space.call_method(w_unmarshaller, 'load')
+    pycode = space.interpclass_w(w_code)
+    if pycode is None:
+        raise OperationError(space.w_ImportError, space.wrap(
+            "Non-code object in %.200s" % cpathname))
+    return pycode
+
+def load_compiled_module(space, w_modulename, w_mod, cpathname, fd):
     """
     Load a module from a compiled file, execute it, and return its
     module object.
     """
-
-def read_compiled_module(space, cpathname, fd):
-    """ Read a code object from a file and check it for validity """
+    osfile = OsFileWrapper(fd)
+    magic = _r_long(osfile)
+    if magic != pyc_magic:
+        raise OperationError(space.w_ImportError, space.wrap(
+            "Bad magic number in %.200s" % cpathname))
+        return NULL;
+    _r_long(osfile) # skip time stamp
+    code_w = read_compiled_module(space, cpathname, fd)
+    #if (Py_VerboseFlag)
+    #    PySys_WriteStderr("import %s # precompiled from %s\n",
+    #        name, cpathname);
+    w_dic = space.getattr(w_mod, space.wrap('__dict__'))
+    code_w.exec_code(space, w_dic, w_dic)
+    return w_mod
 
 
 def write_compiled_module(space, co, cpathname, mtime):

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 17:38:23 2005
@@ -1,7 +1,10 @@
 import py
+from pypy.interpreter.module import Module
 from pypy.interpreter import gateway
+import pypy.interpreter.pycode
 from pypy.tool.udir import udir 
 import sys, os
+import tempfile, marshal
 
 def _setup(space): 
     dn=os.path.abspath(os.path.join(os.path.dirname(__file__), 'impsubdir'))
@@ -155,28 +158,28 @@
 
 from pypy.module.__builtin__ import importing
 
+def _getlong(data):
+    x = marshal.dumps(data)
+    return x[-4:]
+
+def _testfile(magic, mtime, co=None):
+    fd, cpathname = tempfile.mkstemp()
+    os.close(fd)
+    f = file(cpathname, "wb")
+    f.write(_getlong(magic))
+    f.write(_getlong(mtime))
+    if co:
+        marshal.dump(co, f)
+    f.close()
+    return cpathname
+
 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)
+        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
@@ -186,11 +189,44 @@
         assert ret < 0
         os.remove(cpathname)
         # check for wrong version
-        cpathname = testfile(importing.pyc_magic+1, mtime)
+        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_read_compiled_module(self):
+        space = self.space
+        pathname = "whatever"
+        mtime = 12345
+        co = compile('x = 42', '?', 'exec')
+        cpathname = _testfile(importing.pyc_magic, mtime, co)
+        fd = os.open(cpathname, os.O_BINARY | os.O_RDONLY, 0777)
+        os.lseek(fd, 8, 0)
+        code_w = importing.read_compiled_module(space, cpathname, fd)
+        os.close(fd)
+        assert type(code_w) is pypy.interpreter.pycode.PyCode
+        w_dic = space.newdict([])
+        code_w.exec_code(space, w_dic, w_dic)
+        w_ret = space.getitem(w_dic, space.wrap('x'))
+        ret = space.int_w(w_ret)
+        assert ret == 42
+
+    def test_load_compiled_module(self):
+        space = self.space
+        pathname = "whatever"
+        mtime = 12345
+        co = compile('x = 42', '?', 'exec')
+        cpathname = _testfile(importing.pyc_magic, mtime, co)
+        w_modulename = space.wrap('somemodule')
+        fd = os.open(cpathname, os.O_BINARY | os.O_RDONLY, 0777)
+        w_mod = space.wrap(Module(space, w_modulename))
+        w_ret = importing.load_compiled_module(space, w_modulename, w_mod, cpathname, fd)
+        os.close(fd)
+        assert w_mod is w_ret
+        w_ret = space.getattr(w_mod, space.wrap('x'))
+        ret = space.int_w(w_ret)
+        assert ret == 42
+
 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/__init__.py
==============================================================================
--- pypy/dist/pypy/module/marshal/__init__.py	(original)
+++ pypy/dist/pypy/module/marshal/__init__.py	Tue Jul 26 17:38:23 2005
@@ -10,6 +10,8 @@
         'dumps'         : 'app_marshal.dumps',
         'load'          : 'app_marshal.load',
         'loads'         : 'app_marshal.loads',
+        '_Marshaller'   : 'app_marshal.Marshaller',
+        '_Unmarshaller' : 'app_marshal.Unmarshaller',
     }
 
     interpleveldefs = {

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 17:38:23 2005
@@ -271,7 +271,10 @@
         c = self._read()
         if not c:
             raise EOFError
-        return self.dispatch[c](self)
+        try:
+            return self.dispatch[c](self)
+        except KeyError:
+            raise ValueError, "bad marshal code: %c (%d)" % (c, ord(c))
 
     def r_short(self):
         lo = ord(self._read())

Modified: pypy/dist/pypy/tool/osfilewrapper.py
==============================================================================
--- pypy/dist/pypy/tool/osfilewrapper.py	(original)
+++ pypy/dist/pypy/tool/osfilewrapper.py	Tue Jul 26 17:38:23 2005
@@ -29,8 +29,7 @@
     def close(self):
         os.close(self.fd)
 
-    def create_wrapper(cls, filename, flag, mode=0777):
-        fd = os.open(filename, flag, mode)
-        return cls(fd)
     
-    create_wrapper = classmethod(create_wrapper)
+def create_wrapper(filename, flag, mode=0777):
+    fd = os.open(filename, flag, mode)
+    return OsFileWrapper(fd)



More information about the Pypy-commit mailing list