0001 import sys 0002 import os 0003 import marshal 0004 import imp 0005 import struct 0006 import time 0007 0008 import zlib # implied prerequisite 0009 from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED 0010 from test import test_support 0011 from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co 0012 0013 import zipimport 0014 0015 0016 def make_pyc(co, mtime): 0017 data = marshal.dumps(co) 0018 if type(mtime) is type(0.0): 0019 # Mac mtimes need a bit of special casing 0020 if mtime < 0x7fffffff: 0021 mtime = int(mtime) 0022 else: 0023 mtime = int(-0x100000000L + long(mtime)) 0024 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data 0025 return pyc 0026 0027 NOW = time.time() 0028 test_pyc = make_pyc(test_co, NOW) 0029 0030 0031 if __debug__: 0032 pyc_ext = ".pyc" 0033 else: 0034 pyc_ext = ".pyo" 0035 0036 0037 TESTMOD = "ziptestmodule" 0038 TESTPACK = "ziptestpackage" 0039 TESTPACK2 = "ziptestpackage2" 0040 TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip") 0041 0042 class UncompressedZipImportTestCase(ImportHooksBaseTestCase): 0043 0044 compression = ZIP_STORED 0045 0046 def setUp(self): 0047 # We're reusing the zip archive path, so we must clear the 0048 # cached directory info. 0049 zipimport._zip_directory_cache.clear() 0050 ImportHooksBaseTestCase.setUp(self) 0051 0052 def doTest(self, expected_ext, files, *modules, **kw): 0053 z = ZipFile(TEMP_ZIP, "w") 0054 try: 0055 for name, (mtime, data) in files.items(): 0056 zinfo = ZipInfo(name, time.localtime(mtime)) 0057 zinfo.compress_type = self.compression 0058 z.writestr(zinfo, data) 0059 z.close() 0060 0061 stuff = kw.get("stuff", None) 0062 if stuff is not None: 0063 # Prepend 'stuff' to the start of the zipfile 0064 f = open(TEMP_ZIP, "rb") 0065 data = f.read() 0066 f.close() 0067 0068 f = open(TEMP_ZIP, "wb") 0069 f.write(stuff) 0070 f.write(data) 0071 f.close() 0072 0073 sys.path.insert(0, TEMP_ZIP) 0074 0075 mod = __import__(".".join(modules), globals(), locals(), 0076 ["__dummy__"]) 0077 if expected_ext: 0078 file = mod.get_file() 0079 self.assertEquals(file, os.path.join(TEMP_ZIP, 0080 *modules) + expected_ext) 0081 finally: 0082 z.close() 0083 os.remove(TEMP_ZIP) 0084 0085 def testAFakeZlib(self): 0086 # 0087 # This could cause a stack overflow before: importing zlib.py 0088 # from a compressed archive would cause zlib to be imported 0089 # which would find zlib.py in the archive, which would... etc. 0090 # 0091 # This test *must* be executed first: it must be the first one 0092 # to trigger zipimport to import zlib (zipimport caches the 0093 # zlib.decompress function object, after which the problem being 0094 # tested here wouldn't be a problem anymore... 0095 # (Hence the 'A' in the test method name: to make it the first 0096 # item in a list sorted by name, like unittest.makeSuite() does.) 0097 # 0098 # This test fails on platforms on which the zlib module is 0099 # statically linked, but the problem it tests for can't 0100 # occur in that case (builtin modules are always found first), 0101 # so we'll simply skip it then. Bug #765456. 0102 # 0103 if "zlib" in sys.builtin_module_names: 0104 return 0105 if "zlib" in sys.modules: 0106 del sys.modules["zlib"] 0107 files = {"zlib.py": (NOW, test_src)} 0108 try: 0109 self.doTest(".py", files, "zlib") 0110 except ImportError: 0111 if self.compression != ZIP_DEFLATED: 0112 self.fail("expected test to not raise ImportError") 0113 else: 0114 if self.compression != ZIP_STORED: 0115 self.fail("expected test to raise ImportError") 0116 0117 def testPy(self): 0118 files = {TESTMOD + ".py": (NOW, test_src)} 0119 self.doTest(".py", files, TESTMOD) 0120 0121 def testPyc(self): 0122 files = {TESTMOD + pyc_ext: (NOW, test_pyc)} 0123 self.doTest(pyc_ext, files, TESTMOD) 0124 0125 def testBoth(self): 0126 files = {TESTMOD + ".py": (NOW, test_src), 0127 TESTMOD + pyc_ext: (NOW, test_pyc)} 0128 self.doTest(pyc_ext, files, TESTMOD) 0129 0130 def testEmptyPy(self): 0131 files = {TESTMOD + ".py": (NOW, "")} 0132 self.doTest(None, files, TESTMOD) 0133 0134 def testBadMagic(self): 0135 # make pyc magic word invalid, forcing loading from .py 0136 m0 = ord(test_pyc[0]) 0137 m0 ^= 0x04 # flip an arbitrary bit 0138 badmagic_pyc = chr(m0) + test_pyc[1:] 0139 files = {TESTMOD + ".py": (NOW, test_src), 0140 TESTMOD + pyc_ext: (NOW, badmagic_pyc)} 0141 self.doTest(".py", files, TESTMOD) 0142 0143 def testBadMagic2(self): 0144 # make pyc magic word invalid, causing an ImportError 0145 m0 = ord(test_pyc[0]) 0146 m0 ^= 0x04 # flip an arbitrary bit 0147 badmagic_pyc = chr(m0) + test_pyc[1:] 0148 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)} 0149 try: 0150 self.doTest(".py", files, TESTMOD) 0151 except ImportError: 0152 pass 0153 else: 0154 self.fail("expected ImportError; import from bad pyc") 0155 0156 def testBadMTime(self): 0157 t3 = ord(test_pyc[7]) 0158 t3 ^= 0x02 # flip the second bit -- not the first as that one 0159 # isn't stored in the .py's mtime in the zip archive. 0160 badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:] 0161 files = {TESTMOD + ".py": (NOW, test_src), 0162 TESTMOD + pyc_ext: (NOW, badtime_pyc)} 0163 self.doTest(".py", files, TESTMOD) 0164 0165 def testPackage(self): 0166 packdir = TESTPACK + os.sep 0167 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), 0168 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)} 0169 self.doTest(pyc_ext, files, TESTPACK, TESTMOD) 0170 0171 def testDeepPackage(self): 0172 packdir = TESTPACK + os.sep 0173 packdir2 = packdir + TESTPACK2 + os.sep 0174 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), 0175 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), 0176 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} 0177 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) 0178 0179 def testGetData(self): 0180 z = ZipFile(TEMP_ZIP, "w") 0181 z.compression = self.compression 0182 try: 0183 name = "testdata.dat" 0184 data = "".join([chr(x) for x in range(256)]) * 500 0185 z.writestr(name, data) 0186 z.close() 0187 zi = zipimport.zipimporter(TEMP_ZIP) 0188 self.assertEquals(data, zi.get_data(name)) 0189 finally: 0190 z.close() 0191 os.remove(TEMP_ZIP) 0192 0193 def testImporterAttr(self): 0194 src = """if 1: # indent hack 0195 def get_file(): 0196 return __file__ 0197 if __loader__.get_data("some.data") != "some data": 0198 raise AssertionError, "bad data"\n""" 0199 pyc = make_pyc(compile(src, "<???>", "exec"), NOW) 0200 files = {TESTMOD + pyc_ext: (NOW, pyc), 0201 "some.data": (NOW, "some data")} 0202 self.doTest(pyc_ext, files, TESTMOD) 0203 0204 def testImport_WithStuff(self): 0205 # try importing from a zipfile which contains additional 0206 # stuff at the beginning of the file 0207 files = {TESTMOD + ".py": (NOW, test_src)} 0208 self.doTest(".py", files, TESTMOD, 0209 stuff="Some Stuff"*31) 0210 0211 class CompressedZipImportTestCase(UncompressedZipImportTestCase): 0212 compression = ZIP_DEFLATED 0213 0214 0215 def test_main(): 0216 test_support.run_unittest( 0217 UncompressedZipImportTestCase, 0218 CompressedZipImportTestCase 0219 ) 0220 0221 if __name__ == "__main__": 0222 test_main() 0223
Generated by PyXR 0.9.4