[pypy-svn] pypy default: Fixed zipimport tests.

alex_gaynor commits-noreply at bitbucket.org
Wed Jan 19 17:26:51 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r40940:f7e7fdadc077
Date: 2011-01-19 10:25 -0600
http://bitbucket.org/pypy/pypy/changeset/f7e7fdadc077/

Log:	Fixed zipimport tests.

diff --git a/pypy/module/zipimport/test/test_undocumented.py b/pypy/module/zipimport/test/test_undocumented.py
--- a/pypy/module/zipimport/test/test_undocumented.py
+++ b/pypy/module/zipimport/test/test_undocumented.py
@@ -17,62 +17,58 @@
                      os.path.join('_pkg', '_subpkg', 'submodule')
                                ])
 
-def temp_zipfile(created_paths, source=True, bytecode=True):
-    """Create a temporary zip file for testing.
-
-    Clears zipimport._zip_directory_cache.
-
-    """
-    import zipimport, os, shutil, zipfile, py_compile
-    example_code = 'attr = None'
-    TESTFN = '@test'
-    zipimport._zip_directory_cache.clear()
-    zip_path = TESTFN + '.zip'
-    bytecode_suffix = 'c'# if __debug__ else 'o'
-    zip_file = zipfile.ZipFile(zip_path, 'w')
-    for path in created_paths:
-        if os.sep in path:
-            directory = os.path.split(path)[0]
-            if not os.path.exists(directory):
-                os.makedirs(directory)
-        code_path = path + '.py'
-        try:
-            temp_file = open(code_path, 'w')
-            temp_file.write(example_code)
-        finally:
-            temp_file.close()
-        if source:
-            zip_file.write(code_path)
-        if bytecode:
-            py_compile.compile(code_path, doraise=True)
-            zip_file.write(code_path + bytecode_suffix)
-    zip_file.close()
-    return os.path.abspath(zip_path)
-
-def cleanup_zipfile(created_paths):
-    import os, shutil
-    bytecode_suffix = 'c'# if __debug__ else 'o'
-    zip_path = '@test.zip'
-    for path in created_paths:
-        if os.sep in path:
-            directory = os.path.split(path)[0]
-            if os.path.exists(directory):
-                shutil.rmtree(directory)
-        else:
-            for suffix in ('.py', '.py' + bytecode_suffix):
-                if os.path.exists(path + suffix):
-                    os.unlink(path + suffix)
-    os.unlink(zip_path)
-
 class AppTestZipImport:
     def setup_class(cls):
         space = gettestobjspace(usemodules=['zipimport', 'rctime'])
         cls.space = space
-        source = "():\n" + str(py.code.Source(temp_zipfile).indent()) + "\n    return temp_zipfile"
-        cls.w_temp_zipfile = space.appexec([], source)
-        source = "():\n" + str(py.code.Source(cleanup_zipfile).indent())+ "\n    return cleanup_zipfile"
-        cls.w_cleanup_zipfile = space.appexec([], source)
         cls.w_created_paths = space.wrap(created_paths)
+    
+    def w_temp_zipfile(self, created_paths, source=True, bytecode=True):
+        """Create a temporary zip file for testing.
+
+        Clears zipimport._zip_directory_cache.
+
+        """
+        import zipimport, os, shutil, zipfile, py_compile
+        example_code = 'attr = None'
+        TESTFN = '@test'
+        zipimport._zip_directory_cache.clear()
+        zip_path = TESTFN + '.zip'
+        bytecode_suffix = 'c'# if __debug__ else 'o'
+        zip_file = zipfile.ZipFile(zip_path, 'w')
+        for path in created_paths:
+            if os.sep in path:
+                directory = os.path.split(path)[0]
+                if not os.path.exists(directory):
+                    os.makedirs(directory)
+            code_path = path + '.py'
+            try:
+                temp_file = open(code_path, 'w')
+                temp_file.write(example_code)
+            finally:
+                temp_file.close()
+            if source:
+                zip_file.write(code_path)
+            if bytecode:
+                py_compile.compile(code_path, doraise=True)
+                zip_file.write(code_path + bytecode_suffix)
+        zip_file.close()
+        return os.path.abspath(zip_path)
+
+    def w_cleanup_zipfile(self, created_paths):
+        import os, shutil
+        bytecode_suffix = 'c'# if __debug__ else 'o'
+        zip_path = '@test.zip'
+        for path in created_paths:
+            if os.sep in path:
+                directory = os.path.split(path)[0]
+                if os.path.exists(directory):
+                    shutil.rmtree(directory)
+            else:
+                for suffix in ('.py', '.py' + bytecode_suffix):
+                    if os.path.exists(path + suffix):
+                        os.unlink(path + suffix)
+        os.unlink(zip_path)
 
     def test_inheritance(self):
         # Should inherit from ImportError.

