Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
| 3 | import marshal |
| 4 | import imp |
| 5 | import struct |
| 6 | import time |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 7 | import unittest |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 8 | |
| 9 | import zlib # implied prerequisite |
| 10 | from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED |
| 11 | from test import test_support |
| 12 | from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co |
| 13 | |
| 14 | import zipimport |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15 | import linecache |
| 16 | import doctest |
| 17 | import inspect |
| 18 | import StringIO |
| 19 | from traceback import extract_tb, extract_stack, print_tb |
| 20 | raise_src = 'def do_raise(): raise TypeError\n' |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 21 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 22 | # so we only run testAFakeZlib once if this test is run repeatedly |
| 23 | # which happens when we look for ref leaks |
| 24 | test_imported = False |
| 25 | |
| 26 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 27 | def make_pyc(co, mtime): |
| 28 | data = marshal.dumps(co) |
Jack Jansen | 472e7db | 2003-01-08 16:37:03 +0000 | [diff] [blame] | 29 | if type(mtime) is type(0.0): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 30 | # Mac mtimes need a bit of special casing |
| 31 | if mtime < 0x7fffffff: |
| 32 | mtime = int(mtime) |
| 33 | else: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 34 | mtime = int(-0x100000000 + int(mtime)) |
Jack Jansen | 472e7db | 2003-01-08 16:37:03 +0000 | [diff] [blame] | 35 | pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 36 | return pyc |
| 37 | |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 38 | def module_path_to_dotted_name(path): |
| 39 | return path.replace(os.sep, '.') |
| 40 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 41 | NOW = time.time() |
| 42 | test_pyc = make_pyc(test_co, NOW) |
| 43 | |
| 44 | |
| 45 | if __debug__: |
| 46 | pyc_ext = ".pyc" |
| 47 | else: |
| 48 | pyc_ext = ".pyo" |
| 49 | |
| 50 | |
| 51 | TESTMOD = "ziptestmodule" |
| 52 | TESTPACK = "ziptestpackage" |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 53 | TESTPACK2 = "ziptestpackage2" |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 54 | TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip") |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 55 | |
| 56 | class 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | # cached directory info and linecache |
| 63 | linecache.clearcache() |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 64 | zipimport._zip_directory_cache.clear() |
| 65 | ImportHooksBaseTestCase.setUp(self) |
| 66 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 67 | def doTest(self, expected_ext, files, *modules, **kw): |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 68 | 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 Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 75 | |
| 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 Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 88 | sys.path.insert(0, TEMP_ZIP) |
| 89 | |
| 90 | mod = __import__(".".join(modules), globals(), locals(), |
| 91 | ["__dummy__"]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | |
| 93 | call = kw.get('call') |
| 94 | if call is not None: |
| 95 | call(mod) |
| 96 | |
Just van Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 97 | if expected_ext: |
| 98 | file = mod.get_file() |
| 99 | self.assertEquals(file, os.path.join(TEMP_ZIP, |
Just van Rossum | 6706c4d | 2003-01-09 22:27:10 +0000 | [diff] [blame] | 100 | *modules) + expected_ext) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 101 | 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 Rossum | 5949854 | 2003-11-18 23:00:55 +0000 | [diff] [blame] | 118 | # 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 Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 125 | 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 Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 150 | def testEmptyPy(self): |
| 151 | files = {TESTMOD + ".py": (NOW, "")} |
| 152 | self.doTest(None, files, TESTMOD) |
| 153 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 154 | def testBadMagic(self): |
| 155 | # make pyc magic word invalid, forcing loading from .py |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame^] | 156 | badmagic_pyc = bytes(test_pyc) |
| 157 | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 158 | 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 Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame^] | 164 | badmagic_pyc = bytes(test_pyc) |
| 165 | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 166 | 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 Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame^] | 175 | badtime_pyc = bytes(test_pyc) |
| 176 | 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 Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 178 | 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 Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 190 | packdir2 = packdir + TESTPACK2 + os.sep |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 191 | 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 Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 194 | self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 195 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 196 | 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) |
| 212 | self.assertEquals(zi.is_package(TESTPACK), True) |
| 213 | zi.load_module(TESTPACK) |
Tim Peters | bc29c1a | 2006-01-23 21:28:42 +0000 | [diff] [blame] | 214 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 215 | self.assertEquals(zi.is_package(packdir + '__init__'), False) |
| 216 | self.assertEquals(zi.is_package(packdir + TESTPACK2), True) |
| 217 | self.assertEquals(zi.is_package(packdir2 + TESTMOD), False) |
| 218 | |
| 219 | mod_name = packdir2 + TESTMOD |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 220 | mod = __import__(module_path_to_dotted_name(mod_name)) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 221 | self.assertEquals(zi.get_source(TESTPACK), None) |
| 222 | self.assertEquals(zi.get_source(mod_name), None) |
| 223 | finally: |
| 224 | z.close() |
| 225 | os.remove(TEMP_ZIP) |
| 226 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 227 | def testGetData(self): |
| 228 | z = ZipFile(TEMP_ZIP, "w") |
| 229 | z.compression = self.compression |
| 230 | try: |
| 231 | name = "testdata.dat" |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame^] | 232 | data = bytes(x for x in range(256)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 233 | z.writestr(name, data) |
| 234 | z.close() |
| 235 | zi = zipimport.zipimporter(TEMP_ZIP) |
| 236 | self.assertEquals(data, zi.get_data(name)) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 237 | self.assert_('zipimporter object' in repr(zi)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 238 | finally: |
| 239 | z.close() |
| 240 | os.remove(TEMP_ZIP) |
| 241 | |
| 242 | def testImporterAttr(self): |
| 243 | src = """if 1: # indent hack |
| 244 | def get_file(): |
| 245 | return __file__ |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame^] | 246 | if __loader__.get_data("some.data") != b"some data": |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 247 | raise AssertionError, "bad data"\n""" |
| 248 | pyc = make_pyc(compile(src, "<???>", "exec"), NOW) |
| 249 | files = {TESTMOD + pyc_ext: (NOW, pyc), |
| 250 | "some.data": (NOW, "some data")} |
| 251 | self.doTest(pyc_ext, files, TESTMOD) |
| 252 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 253 | def testImport_WithStuff(self): |
| 254 | # try importing from a zipfile which contains additional |
| 255 | # stuff at the beginning of the file |
| 256 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 257 | self.doTest(".py", files, TESTMOD, |
| 258 | stuff="Some Stuff"*31) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 259 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | def assertModuleSource(self, module): |
| 261 | self.assertEqual(inspect.getsource(module), test_src) |
| 262 | |
| 263 | def testGetSource(self): |
| 264 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 265 | self.doTest(".py", files, TESTMOD, call=self.assertModuleSource) |
| 266 | |
| 267 | def testGetCompiledSource(self): |
| 268 | pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW) |
| 269 | files = {TESTMOD + ".py": (NOW, test_src), |
| 270 | TESTMOD + pyc_ext: (NOW, pyc)} |
| 271 | self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource) |
| 272 | |
| 273 | def runDoctest(self, callback): |
| 274 | files = {TESTMOD + ".py": (NOW, test_src), |
| 275 | "xyz.txt": (NOW, ">>> log.append(True)\n")} |
| 276 | self.doTest(".py", files, TESTMOD, call=callback) |
| 277 | |
| 278 | def doDoctestFile(self, module): |
| 279 | log = [] |
| 280 | old_master, doctest.master = doctest.master, None |
| 281 | try: |
| 282 | doctest.testfile( |
| 283 | 'xyz.txt', package=module, module_relative=True, |
| 284 | globs=locals() |
| 285 | ) |
| 286 | finally: |
| 287 | doctest.master = old_master |
| 288 | self.assertEqual(log,[True]) |
| 289 | |
| 290 | def testDoctestFile(self): |
| 291 | self.runDoctest(self.doDoctestFile) |
| 292 | |
| 293 | def doDoctestSuite(self, module): |
| 294 | log = [] |
| 295 | doctest.DocFileTest( |
| 296 | 'xyz.txt', package=module, module_relative=True, |
| 297 | globs=locals() |
| 298 | ).run() |
| 299 | self.assertEqual(log,[True]) |
| 300 | |
| 301 | def testDoctestSuite(self): |
| 302 | self.runDoctest(self.doDoctestSuite) |
| 303 | |
| 304 | |
| 305 | def doTraceback(self, module): |
| 306 | try: |
| 307 | module.do_raise() |
| 308 | except: |
| 309 | tb = sys.exc_info()[2].tb_next |
| 310 | |
| 311 | f,lno,n,line = extract_tb(tb, 1)[0] |
| 312 | self.assertEqual(line, raise_src.strip()) |
| 313 | |
| 314 | f,lno,n,line = extract_stack(tb.tb_frame, 1)[0] |
| 315 | self.assertEqual(line, raise_src.strip()) |
| 316 | |
| 317 | s = StringIO.StringIO() |
| 318 | print_tb(tb, 1, s) |
| 319 | self.failUnless(s.getvalue().endswith(raise_src)) |
| 320 | else: |
| 321 | raise AssertionError("This ought to be impossible") |
| 322 | |
| 323 | def testTraceback(self): |
| 324 | files = {TESTMOD + ".py": (NOW, raise_src)} |
| 325 | self.doTest(None, files, TESTMOD, call=self.doTraceback) |
| 326 | |
| 327 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 328 | class CompressedZipImportTestCase(UncompressedZipImportTestCase): |
| 329 | compression = ZIP_DEFLATED |
| 330 | |
| 331 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 332 | class BadFileZipImportTestCase(unittest.TestCase): |
| 333 | def assertZipFailure(self, filename): |
| 334 | self.assertRaises(zipimport.ZipImportError, |
| 335 | zipimport.zipimporter, filename) |
| 336 | |
| 337 | def testNoFile(self): |
| 338 | self.assertZipFailure('AdfjdkFJKDFJjdklfjs') |
| 339 | |
| 340 | def testEmptyFilename(self): |
| 341 | self.assertZipFailure('') |
| 342 | |
| 343 | def testBadArgs(self): |
| 344 | self.assertRaises(TypeError, zipimport.zipimporter, None) |
| 345 | self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None) |
| 346 | |
| 347 | def testFilenameTooLong(self): |
| 348 | self.assertZipFailure('A' * 33000) |
| 349 | |
| 350 | def testEmptyFile(self): |
| 351 | test_support.unlink(TESTMOD) |
| 352 | open(TESTMOD, 'w+').close() |
| 353 | self.assertZipFailure(TESTMOD) |
| 354 | |
| 355 | def testFileUnreadable(self): |
| 356 | test_support.unlink(TESTMOD) |
| 357 | fd = os.open(TESTMOD, os.O_CREAT, 000) |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 358 | try: |
| 359 | os.close(fd) |
| 360 | self.assertZipFailure(TESTMOD) |
| 361 | finally: |
| 362 | # If we leave "the read-only bit" set on Windows, nothing can |
| 363 | # delete TESTMOD, and later tests suffer bogus failures. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 364 | os.chmod(TESTMOD, 0o666) |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 365 | test_support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 366 | |
| 367 | def testNotZipFile(self): |
| 368 | test_support.unlink(TESTMOD) |
| 369 | fp = open(TESTMOD, 'w+') |
| 370 | fp.write('a' * 22) |
| 371 | fp.close() |
| 372 | self.assertZipFailure(TESTMOD) |
| 373 | |
Neal Norwitz | dbc95f4 | 2006-01-23 08:48:03 +0000 | [diff] [blame] | 374 | # XXX: disabled until this works on Big-endian machines |
| 375 | def _testBogusZipFile(self): |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 376 | test_support.unlink(TESTMOD) |
| 377 | fp = open(TESTMOD, 'w+') |
| 378 | fp.write(struct.pack('=I', 0x06054B50)) |
| 379 | fp.write('a' * 18) |
| 380 | fp.close() |
| 381 | z = zipimport.zipimporter(TESTMOD) |
| 382 | |
| 383 | try: |
| 384 | self.assertRaises(TypeError, z.find_module, None) |
| 385 | self.assertRaises(TypeError, z.load_module, None) |
| 386 | self.assertRaises(TypeError, z.is_package, None) |
| 387 | self.assertRaises(TypeError, z.get_code, None) |
| 388 | self.assertRaises(TypeError, z.get_data, None) |
| 389 | self.assertRaises(TypeError, z.get_source, None) |
| 390 | |
| 391 | error = zipimport.ZipImportError |
| 392 | self.assertEqual(z.find_module('abc'), None) |
| 393 | |
| 394 | self.assertRaises(error, z.load_module, 'abc') |
| 395 | self.assertRaises(error, z.get_code, 'abc') |
| 396 | self.assertRaises(IOError, z.get_data, 'abc') |
| 397 | self.assertRaises(error, z.get_source, 'abc') |
| 398 | self.assertRaises(error, z.is_package, 'abc') |
| 399 | finally: |
| 400 | zipimport._zip_directory_cache.clear() |
| 401 | |
| 402 | |
| 403 | def cleanup(): |
| 404 | # this is necessary if test is run repeated (like when finding leaks) |
| 405 | global test_imported |
| 406 | if test_imported: |
| 407 | zipimport._zip_directory_cache.clear() |
| 408 | if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'): |
| 409 | delattr(UncompressedZipImportTestCase, 'testAFakeZlib') |
| 410 | if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'): |
| 411 | delattr(CompressedZipImportTestCase, 'testAFakeZlib') |
| 412 | test_imported = True |
| 413 | |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 414 | def test_main(): |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 415 | cleanup() |
| 416 | try: |
| 417 | test_support.run_unittest( |
| 418 | UncompressedZipImportTestCase, |
| 419 | CompressedZipImportTestCase, |
| 420 | BadFileZipImportTestCase, |
| 421 | ) |
| 422 | finally: |
| 423 | test_support.unlink(TESTMOD) |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 424 | |
| 425 | if __name__ == "__main__": |
| 426 | test_main() |