blob: 86acdd20918a669a54adb9393f2a44f24884e649 [file] [log] [blame]
Just van Rossum52e14d62002-12-30 22:08:05 +00001import sys
2import os
3import marshal
4import imp
5import struct
6import time
7
8import zlib # implied prerequisite
9from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
10from test import test_support
11from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
12
13import zipimport
14
15
16def make_pyc(co, mtime):
17 data = marshal.dumps(co)
Jack Jansen472e7db2003-01-08 16:37:03 +000018 if type(mtime) is type(0.0):
Tim Petersf2715e02003-02-19 02:35:07 +000019 # Mac mtimes need a bit of special casing
20 if mtime < 0x7fffffff:
21 mtime = int(mtime)
22 else:
23 mtime = int(-0x100000000L + long(mtime))
Jack Jansen472e7db2003-01-08 16:37:03 +000024 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
Just van Rossum52e14d62002-12-30 22:08:05 +000025 return pyc
26
27NOW = time.time()
28test_pyc = make_pyc(test_co, NOW)
29
30
31if __debug__:
32 pyc_ext = ".pyc"
33else:
34 pyc_ext = ".pyo"
35
36
37TESTMOD = "ziptestmodule"
38TESTPACK = "ziptestpackage"
Just van Rossumd35c6db2003-01-02 12:55:48 +000039TESTPACK2 = "ziptestpackage2"
Martin v. Löwisa94568a2003-05-10 07:36:56 +000040TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip")
Just van Rossum52e14d62002-12-30 22:08:05 +000041
42class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
43
44 compression = ZIP_STORED
45
46 def setUp(self):
47 # We're reusing the zip archive path, so we must clear the
48 # cached directory info.
49 zipimport._zip_directory_cache.clear()
50 ImportHooksBaseTestCase.setUp(self)
51
Thomas Heller354e3d92003-07-22 18:10:15 +000052 def doTest(self, expected_ext, files, *modules, **kw):
Just van Rossum52e14d62002-12-30 22:08:05 +000053 z = ZipFile(TEMP_ZIP, "w")
54 try:
55 for name, (mtime, data) in files.items():
56 zinfo = ZipInfo(name, time.localtime(mtime))
57 zinfo.compress_type = self.compression
58 z.writestr(zinfo, data)
59 z.close()
Thomas Heller354e3d92003-07-22 18:10:15 +000060
61 stuff = kw.get("stuff", None)
62 if stuff is not None:
63 # Prepend 'stuff' to the start of the zipfile
64 f = open(TEMP_ZIP, "rb")
65 data = f.read()
66 f.close()
67
68 f = open(TEMP_ZIP, "wb")
69 f.write(stuff)
70 f.write(data)
71 f.close()
72
Just van Rossum52e14d62002-12-30 22:08:05 +000073 sys.path.insert(0, TEMP_ZIP)
74
75 mod = __import__(".".join(modules), globals(), locals(),
76 ["__dummy__"])
Just van Rossum9a3129c2003-01-03 11:18:56 +000077 if expected_ext:
78 file = mod.get_file()
79 self.assertEquals(file, os.path.join(TEMP_ZIP,
Just van Rossum6706c4d2003-01-09 22:27:10 +000080 *modules) + expected_ext)
Just van Rossum52e14d62002-12-30 22:08:05 +000081 finally:
82 z.close()
83 os.remove(TEMP_ZIP)
84
85 def testAFakeZlib(self):
86 #
87 # This could cause a stack overflow before: importing zlib.py
88 # from a compressed archive would cause zlib to be imported
89 # which would find zlib.py in the archive, which would... etc.
90 #
91 # This test *must* be executed first: it must be the first one
92 # to trigger zipimport to import zlib (zipimport caches the
93 # zlib.decompress function object, after which the problem being
94 # tested here wouldn't be a problem anymore...
95 # (Hence the 'A' in the test method name: to make it the first
96 # item in a list sorted by name, like unittest.makeSuite() does.)
97 #
98 if "zlib" in sys.modules:
99 del sys.modules["zlib"]
100 files = {"zlib.py": (NOW, test_src)}
101 try:
102 self.doTest(".py", files, "zlib")
103 except ImportError:
104 if self.compression != ZIP_DEFLATED:
105 self.fail("expected test to not raise ImportError")
106 else:
107 if self.compression != ZIP_STORED:
108 self.fail("expected test to raise ImportError")
109
110 def testPy(self):
111 files = {TESTMOD + ".py": (NOW, test_src)}
112 self.doTest(".py", files, TESTMOD)
113
114 def testPyc(self):
115 files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
116 self.doTest(pyc_ext, files, TESTMOD)
117
118 def testBoth(self):
119 files = {TESTMOD + ".py": (NOW, test_src),
120 TESTMOD + pyc_ext: (NOW, test_pyc)}
121 self.doTest(pyc_ext, files, TESTMOD)
122
Just van Rossum9a3129c2003-01-03 11:18:56 +0000123 def testEmptyPy(self):
124 files = {TESTMOD + ".py": (NOW, "")}
125 self.doTest(None, files, TESTMOD)
126
Just van Rossum52e14d62002-12-30 22:08:05 +0000127 def testBadMagic(self):
128 # make pyc magic word invalid, forcing loading from .py
129 m0 = ord(test_pyc[0])
130 m0 ^= 0x04 # flip an arbitrary bit
131 badmagic_pyc = chr(m0) + test_pyc[1:]
132 files = {TESTMOD + ".py": (NOW, test_src),
133 TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
134 self.doTest(".py", files, TESTMOD)
135
136 def testBadMagic2(self):
137 # make pyc magic word invalid, causing an ImportError
138 m0 = ord(test_pyc[0])
139 m0 ^= 0x04 # flip an arbitrary bit
140 badmagic_pyc = chr(m0) + test_pyc[1:]
141 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
142 try:
143 self.doTest(".py", files, TESTMOD)
144 except ImportError:
145 pass
146 else:
147 self.fail("expected ImportError; import from bad pyc")
148
149 def testBadMTime(self):
150 t3 = ord(test_pyc[7])
151 t3 ^= 0x02 # flip the second bit -- not the first as that one
152 # isn't stored in the .py's mtime in the zip archive.
153 badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]
154 files = {TESTMOD + ".py": (NOW, test_src),
155 TESTMOD + pyc_ext: (NOW, badtime_pyc)}
156 self.doTest(".py", files, TESTMOD)
157
158 def testPackage(self):
159 packdir = TESTPACK + os.sep
160 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
161 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
162 self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
163
164 def testDeepPackage(self):
165 packdir = TESTPACK + os.sep
Just van Rossumd35c6db2003-01-02 12:55:48 +0000166 packdir2 = packdir + TESTPACK2 + os.sep
Just van Rossum52e14d62002-12-30 22:08:05 +0000167 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
168 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
169 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
Just van Rossumd35c6db2003-01-02 12:55:48 +0000170 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
Just van Rossum52e14d62002-12-30 22:08:05 +0000171
172 def testGetData(self):
173 z = ZipFile(TEMP_ZIP, "w")
174 z.compression = self.compression
175 try:
176 name = "testdata.dat"
177 data = "".join([chr(x) for x in range(256)]) * 500
178 z.writestr(name, data)
179 z.close()
180 zi = zipimport.zipimporter(TEMP_ZIP)
181 self.assertEquals(data, zi.get_data(name))
182 finally:
183 z.close()
184 os.remove(TEMP_ZIP)
185
186 def testImporterAttr(self):
187 src = """if 1: # indent hack
188 def get_file():
189 return __file__
Just van Rossumd35c6db2003-01-02 12:55:48 +0000190 if __loader__.get_data("some.data") != "some data":
Just van Rossum52e14d62002-12-30 22:08:05 +0000191 raise AssertionError, "bad data"\n"""
192 pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
193 files = {TESTMOD + pyc_ext: (NOW, pyc),
194 "some.data": (NOW, "some data")}
195 self.doTest(pyc_ext, files, TESTMOD)
196
Thomas Heller354e3d92003-07-22 18:10:15 +0000197 def testImport_WithStuff(self):
198 # try importing from a zipfile which contains additional
199 # stuff at the beginning of the file
200 files = {TESTMOD + ".py": (NOW, test_src)}
201 self.doTest(".py", files, TESTMOD,
202 stuff="Some Stuff"*31)
Just van Rossum52e14d62002-12-30 22:08:05 +0000203
204class CompressedZipImportTestCase(UncompressedZipImportTestCase):
205 compression = ZIP_DEFLATED
206
207
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000208def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000209 test_support.run_unittest(
210 UncompressedZipImportTestCase,
211 CompressedZipImportTestCase
212 )
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000213
214if __name__ == "__main__":
215 test_main()