blob: c89aef5799f099d1ad1d6a4eecf9aed9f12710fa [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Just van Rossum52e14d62002-12-30 22:08:05 +000010from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
11
Ezio Melotti78ea2022009-09-12 18:41:20 +000012# some tests can be ran even without zlib
13try:
14 import zlib
15except ImportError:
16 zlib = None
17
18from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
19
Just van Rossum52e14d62002-12-30 22:08:05 +000020import zipimport
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021import linecache
22import doctest
23import inspect
Guido van Rossum34d19282007-08-09 01:03:29 +000024import io
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000025from traceback import extract_tb, extract_stack, print_tb
26raise_src = 'def do_raise(): raise TypeError\n'
Just van Rossum52e14d62002-12-30 22:08:05 +000027
Neal Norwitzb155b622006-01-23 07:52:13 +000028# so we only run testAFakeZlib once if this test is run repeatedly
29# which happens when we look for ref leaks
30test_imported = False
31
32
Just van Rossum52e14d62002-12-30 22:08:05 +000033def make_pyc(co, mtime):
34 data = marshal.dumps(co)
Jack Jansen472e7db2003-01-08 16:37:03 +000035 if type(mtime) is type(0.0):
Tim Petersf2715e02003-02-19 02:35:07 +000036 # Mac mtimes need a bit of special casing
37 if mtime < 0x7fffffff:
38 mtime = int(mtime)
39 else:
Guido van Rossume2a383d2007-01-15 16:59:06 +000040 mtime = int(-0x100000000 + int(mtime))
Jack Jansen472e7db2003-01-08 16:37:03 +000041 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
Just van Rossum52e14d62002-12-30 22:08:05 +000042 return pyc
43
Tim Peters68f2d002006-01-23 22:19:24 +000044def module_path_to_dotted_name(path):
45 return path.replace(os.sep, '.')
46
Just van Rossum52e14d62002-12-30 22:08:05 +000047NOW = time.time()
48test_pyc = make_pyc(test_co, NOW)
49
50
51if __debug__:
52 pyc_ext = ".pyc"
53else:
54 pyc_ext = ".pyo"
55
56
57TESTMOD = "ziptestmodule"
58TESTPACK = "ziptestpackage"
Just van Rossumd35c6db2003-01-02 12:55:48 +000059TESTPACK2 = "ziptestpackage2"
Skip Montanaro7a98be22007-08-16 14:35:24 +000060TEMP_ZIP = os.path.abspath("junk95142.zip")
Just van Rossum52e14d62002-12-30 22:08:05 +000061
Ezio Melotti78ea2022009-09-12 18:41:20 +000062
Just van Rossum52e14d62002-12-30 22:08:05 +000063class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
64
65 compression = ZIP_STORED
66
67 def setUp(self):
68 # We're reusing the zip archive path, so we must clear the
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069 # cached directory info and linecache
70 linecache.clearcache()
Just van Rossum52e14d62002-12-30 22:08:05 +000071 zipimport._zip_directory_cache.clear()
72 ImportHooksBaseTestCase.setUp(self)
73
Thomas Heller354e3d92003-07-22 18:10:15 +000074 def doTest(self, expected_ext, files, *modules, **kw):
Just van Rossum52e14d62002-12-30 22:08:05 +000075 z = ZipFile(TEMP_ZIP, "w")
76 try:
77 for name, (mtime, data) in files.items():
78 zinfo = ZipInfo(name, time.localtime(mtime))
79 zinfo.compress_type = self.compression
80 z.writestr(zinfo, data)
81 z.close()
Thomas Heller354e3d92003-07-22 18:10:15 +000082
83 stuff = kw.get("stuff", None)
84 if stuff is not None:
85 # Prepend 'stuff' to the start of the zipfile
86 f = open(TEMP_ZIP, "rb")
87 data = f.read()
88 f.close()
89
90 f = open(TEMP_ZIP, "wb")
91 f.write(stuff)
92 f.write(data)
93 f.close()
94
Just van Rossum52e14d62002-12-30 22:08:05 +000095 sys.path.insert(0, TEMP_ZIP)
96
97 mod = __import__(".".join(modules), globals(), locals(),
98 ["__dummy__"])
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000099
100 call = kw.get('call')
101 if call is not None:
102 call(mod)
103
Just van Rossum9a3129c2003-01-03 11:18:56 +0000104 if expected_ext:
105 file = mod.get_file()
106 self.assertEquals(file, os.path.join(TEMP_ZIP,
Just van Rossum6706c4d2003-01-09 22:27:10 +0000107 *modules) + expected_ext)
Just van Rossum52e14d62002-12-30 22:08:05 +0000108 finally:
109 z.close()
110 os.remove(TEMP_ZIP)
111
112 def testAFakeZlib(self):
113 #
114 # This could cause a stack overflow before: importing zlib.py
115 # from a compressed archive would cause zlib to be imported
116 # which would find zlib.py in the archive, which would... etc.
117 #
118 # This test *must* be executed first: it must be the first one
119 # to trigger zipimport to import zlib (zipimport caches the
120 # zlib.decompress function object, after which the problem being
121 # tested here wouldn't be a problem anymore...
122 # (Hence the 'A' in the test method name: to make it the first
123 # item in a list sorted by name, like unittest.makeSuite() does.)
124 #
Just van Rossum59498542003-11-18 23:00:55 +0000125 # This test fails on platforms on which the zlib module is
126 # statically linked, but the problem it tests for can't
127 # occur in that case (builtin modules are always found first),
128 # so we'll simply skip it then. Bug #765456.
129 #
130 if "zlib" in sys.builtin_module_names:
131 return
Just van Rossum52e14d62002-12-30 22:08:05 +0000132 if "zlib" in sys.modules:
133 del sys.modules["zlib"]
134 files = {"zlib.py": (NOW, test_src)}
135 try:
136 self.doTest(".py", files, "zlib")
137 except ImportError:
138 if self.compression != ZIP_DEFLATED:
139 self.fail("expected test to not raise ImportError")
140 else:
141 if self.compression != ZIP_STORED:
142 self.fail("expected test to raise ImportError")
143
144 def testPy(self):
145 files = {TESTMOD + ".py": (NOW, test_src)}
146 self.doTest(".py", files, TESTMOD)
147
148 def testPyc(self):
149 files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
150 self.doTest(pyc_ext, files, TESTMOD)
151
152 def testBoth(self):
153 files = {TESTMOD + ".py": (NOW, test_src),
154 TESTMOD + pyc_ext: (NOW, test_pyc)}
155 self.doTest(pyc_ext, files, TESTMOD)
156
Just van Rossum9a3129c2003-01-03 11:18:56 +0000157 def testEmptyPy(self):
158 files = {TESTMOD + ".py": (NOW, "")}
159 self.doTest(None, files, TESTMOD)
160
Just van Rossum52e14d62002-12-30 22:08:05 +0000161 def testBadMagic(self):
162 # make pyc magic word invalid, forcing loading from .py
Guido van Rossum254348e2007-11-21 19:29:53 +0000163 badmagic_pyc = bytearray(test_pyc)
Guido van Rossumad8d3002007-08-03 18:40:49 +0000164 badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
Just van Rossum52e14d62002-12-30 22:08:05 +0000165 files = {TESTMOD + ".py": (NOW, test_src),
166 TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
167 self.doTest(".py", files, TESTMOD)
168
169 def testBadMagic2(self):
170 # make pyc magic word invalid, causing an ImportError
Guido van Rossum254348e2007-11-21 19:29:53 +0000171 badmagic_pyc = bytearray(test_pyc)
Guido van Rossumad8d3002007-08-03 18:40:49 +0000172 badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
Just van Rossum52e14d62002-12-30 22:08:05 +0000173 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
174 try:
175 self.doTest(".py", files, TESTMOD)
176 except ImportError:
177 pass
178 else:
179 self.fail("expected ImportError; import from bad pyc")
180
181 def testBadMTime(self):
Guido van Rossum254348e2007-11-21 19:29:53 +0000182 badtime_pyc = bytearray(test_pyc)
Guido van Rossumad8d3002007-08-03 18:40:49 +0000183 badtime_pyc[7] ^= 0x02 # flip the second bit -- not the first as that one
184 # isn't stored in the .py's mtime in the zip archive.
Just van Rossum52e14d62002-12-30 22:08:05 +0000185 files = {TESTMOD + ".py": (NOW, test_src),
186 TESTMOD + pyc_ext: (NOW, badtime_pyc)}
187 self.doTest(".py", files, TESTMOD)
188
189 def testPackage(self):
190 packdir = TESTPACK + os.sep
191 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
192 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
193 self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
194
195 def testDeepPackage(self):
196 packdir = TESTPACK + os.sep
Just van Rossumd35c6db2003-01-02 12:55:48 +0000197 packdir2 = packdir + TESTPACK2 + os.sep
Just van Rossum52e14d62002-12-30 22:08:05 +0000198 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
199 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
200 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
Just van Rossumd35c6db2003-01-02 12:55:48 +0000201 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
Just van Rossum52e14d62002-12-30 22:08:05 +0000202
Neal Norwitzb155b622006-01-23 07:52:13 +0000203 def testZipImporterMethods(self):
204 packdir = TESTPACK + os.sep
205 packdir2 = packdir + TESTPACK2 + os.sep
206 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
207 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
208 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
209
210 z = ZipFile(TEMP_ZIP, "w")
211 try:
212 for name, (mtime, data) in files.items():
213 zinfo = ZipInfo(name, time.localtime(mtime))
214 zinfo.compress_type = self.compression
215 z.writestr(zinfo, data)
216 z.close()
217
218 zi = zipimport.zipimporter(TEMP_ZIP)
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000219 self.assertEquals(zi.archive, TEMP_ZIP)
Neal Norwitzb155b622006-01-23 07:52:13 +0000220 self.assertEquals(zi.is_package(TESTPACK), True)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000221 mod = zi.load_module(TESTPACK)
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000222 self.assertEquals(zi.get_filename(TESTPACK), mod.__file__)
Tim Petersbc29c1a2006-01-23 21:28:42 +0000223
Neal Norwitzb155b622006-01-23 07:52:13 +0000224 self.assertEquals(zi.is_package(packdir + '__init__'), False)
225 self.assertEquals(zi.is_package(packdir + TESTPACK2), True)
226 self.assertEquals(zi.is_package(packdir2 + TESTMOD), False)
227
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000228 mod_path = packdir2 + TESTMOD
229 mod_name = module_path_to_dotted_name(mod_path)
Georg Brandl89fad142010-03-14 10:23:39 +0000230 __import__(mod_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000231 mod = sys.modules[mod_name]
Neal Norwitzb155b622006-01-23 07:52:13 +0000232 self.assertEquals(zi.get_source(TESTPACK), None)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000233 self.assertEquals(zi.get_source(mod_path), None)
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000234 self.assertEquals(zi.get_filename(mod_path), mod.__file__)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000235 # To pass in the module name instead of the path, we must use the right importer
236 loader = mod.__loader__
237 self.assertEquals(loader.get_source(mod_name), None)
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000238 self.assertEquals(loader.get_filename(mod_name), mod.__file__)
Christian Heimes7f044312008-01-06 17:05:40 +0000239
240 # test prefix and archivepath members
241 zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
242 self.assertEquals(zi2.archive, TEMP_ZIP)
243 self.assertEquals(zi2.prefix, TESTPACK + os.sep)
Neal Norwitzb155b622006-01-23 07:52:13 +0000244 finally:
245 z.close()
246 os.remove(TEMP_ZIP)
247
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000248 def testZipImporterMethodsInSubDirectory(self):
249 packdir = TESTPACK + os.sep
250 packdir2 = packdir + TESTPACK2 + os.sep
251 files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
252 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
253
254 z = ZipFile(TEMP_ZIP, "w")
255 try:
256 for name, (mtime, data) in files.items():
257 zinfo = ZipInfo(name, time.localtime(mtime))
258 zinfo.compress_type = self.compression
259 z.writestr(zinfo, data)
260 z.close()
261
262 zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
263 self.assertEquals(zi.archive, TEMP_ZIP)
264 self.assertEquals(zi.prefix, packdir)
265 self.assertEquals(zi.is_package(TESTPACK2), True)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000266 mod = zi.load_module(TESTPACK2)
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000267 self.assertEquals(zi.get_filename(TESTPACK2), mod.__file__)
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000268
269 self.assertEquals(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
270 self.assertEquals(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
271
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000272 mod_path = TESTPACK2 + os.sep + TESTMOD
273 mod_name = module_path_to_dotted_name(mod_path)
Georg Brandl89fad142010-03-14 10:23:39 +0000274 __import__(mod_name)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000275 mod = sys.modules[mod_name]
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000276 self.assertEquals(zi.get_source(TESTPACK2), None)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000277 self.assertEquals(zi.get_source(mod_path), None)
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000278 self.assertEquals(zi.get_filename(mod_path), mod.__file__)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000279 # To pass in the module name instead of the path, we must use the right importer
280 loader = mod.__loader__
281 self.assertEquals(loader.get_source(mod_name), None)
Nick Coghlan9a1d6e32009-02-08 03:37:27 +0000282 self.assertEquals(loader.get_filename(mod_name), mod.__file__)
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +0000283 finally:
284 z.close()
285 os.remove(TEMP_ZIP)
286
Just van Rossum52e14d62002-12-30 22:08:05 +0000287 def testGetData(self):
288 z = ZipFile(TEMP_ZIP, "w")
289 z.compression = self.compression
290 try:
291 name = "testdata.dat"
Guido van Rossumad8d3002007-08-03 18:40:49 +0000292 data = bytes(x for x in range(256))
Just van Rossum52e14d62002-12-30 22:08:05 +0000293 z.writestr(name, data)
294 z.close()
295 zi = zipimport.zipimporter(TEMP_ZIP)
296 self.assertEquals(data, zi.get_data(name))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000297 self.assertIn('zipimporter object', repr(zi))
Just van Rossum52e14d62002-12-30 22:08:05 +0000298 finally:
299 z.close()
300 os.remove(TEMP_ZIP)
301
302 def testImporterAttr(self):
303 src = """if 1: # indent hack
304 def get_file():
305 return __file__
Guido van Rossumad8d3002007-08-03 18:40:49 +0000306 if __loader__.get_data("some.data") != b"some data":
Collin Winter828f04a2007-08-31 00:04:24 +0000307 raise AssertionError("bad data")\n"""
Just van Rossum52e14d62002-12-30 22:08:05 +0000308 pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
309 files = {TESTMOD + pyc_ext: (NOW, pyc),
310 "some.data": (NOW, "some data")}
311 self.doTest(pyc_ext, files, TESTMOD)
312
Thomas Heller354e3d92003-07-22 18:10:15 +0000313 def testImport_WithStuff(self):
314 # try importing from a zipfile which contains additional
315 # stuff at the beginning of the file
316 files = {TESTMOD + ".py": (NOW, test_src)}
317 self.doTest(".py", files, TESTMOD,
Guido van Rossum85825dc2007-08-27 17:03:28 +0000318 stuff=b"Some Stuff"*31)
Just van Rossum52e14d62002-12-30 22:08:05 +0000319
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 def assertModuleSource(self, module):
321 self.assertEqual(inspect.getsource(module), test_src)
322
323 def testGetSource(self):
324 files = {TESTMOD + ".py": (NOW, test_src)}
325 self.doTest(".py", files, TESTMOD, call=self.assertModuleSource)
326
327 def testGetCompiledSource(self):
328 pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)
329 files = {TESTMOD + ".py": (NOW, test_src),
330 TESTMOD + pyc_ext: (NOW, pyc)}
331 self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource)
332
333 def runDoctest(self, callback):
334 files = {TESTMOD + ".py": (NOW, test_src),
335 "xyz.txt": (NOW, ">>> log.append(True)\n")}
336 self.doTest(".py", files, TESTMOD, call=callback)
337
338 def doDoctestFile(self, module):
339 log = []
340 old_master, doctest.master = doctest.master, None
341 try:
342 doctest.testfile(
343 'xyz.txt', package=module, module_relative=True,
344 globs=locals()
345 )
346 finally:
347 doctest.master = old_master
348 self.assertEqual(log,[True])
349
350 def testDoctestFile(self):
351 self.runDoctest(self.doDoctestFile)
352
353 def doDoctestSuite(self, module):
354 log = []
355 doctest.DocFileTest(
356 'xyz.txt', package=module, module_relative=True,
357 globs=locals()
358 ).run()
359 self.assertEqual(log,[True])
360
361 def testDoctestSuite(self):
362 self.runDoctest(self.doDoctestSuite)
363
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 def doTraceback(self, module):
365 try:
366 module.do_raise()
367 except:
368 tb = sys.exc_info()[2].tb_next
369
370 f,lno,n,line = extract_tb(tb, 1)[0]
371 self.assertEqual(line, raise_src.strip())
372
373 f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]
374 self.assertEqual(line, raise_src.strip())
375
Guido van Rossum34d19282007-08-09 01:03:29 +0000376 s = io.StringIO()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377 print_tb(tb, 1, s)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000378 self.assertTrue(s.getvalue().endswith(raise_src))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 else:
380 raise AssertionError("This ought to be impossible")
381
382 def testTraceback(self):
383 files = {TESTMOD + ".py": (NOW, raise_src)}
384 self.doTest(None, files, TESTMOD, call=self.doTraceback)
385
386
Ezio Melotti78ea2022009-09-12 18:41:20 +0000387@unittest.skipUnless(zlib, "requires zlib")
Just van Rossum52e14d62002-12-30 22:08:05 +0000388class CompressedZipImportTestCase(UncompressedZipImportTestCase):
389 compression = ZIP_DEFLATED
390
391
Neal Norwitzb155b622006-01-23 07:52:13 +0000392class BadFileZipImportTestCase(unittest.TestCase):
393 def assertZipFailure(self, filename):
394 self.assertRaises(zipimport.ZipImportError,
395 zipimport.zipimporter, filename)
396
397 def testNoFile(self):
398 self.assertZipFailure('AdfjdkFJKDFJjdklfjs')
399
400 def testEmptyFilename(self):
401 self.assertZipFailure('')
402
403 def testBadArgs(self):
404 self.assertRaises(TypeError, zipimport.zipimporter, None)
405 self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None)
406
407 def testFilenameTooLong(self):
408 self.assertZipFailure('A' * 33000)
409
410 def testEmptyFile(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000411 support.unlink(TESTMOD)
Neal Norwitzb155b622006-01-23 07:52:13 +0000412 open(TESTMOD, 'w+').close()
413 self.assertZipFailure(TESTMOD)
414
415 def testFileUnreadable(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000416 support.unlink(TESTMOD)
Neal Norwitzb155b622006-01-23 07:52:13 +0000417 fd = os.open(TESTMOD, os.O_CREAT, 000)
Tim Peters68f2d002006-01-23 22:19:24 +0000418 try:
419 os.close(fd)
420 self.assertZipFailure(TESTMOD)
421 finally:
422 # If we leave "the read-only bit" set on Windows, nothing can
423 # delete TESTMOD, and later tests suffer bogus failures.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000424 os.chmod(TESTMOD, 0o666)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000425 support.unlink(TESTMOD)
Neal Norwitzb155b622006-01-23 07:52:13 +0000426
427 def testNotZipFile(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000428 support.unlink(TESTMOD)
Neal Norwitzb155b622006-01-23 07:52:13 +0000429 fp = open(TESTMOD, 'w+')
430 fp.write('a' * 22)
431 fp.close()
432 self.assertZipFailure(TESTMOD)
433
Neal Norwitzdbc95f42006-01-23 08:48:03 +0000434 # XXX: disabled until this works on Big-endian machines
435 def _testBogusZipFile(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000436 support.unlink(TESTMOD)
Neal Norwitzb155b622006-01-23 07:52:13 +0000437 fp = open(TESTMOD, 'w+')
438 fp.write(struct.pack('=I', 0x06054B50))
439 fp.write('a' * 18)
440 fp.close()
441 z = zipimport.zipimporter(TESTMOD)
442
443 try:
444 self.assertRaises(TypeError, z.find_module, None)
445 self.assertRaises(TypeError, z.load_module, None)
446 self.assertRaises(TypeError, z.is_package, None)
447 self.assertRaises(TypeError, z.get_code, None)
448 self.assertRaises(TypeError, z.get_data, None)
449 self.assertRaises(TypeError, z.get_source, None)
450
451 error = zipimport.ZipImportError
452 self.assertEqual(z.find_module('abc'), None)
453
454 self.assertRaises(error, z.load_module, 'abc')
455 self.assertRaises(error, z.get_code, 'abc')
456 self.assertRaises(IOError, z.get_data, 'abc')
457 self.assertRaises(error, z.get_source, 'abc')
458 self.assertRaises(error, z.is_package, 'abc')
459 finally:
460 zipimport._zip_directory_cache.clear()
461
462
463def cleanup():
464 # this is necessary if test is run repeated (like when finding leaks)
465 global test_imported
466 if test_imported:
467 zipimport._zip_directory_cache.clear()
468 if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'):
469 delattr(UncompressedZipImportTestCase, 'testAFakeZlib')
470 if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'):
471 delattr(CompressedZipImportTestCase, 'testAFakeZlib')
472 test_imported = True
473
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000474def test_main():
Neal Norwitzb155b622006-01-23 07:52:13 +0000475 cleanup()
476 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000477 support.run_unittest(
Neal Norwitzb155b622006-01-23 07:52:13 +0000478 UncompressedZipImportTestCase,
479 CompressedZipImportTestCase,
480 BadFileZipImportTestCase,
481 )
482 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000483 support.unlink(TESTMOD)
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000484
485if __name__ == "__main__":
486 test_main()