blob: c7da859e6e33b5d3b33a0e21e21442b0625bbcf7 [file] [log] [blame]
Just van Rossum52e14d62002-12-30 22:08:05 +00001import sys
2import os
3import marshal
4import imp
5import struct
6import time
Neal Norwitzb155b622006-01-23 07:52:13 +00007import unittest
Just van Rossum52e14d62002-12-30 22:08:05 +00008
9import zlib # implied prerequisite
10from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
11from test import test_support
12from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
13
14import zipimport
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015import linecache
16import doctest
17import inspect
Guido van Rossum34d19282007-08-09 01:03:29 +000018import io
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019from traceback import extract_tb, extract_stack, print_tb
20raise_src = 'def do_raise(): raise TypeError\n'
Just van Rossum52e14d62002-12-30 22:08:05 +000021
Neal Norwitzb155b622006-01-23 07:52:13 +000022# so we only run testAFakeZlib once if this test is run repeatedly
23# which happens when we look for ref leaks
24test_imported = False
25
26
Just van Rossum52e14d62002-12-30 22:08:05 +000027def make_pyc(co, mtime):
28 data = marshal.dumps(co)
Jack Jansen472e7db2003-01-08 16:37:03 +000029 if type(mtime) is type(0.0):
Tim Petersf2715e02003-02-19 02:35:07 +000030 # Mac mtimes need a bit of special casing
31 if mtime < 0x7fffffff:
32 mtime = int(mtime)
33 else:
Guido van Rossume2a383d2007-01-15 16:59:06 +000034 mtime = int(-0x100000000 + int(mtime))
Jack Jansen472e7db2003-01-08 16:37:03 +000035 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
Just van Rossum52e14d62002-12-30 22:08:05 +000036 return pyc
37
Tim Peters68f2d002006-01-23 22:19:24 +000038def module_path_to_dotted_name(path):
39 return path.replace(os.sep, '.')
40
Just van Rossum52e14d62002-12-30 22:08:05 +000041NOW = time.time()
42test_pyc = make_pyc(test_co, NOW)
43
44
45if __debug__:
46 pyc_ext = ".pyc"
47else:
48 pyc_ext = ".pyo"
49
50
51TESTMOD = "ziptestmodule"
52TESTPACK = "ziptestpackage"
Just van Rossumd35c6db2003-01-02 12:55:48 +000053TESTPACK2 = "ziptestpackage2"
Skip Montanaro7a98be22007-08-16 14:35:24 +000054TEMP_ZIP = os.path.abspath("junk95142.zip")
Just van Rossum52e14d62002-12-30 22:08:05 +000055
56class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
57
58 compression = ZIP_STORED
59
60 def setUp(self):
61 # We're reusing the zip archive path, so we must clear the
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062 # cached directory info and linecache
63 linecache.clearcache()
Just van Rossum52e14d62002-12-30 22:08:05 +000064 zipimport._zip_directory_cache.clear()
65 ImportHooksBaseTestCase.setUp(self)
66
Thomas Heller354e3d92003-07-22 18:10:15 +000067 def doTest(self, expected_ext, files, *modules, **kw):
Just van Rossum52e14d62002-12-30 22:08:05 +000068 z = ZipFile(TEMP_ZIP, "w")
69 try:
70 for name, (mtime, data) in files.items():
71 zinfo = ZipInfo(name, time.localtime(mtime))
72 zinfo.compress_type = self.compression
73 z.writestr(zinfo, data)
74 z.close()
Thomas Heller354e3d92003-07-22 18:10:15 +000075
76 stuff = kw.get("stuff", None)
77 if stuff is not None:
78 # Prepend 'stuff' to the start of the zipfile
79 f = open(TEMP_ZIP, "rb")
80 data = f.read()
81 f.close()
82
83 f = open(TEMP_ZIP, "wb")
84 f.write(stuff)
85 f.write(data)
86 f.close()
87
Just van Rossum52e14d62002-12-30 22:08:05 +000088 sys.path.insert(0, TEMP_ZIP)
89
90 mod = __import__(".".join(modules), globals(), locals(),
91 ["__dummy__"])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092
93 call = kw.get('call')
94 if call is not None:
95 call(mod)
96
Just van Rossum9a3129c2003-01-03 11:18:56 +000097 if expected_ext:
98 file = mod.get_file()
99 self.assertEquals(file, os.path.join(TEMP_ZIP,
Just van Rossum6706c4d2003-01-09 22:27:10 +0000100 *modules) + expected_ext)
Just van Rossum52e14d62002-12-30 22:08:05 +0000101 finally:
102 z.close()
103 os.remove(TEMP_ZIP)
104
105 def testAFakeZlib(self):
106 #
107 # This could cause a stack overflow before: importing zlib.py
108 # from a compressed archive would cause zlib to be imported
109 # which would find zlib.py in the archive, which would... etc.
110 #
111 # This test *must* be executed first: it must be the first one
112 # to trigger zipimport to import zlib (zipimport caches the
113 # zlib.decompress function object, after which the problem being
114 # tested here wouldn't be a problem anymore...
115 # (Hence the 'A' in the test method name: to make it the first
116 # item in a list sorted by name, like unittest.makeSuite() does.)
117 #
Just van Rossum59498542003-11-18 23:00:55 +0000118 # This test fails on platforms on which the zlib module is
119 # statically linked, but the problem it tests for can't
120 # occur in that case (builtin modules are always found first),
121 # so we'll simply skip it then. Bug #765456.
122 #
123 if "zlib" in sys.builtin_module_names:
124 return
Just van Rossum52e14d62002-12-30 22:08:05 +0000125 if "zlib" in sys.modules:
126 del sys.modules["zlib"]
127 files = {"zlib.py": (NOW, test_src)}
128 try:
129 self.doTest(".py", files, "zlib")
130 except ImportError:
131 if self.compression != ZIP_DEFLATED:
132 self.fail("expected test to not raise ImportError")
133 else:
134 if self.compression != ZIP_STORED:
135 self.fail("expected test to raise ImportError")
136
137 def testPy(self):
138 files = {TESTMOD + ".py": (NOW, test_src)}
139 self.doTest(".py", files, TESTMOD)
140
141 def testPyc(self):
142 files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
143 self.doTest(pyc_ext, files, TESTMOD)
144
145 def testBoth(self):
146 files = {TESTMOD + ".py": (NOW, test_src),
147 TESTMOD + pyc_ext: (NOW, test_pyc)}
148 self.doTest(pyc_ext, files, TESTMOD)
149
Just van Rossum9a3129c2003-01-03 11:18:56 +0000150 def testEmptyPy(self):
151 files = {TESTMOD + ".py": (NOW, "")}
152 self.doTest(None, files, TESTMOD)
153
Just van Rossum52e14d62002-12-30 22:08:05 +0000154 def testBadMagic(self):
155 # make pyc magic word invalid, forcing loading from .py
Guido van Rossum254348e2007-11-21 19:29:53 +0000156 badmagic_pyc = bytearray(test_pyc)
Guido van Rossumad8d3002007-08-03 18:40:49 +0000157 badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
Just van Rossum52e14d62002-12-30 22:08:05 +0000158 files = {TESTMOD + ".py": (NOW, test_src),
159 TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
160 self.doTest(".py", files, TESTMOD)
161
162 def testBadMagic2(self):
163 # make pyc magic word invalid, causing an ImportError
Guido van Rossum254348e2007-11-21 19:29:53 +0000164 badmagic_pyc = bytearray(test_pyc)
Guido van Rossumad8d3002007-08-03 18:40:49 +0000165 badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
Just van Rossum52e14d62002-12-30 22:08:05 +0000166 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
167 try:
168 self.doTest(".py", files, TESTMOD)
169 except ImportError:
170 pass
171 else:
172 self.fail("expected ImportError; import from bad pyc")
173
174 def testBadMTime(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000175 badtime_pyc = bytearray(test_pyc)
Guido van Rossumad8d3002007-08-03 18:40:49 +0000176 badtime_pyc[7] ^= 0x02 # flip the second bit -- not the first as that one
177 # isn't stored in the .py's mtime in the zip archive.
Just van Rossum52e14d62002-12-30 22:08:05 +0000178 files = {TESTMOD + ".py": (NOW, test_src),
179 TESTMOD + pyc_ext: (NOW, badtime_pyc)}
180 self.doTest(".py", files, TESTMOD)
181
182 def testPackage(self):
183 packdir = TESTPACK + os.sep
184 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
185 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
186 self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
187
188 def testDeepPackage(self):
189 packdir = TESTPACK + os.sep
Just van Rossumd35c6db2003-01-02 12:55:48 +0000190 packdir2 = packdir + TESTPACK2 + os.sep
Just van Rossum52e14d62002-12-30 22:08:05 +0000191 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
192 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
193 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
Just van Rossumd35c6db2003-01-02 12:55:48 +0000194 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
Just van Rossum52e14d62002-12-30 22:08:05 +0000195
Neal Norwitzb155b622006-01-23 07:52:13 +0000196 def testZipImporterMethods(self):
197 packdir = TESTPACK + os.sep
198 packdir2 = packdir + TESTPACK2 + os.sep
199 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
200 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
201 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
202
203 z = ZipFile(TEMP_ZIP, "w")
204 try:
205 for name, (mtime, data) in files.items():
206 zinfo = ZipInfo(name, time.localtime(mtime))
207 zinfo.compress_type = self.compression
208 z.writestr(zinfo, data)
209 z.close()
210
211 zi = zipimport.zipimporter(TEMP_ZIP)
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000212 self.assertEquals(zi.archive, TEMP_ZIP)
Neal Norwitzb155b622006-01-23 07:52:13 +0000213 self.assertEquals(zi.is_package(TESTPACK), True)
214 zi.load_module(TESTPACK)
Tim Petersbc29c1a2006-01-23 21:28:42 +0000215
Neal Norwitzb155b622006-01-23 07:52:13 +0000216 self.assertEquals(zi.is_package(packdir + '__init__'), False)
217 self.assertEquals(zi.is_package(packdir + TESTPACK2), True)
218 self.assertEquals(zi.is_package(packdir2 + TESTMOD), False)
219
220 mod_name = packdir2 + TESTMOD
Tim Peters68f2d002006-01-23 22:19:24 +0000221 mod = __import__(module_path_to_dotted_name(mod_name))
Neal Norwitzb155b622006-01-23 07:52:13 +0000222 self.assertEquals(zi.get_source(TESTPACK), None)
223 self.assertEquals(zi.get_source(mod_name), None)
Christian Heimes7f044312008-01-06 17:05:40 +0000224
225 # test prefix and archivepath members
226 zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
227 self.assertEquals(zi2.archive, TEMP_ZIP)
228 self.assertEquals(zi2.prefix, TESTPACK + os.sep)
Neal Norwitzb155b622006-01-23 07:52:13 +0000229 finally:
230 z.close()
231 os.remove(TEMP_ZIP)
232
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000233 def testZipImporterMethodsInSubDirectory(self):
234 packdir = TESTPACK + os.sep
235 packdir2 = packdir + TESTPACK2 + os.sep
236 files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
237 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
238
239 z = ZipFile(TEMP_ZIP, "w")
240 try:
241 for name, (mtime, data) in files.items():
242 zinfo = ZipInfo(name, time.localtime(mtime))
243 zinfo.compress_type = self.compression
244 z.writestr(zinfo, data)
245 z.close()
246
247 zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
248 self.assertEquals(zi.archive, TEMP_ZIP)
249 self.assertEquals(zi.prefix, packdir)
250 self.assertEquals(zi.is_package(TESTPACK2), True)
251 zi.load_module(TESTPACK2)
252
253 self.assertEquals(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
254 self.assertEquals(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
255
256 mod_name = TESTPACK2 + os.sep + TESTMOD
257 mod = __import__(module_path_to_dotted_name(mod_name))
258 self.assertEquals(zi.get_source(TESTPACK2), None)
259 self.assertEquals(zi.get_source(mod_name), None)
260 finally:
261 z.close()
262 os.remove(TEMP_ZIP)
263
Just van Rossum52e14d62002-12-30 22:08:05 +0000264 def testGetData(self):
265 z = ZipFile(TEMP_ZIP, "w")
266 z.compression = self.compression
267 try:
268 name = "testdata.dat"
Guido van Rossumad8d3002007-08-03 18:40:49 +0000269 data = bytes(x for x in range(256))
Just van Rossum52e14d62002-12-30 22:08:05 +0000270 z.writestr(name, data)
271 z.close()
272 zi = zipimport.zipimporter(TEMP_ZIP)
273 self.assertEquals(data, zi.get_data(name))
Neal Norwitzb155b622006-01-23 07:52:13 +0000274 self.assert_('zipimporter object' in repr(zi))
Just van Rossum52e14d62002-12-30 22:08:05 +0000275 finally:
276 z.close()
277 os.remove(TEMP_ZIP)
278
279 def testImporterAttr(self):
280 src = """if 1: # indent hack
281 def get_file():
282 return __file__
Guido van Rossumad8d3002007-08-03 18:40:49 +0000283 if __loader__.get_data("some.data") != b"some data":
Collin Winter828f04a2007-08-31 00:04:24 +0000284 raise AssertionError("bad data")\n"""
Just van Rossum52e14d62002-12-30 22:08:05 +0000285 pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
286 files = {TESTMOD + pyc_ext: (NOW, pyc),
287 "some.data": (NOW, "some data")}
288 self.doTest(pyc_ext, files, TESTMOD)
289
Thomas Heller354e3d92003-07-22 18:10:15 +0000290 def testImport_WithStuff(self):
291 # try importing from a zipfile which contains additional
292 # stuff at the beginning of the file
293 files = {TESTMOD + ".py": (NOW, test_src)}
294 self.doTest(".py", files, TESTMOD,
Guido van Rossum85825dc2007-08-27 17:03:28 +0000295 stuff=b"Some Stuff"*31)
Just van Rossum52e14d62002-12-30 22:08:05 +0000296
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000297 def assertModuleSource(self, module):
298 self.assertEqual(inspect.getsource(module), test_src)
299
300 def testGetSource(self):
301 files = {TESTMOD + ".py": (NOW, test_src)}
302 self.doTest(".py", files, TESTMOD, call=self.assertModuleSource)
303
304 def testGetCompiledSource(self):
305 pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)
306 files = {TESTMOD + ".py": (NOW, test_src),
307 TESTMOD + pyc_ext: (NOW, pyc)}
308 self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource)
309
310 def runDoctest(self, callback):
311 files = {TESTMOD + ".py": (NOW, test_src),
312 "xyz.txt": (NOW, ">>> log.append(True)\n")}
313 self.doTest(".py", files, TESTMOD, call=callback)
314
315 def doDoctestFile(self, module):
316 log = []
317 old_master, doctest.master = doctest.master, None
318 try:
319 doctest.testfile(
320 'xyz.txt', package=module, module_relative=True,
321 globs=locals()
322 )
323 finally:
324 doctest.master = old_master
325 self.assertEqual(log,[True])
326
327 def testDoctestFile(self):
328 self.runDoctest(self.doDoctestFile)
329
330 def doDoctestSuite(self, module):
331 log = []
332 doctest.DocFileTest(
333 'xyz.txt', package=module, module_relative=True,
334 globs=locals()
335 ).run()
336 self.assertEqual(log,[True])
337
338 def testDoctestSuite(self):
339 self.runDoctest(self.doDoctestSuite)
340
341
342 def doTraceback(self, module):
343 try:
344 module.do_raise()
345 except:
346 tb = sys.exc_info()[2].tb_next
347
348 f,lno,n,line = extract_tb(tb, 1)[0]
349 self.assertEqual(line, raise_src.strip())
350
351 f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]
352 self.assertEqual(line, raise_src.strip())
353
Guido van Rossum34d19282007-08-09 01:03:29 +0000354 s = io.StringIO()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355 print_tb(tb, 1, s)
356 self.failUnless(s.getvalue().endswith(raise_src))
357 else:
358 raise AssertionError("This ought to be impossible")
359
360 def testTraceback(self):
361 files = {TESTMOD + ".py": (NOW, raise_src)}
362 self.doTest(None, files, TESTMOD, call=self.doTraceback)
363
364
Just van Rossum52e14d62002-12-30 22:08:05 +0000365class CompressedZipImportTestCase(UncompressedZipImportTestCase):
366 compression = ZIP_DEFLATED
367
368
Neal Norwitzb155b622006-01-23 07:52:13 +0000369class BadFileZipImportTestCase(unittest.TestCase):
370 def assertZipFailure(self, filename):
371 self.assertRaises(zipimport.ZipImportError,
372 zipimport.zipimporter, filename)
373
374 def testNoFile(self):
375 self.assertZipFailure('AdfjdkFJKDFJjdklfjs')
376
377 def testEmptyFilename(self):
378 self.assertZipFailure('')
379
380 def testBadArgs(self):
381 self.assertRaises(TypeError, zipimport.zipimporter, None)
382 self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None)
383
384 def testFilenameTooLong(self):
385 self.assertZipFailure('A' * 33000)
386
387 def testEmptyFile(self):
388 test_support.unlink(TESTMOD)
389 open(TESTMOD, 'w+').close()
390 self.assertZipFailure(TESTMOD)
391
392 def testFileUnreadable(self):
393 test_support.unlink(TESTMOD)
394 fd = os.open(TESTMOD, os.O_CREAT, 000)
Tim Peters68f2d002006-01-23 22:19:24 +0000395 try:
396 os.close(fd)
397 self.assertZipFailure(TESTMOD)
398 finally:
399 # If we leave "the read-only bit" set on Windows, nothing can
400 # delete TESTMOD, and later tests suffer bogus failures.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000401 os.chmod(TESTMOD, 0o666)
Tim Peters68f2d002006-01-23 22:19:24 +0000402 test_support.unlink(TESTMOD)
Neal Norwitzb155b622006-01-23 07:52:13 +0000403
404 def testNotZipFile(self):
405 test_support.unlink(TESTMOD)
406 fp = open(TESTMOD, 'w+')
407 fp.write('a' * 22)
408 fp.close()
409 self.assertZipFailure(TESTMOD)
410
Neal Norwitzdbc95f42006-01-23 08:48:03 +0000411 # XXX: disabled until this works on Big-endian machines
412 def _testBogusZipFile(self):
Neal Norwitzb155b622006-01-23 07:52:13 +0000413 test_support.unlink(TESTMOD)
414 fp = open(TESTMOD, 'w+')
415 fp.write(struct.pack('=I', 0x06054B50))
416 fp.write('a' * 18)
417 fp.close()
418 z = zipimport.zipimporter(TESTMOD)
419
420 try:
421 self.assertRaises(TypeError, z.find_module, None)
422 self.assertRaises(TypeError, z.load_module, None)
423 self.assertRaises(TypeError, z.is_package, None)
424 self.assertRaises(TypeError, z.get_code, None)
425 self.assertRaises(TypeError, z.get_data, None)
426 self.assertRaises(TypeError, z.get_source, None)
427
428 error = zipimport.ZipImportError
429 self.assertEqual(z.find_module('abc'), None)
430
431 self.assertRaises(error, z.load_module, 'abc')
432 self.assertRaises(error, z.get_code, 'abc')
433 self.assertRaises(IOError, z.get_data, 'abc')
434 self.assertRaises(error, z.get_source, 'abc')
435 self.assertRaises(error, z.is_package, 'abc')
436 finally:
437 zipimport._zip_directory_cache.clear()
438
439
440def cleanup():
441 # this is necessary if test is run repeated (like when finding leaks)
442 global test_imported
443 if test_imported:
444 zipimport._zip_directory_cache.clear()
445 if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'):
446 delattr(UncompressedZipImportTestCase, 'testAFakeZlib')
447 if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'):
448 delattr(CompressedZipImportTestCase, 'testAFakeZlib')
449 test_imported = True
450
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000451def test_main():
Neal Norwitzb155b622006-01-23 07:52:13 +0000452 cleanup()
453 try:
454 test_support.run_unittest(
455 UncompressedZipImportTestCase,
456 CompressedZipImportTestCase,
457 BadFileZipImportTestCase,
458 )
459 finally:
460 test_support.unlink(TESTMOD)
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000461
462if __name__ == "__main__":
463 test_main()