diff --git a/pypy/module/zipimport/test/test_zipimport.py b/pypy/module/zipimport/test/test_zipimport.py
--- a/pypy/module/zipimport/test/test_zipimport.py
+++ b/pypy/module/zipimport/test/test_zipimport.py
@@ -58,25 +58,6 @@
         cls.w_tmpzip = space.wrap(str(ziptestmodule))
         cls.w_co = space.wrap(co)
         cls.tmpdir = tmpdir
-        cls.w_writefile = space.appexec([], """():
-        def writefile (self, filename, data):
-            import sys
-            import time
-            from zipfile import ZipFile, ZipInfo
-            z = ZipFile(self.zipfile, 'w')
-            write_files = self.write_files
-            write_files.append((filename, data))
-            for filename, data in write_files:
-                zinfo = ZipInfo(filename, time.localtime(self.now))
-                zinfo.compress_type = self.compression
-                z.writestr(zinfo, data)
-            self.write_files = write_files
-            # XXX populates sys.path, but at applevel
-            if sys.path[0] != self.zipfile:
-                sys.path.insert(0, self.zipfile)
-            z.close()
-        return writefile
-        """)
     make_class = classmethod(make_class)
 
     def setup_class(cls):
@@ -109,8 +90,25 @@
         """)
         self.w_modules = []
 
+    def w_writefile(self, filename, data):
+        import sys
+        import time
+        from zipfile import ZipFile, ZipInfo
+        z = ZipFile(self.zipfile, 'w')
+        write_files = self.write_files
+        write_files.append((filename, data))
+        for filename, data in write_files:
+            zinfo = ZipInfo(filename, time.localtime(self.now))
+            zinfo.compress_type = self.compression
+            z.writestr(zinfo, data)
+        self.write_files = write_files
+        # XXX populates sys.path, but at applevel
+        if sys.path[0] != self.zipfile:
+            sys.path.insert(0, self.zipfile)
+        z.close()
+
     def test_cache(self):
-        self.writefile(self, 'x.py', 'y')
+        self.writefile('x.py', 'y')
         from zipimport import _zip_directory_cache, zipimporter
         new_importer = zipimporter(self.zipfile)
         try:
@@ -119,9 +117,9 @@
             del _zip_directory_cache[self.zipfile]
 
     def test_cache_subdir(self):
-        self.writefile(self, 'x.py', '')
-        self.writefile(self, 'sub/__init__.py', '')
-        self.writefile(self, 'sub/yy.py', '')
+        self.writefile('x.py', '')
+        self.writefile('sub/__init__.py', '')
+        self.writefile('sub/yy.py', '')
         from zipimport import _zip_directory_cache, zipimporter
         sub_importer = zipimporter(self.zipfile + '/sub')
         main_importer = zipimporter(self.zipfile)
@@ -133,7 +131,7 @@
     def test_good_bad_arguments(self):
         from zipimport import zipimporter
         import os
-        self.writefile(self, "x.py", "y")
+        self.writefile("x.py", "y")
         zipimporter(self.zipfile) # should work
         raises(ImportError, "zipimporter(os.path.dirname(self.zipfile))")
         raises(ImportError, 'zipimporter("fsafdetrssffdsagadfsafdssadasa")')
@@ -147,7 +145,7 @@
 
     def test_py(self):
         import sys, os
-        self.writefile(self, "uuu.py", "def f(x): return x")
+        self.writefile("uuu.py", "def f(x): return x")
         mod = __import__('uuu', globals(), locals(), [])
         print mod
         assert mod.f(3) == 3
@@ -161,8 +159,8 @@
     
     def test_pyc(self):
         import sys, os
-        self.writefile(self, "uuu.pyc", self.test_pyc)
-        self.writefile(self, "uuu.py", "def f(x): return x")
+        self.writefile("uuu.pyc", self.test_pyc)
+        self.writefile("uuu.py", "def f(x): return x")
         mod = __import__('uuu', globals(), locals(), [])
         expected = {
             '__doc__' : None,
@@ -187,7 +185,7 @@
         m0 = ord(self.test_pyc[0])
         m0 ^= 0x04
         test_pyc = chr(m0) + self.test_pyc[1:]
-        self.writefile(self, "uu.pyc", test_pyc)
+        self.writefile("uu.pyc", test_pyc)
         raises(ImportError, "__import__('uu', globals(), locals(), [])")
         assert 'uu' not in sys.modules
 
@@ -196,8 +194,8 @@
         m0 = ord(self.test_pyc[0])
         m0 ^= 0x04
         test_pyc = chr(m0) + self.test_pyc[1:]
-        self.writefile(self, "uu.pyc", test_pyc)
-        self.writefile(self, "uu.py", "def f(x): return x")
+        self.writefile("uu.pyc", test_pyc)
+        self.writefile("uu.py", "def f(x): return x")
         mod = __import__("uu", globals(), locals(), [])
         assert mod.f(3) == 3
 
@@ -205,7 +203,7 @@
         m0 = ord(self.test_pyc[0])
         m0 ^= 0x04
         test_pyc = chr(m0) + self.test_pyc[1:]
-        self.writefile(self, "uuu.pyc", test_pyc)
+        self.writefile("uuu.pyc", test_pyc)
         import sys
         import zipimport
         z = zipimport.zipimporter(self.zipfile)
@@ -215,8 +213,8 @@
 
     def test_package(self):
         import os, sys
-        self.writefile(self, "xxuuu/__init__.py", "")
-        self.writefile(self, "xxuuu/yy.py", "def f(x): return x")
+        self.writefile("xxuuu/__init__.py", "")
+        self.writefile("xxuuu/yy.py", "def f(x): return x")
         mod = __import__("xxuuu", globals(), locals(), ['yy'])
         assert mod.__path__ == [self.zipfile + os.path.sep + "xxuuu"]
         assert mod.__file__ == (self.zipfile + os.path.sep
@@ -231,8 +229,8 @@
         mod.__path__ = [self.zipfile + '/xxuuv']
         sys.modules['xxuuv'] = mod
         #
-        self.writefile(self, "xxuuv/__init__.py", "")
-        self.writefile(self, "xxuuv/yy.py", "def f(x): return x")
+        self.writefile("xxuuv/__init__.py", "")
+        self.writefile("xxuuv/yy.py", "def f(x): return x")
         mod = __import__("xxuuv.yy", globals(), locals(), ['__doc__'])
         assert mod.__file__ == (self.zipfile + os.path.sep
                                 + "xxuuv" + os.path.sep
@@ -246,8 +244,8 @@
         mod.__path__ = [self.zipfile + '/xxuuw']
         sys.modules['xxuuw'] = mod
         #
-        self.writefile(self, "xxuuw/__init__.py", "")
-        self.writefile(self, "xxuuw/zz.pyc", self.test_pyc)
+        self.writefile("xxuuw/__init__.py", "")
+        self.writefile("xxuuw/zz.pyc", self.test_pyc)
         mod = __import__("xxuuw.zz", globals(), locals(), ['__doc__'])
         assert mod.__file__ == (self.zipfile + os.path.sep
                                 + "xxuuw" + os.path.sep
@@ -259,10 +257,10 @@
         import os
         import zipimport
         data = "saddsadsa"
-        self.writefile(self, "xxx", data)
-        self.writefile(self, "xx"+os.sep+"__init__.py", "5")
-        self.writefile(self, "yy.py", "3")
-        self.writefile(self, 'uu.pyc', self.test_pyc)
+        self.writefile("xxx", data)
+        self.writefile("xx"+os.sep+"__init__.py", "5")
+        self.writefile("yy.py", "3")
+        self.writefile('uu.pyc', self.test_pyc)
         z = zipimport.zipimporter(self.zipfile)
         assert z.get_data(self.zipfile + os.sep + "xxx") == data
         assert z.is_package("xx")
@@ -286,7 +284,7 @@
         import os
         import zipimport
         self.writefile(
-            self, os.sep.join(("directory", "package", "__init__.py")), "")
+            os.sep.join(("directory", "package", "__init__.py")), "")
         importer = zipimport.zipimporter(self.zipfile + "/directory")
         # Grab this so if the assertion fails, py.test will display its
         # value.  Not sure why it doesn't the assertion uses import.archive
@@ -302,7 +300,7 @@
         import os
         import zipimport
         self.writefile(
-            self, os.sep.join(("directory", "package", "__init__.py")), "")
+            os.sep.join(("directory", "package", "__init__.py")), "")
         z = zipimport.zipimporter(self.zipfile + "/directory")
         mod = z.load_module("package")
         assert z.is_package("package")
@@ -313,7 +311,7 @@
         """
         import os
         import zipimport
-        self.writefile(self,
+        self.writefile(
                      os.sep.join(("directory", "package", "__init__.py")), "")
         importer = zipimport.zipimporter(self.zipfile + "/directory")
         l = [i for i in zipimport._zip_directory_cache]


More information about the Pypy-commit mailing list