[pypy-svn] r78482 - in pypy/branch/fast-forward/pypy/module/zipimport: . test

afa at codespeak.net afa at codespeak.net
Fri Oct 29 18:36:48 CEST 2010


Author: afa
Date: Fri Oct 29 18:36:46 2010
New Revision: 78482

Modified:
   pypy/branch/fast-forward/pypy/module/zipimport/interp_zipimport.py
   pypy/branch/fast-forward/pypy/module/zipimport/test/test_zipimport.py
Log:
Fix in zipimport: get_code() is supposed to return a Code object,
not the content of the .pyc file!


Modified: pypy/branch/fast-forward/pypy/module/zipimport/interp_zipimport.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/zipimport/interp_zipimport.py	(original)
+++ pypy/branch/fast-forward/pypy/module/zipimport/interp_zipimport.py	Fri Oct 29 18:36:46 2010
@@ -183,12 +183,20 @@
             return False
         return True
 
+    def can_use_pyc(self, space, filename, magic, timestamp):
+        if magic != importing.get_pyc_magic(space):
+            return False
+        if self.check_newer_pyfile(space, filename[:-1], timestamp):
+            return False
+        if not self.check_compatible_mtime(space, filename, timestamp):
+            return False
+        return True
+
     def import_pyc_file(self, space, modname, filename, buf, pkgpath):
         w = space.wrap
         magic = importing._get_long(buf[:4])
         timestamp = importing._get_long(buf[4:8])
-        if (self.check_newer_pyfile(space, filename[:-1], timestamp) or
-            not self.check_compatible_mtime(space, filename, timestamp)):
+        if not self.can_use_pyc(space, filename, magic, timestamp):
             return self.import_py_file(space, modname, filename[:-1], buf,
                                        pkgpath)
         buf = buf[8:] # XXX ugly copy, should use sequential read instead
@@ -276,16 +284,22 @@
 
     def get_code(self, space, fullname):
         filename = self.make_filename(fullname)
-        w = space.wrap
         for compiled, _, ext in ENUMERATE_EXTS:
             if self.have_modulefile(space, filename + ext):
+                w_source = self.get_data(space, filename + ext)
+                source = space.str_w(w_source)
                 if compiled:
-                    return self.get_data(space, filename + ext)
+                    magic = importing._get_long(source[:4])
+                    timestamp = importing._get_long(source[4:8])
+                    if not self.can_use_pyc(space, filename + ext,
+                                            magic, timestamp):
+                        continue
+                    code_w = importing.read_compiled_module(
+                        space, filename + ext, source[8:])
                 else:
-                    w_source = self.get_data(space, filename + ext)
-                    w_code = space.builtin.call('compile', w_source,
-                                                w(filename + ext), w('exec'))
-                    return w_code
+                    code_w = importing.parse_source_module(
+                        space, filename + ext, source)
+                return space.wrap(code_w)
         raise operationerrfmt(self.w_ZipImportError,
             "Cannot find source or code for %s in %s", filename, self.name)
     get_code.unwrap_spec = ['self', ObjSpace, str]
@@ -386,7 +400,7 @@
     get_data    = interp2app(W_ZipImporter.get_data),
     get_code    = interp2app(W_ZipImporter.get_code),
     get_source  = interp2app(W_ZipImporter.get_source),
-    _get_filename = interp2app(W_ZipImporter.get_filename),
+    get_filename = interp2app(W_ZipImporter.get_filename),
     is_package  = interp2app(W_ZipImporter.is_package),
     load_module = interp2app(W_ZipImporter.load_module),
     archive     = GetSetProperty(W_ZipImporter.getarchive),

Modified: pypy/branch/fast-forward/pypy/module/zipimport/test/test_zipimport.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/zipimport/test/test_zipimport.py	(original)
+++ pypy/branch/fast-forward/pypy/module/zipimport/test/test_zipimport.py	Fri Oct 29 18:36:46 2010
@@ -175,7 +175,12 @@
         assert mod.__file__.endswith('.zip'+os.sep+'uuu.pyc')
         assert mod.get_file() == mod.__file__
         assert mod.get_name() == mod.__name__
-                                
+        #
+        import zipimport
+        z = zipimport.zipimporter(self.zipfile)
+        code = z.get_code('uuu')
+        assert isinstance(code, type((lambda:0).func_code))
+
     def test_bad_pyc(self):
         import zipimport
         import sys
@@ -206,6 +211,7 @@
         z = zipimport.zipimporter(self.zipfile)
         sys.modules['uuu'] = lambda x : x + 1
         raises(ImportError, z.load_module, 'uuu')
+        raises(zipimport.ZipImportError, z.get_code, 'uuu')
 
     def test_package(self):
         import os, sys
@@ -270,7 +276,7 @@
         assert z.get_source('xx') == "5"
         assert z.archive == self.zipfile
         mod = z.load_module('xx')
-        assert z._get_filename('xx') == mod.__file__
+        assert z.get_filename('xx') == mod.__file__
 
     def test_archive(self):
         """
@@ -300,7 +306,7 @@
         z = zipimport.zipimporter(self.zipfile + "/directory")
         mod = z.load_module("package")
         assert z.is_package("package")
-        assert z._get_filename("package") == mod.__file__
+        assert z.get_filename("package") == mod.__file__
 
     def test_zip_directory_cache(self):
         """ Check full dictionary interface



More information about the Pypy-commit mailing list