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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 10 | from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co |
| 11 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 12 | # some tests can be ran even without zlib |
| 13 | try: |
| 14 | import zlib |
| 15 | except ImportError: |
| 16 | zlib = None |
| 17 | |
| 18 | from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED |
| 19 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 20 | import zipimport |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 21 | import linecache |
| 22 | import doctest |
| 23 | import inspect |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 24 | import io |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 25 | from traceback import extract_tb, extract_stack, print_tb |
| 26 | raise_src = 'def do_raise(): raise TypeError\n' |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 27 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 28 | # so we only run testAFakeZlib once if this test is run repeatedly |
| 29 | # which happens when we look for ref leaks |
| 30 | test_imported = False |
| 31 | |
| 32 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 33 | def make_pyc(co, mtime): |
| 34 | data = marshal.dumps(co) |
Jack Jansen | 472e7db | 2003-01-08 16:37:03 +0000 | [diff] [blame] | 35 | if type(mtime) is type(0.0): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 36 | # Mac mtimes need a bit of special casing |
| 37 | if mtime < 0x7fffffff: |
| 38 | mtime = int(mtime) |
| 39 | else: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 40 | mtime = int(-0x100000000 + int(mtime)) |
Jack Jansen | 472e7db | 2003-01-08 16:37:03 +0000 | [diff] [blame] | 41 | pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 42 | return pyc |
| 43 | |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 44 | def module_path_to_dotted_name(path): |
| 45 | return path.replace(os.sep, '.') |
| 46 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 47 | NOW = time.time() |
| 48 | test_pyc = make_pyc(test_co, NOW) |
| 49 | |
| 50 | |
| 51 | if __debug__: |
| 52 | pyc_ext = ".pyc" |
| 53 | else: |
| 54 | pyc_ext = ".pyo" |
| 55 | |
| 56 | |
| 57 | TESTMOD = "ziptestmodule" |
| 58 | TESTPACK = "ziptestpackage" |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 59 | TESTPACK2 = "ziptestpackage2" |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 60 | TEMP_ZIP = os.path.abspath("junk95142.zip") |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 61 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 62 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 63 | class 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 69 | # cached directory info and linecache |
| 70 | linecache.clearcache() |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 71 | zipimport._zip_directory_cache.clear() |
| 72 | ImportHooksBaseTestCase.setUp(self) |
| 73 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 74 | def doTest(self, expected_ext, files, *modules, **kw): |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 75 | 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 Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 82 | |
| 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 Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 95 | sys.path.insert(0, TEMP_ZIP) |
| 96 | |
| 97 | mod = __import__(".".join(modules), globals(), locals(), |
| 98 | ["__dummy__"]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | |
| 100 | call = kw.get('call') |
| 101 | if call is not None: |
| 102 | call(mod) |
| 103 | |
Just van Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 104 | if expected_ext: |
| 105 | file = mod.get_file() |
| 106 | self.assertEquals(file, os.path.join(TEMP_ZIP, |
Just van Rossum | 6706c4d | 2003-01-09 22:27:10 +0000 | [diff] [blame] | 107 | *modules) + expected_ext) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 108 | 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 Rossum | 5949854 | 2003-11-18 23:00:55 +0000 | [diff] [blame] | 125 | # 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 Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 132 | 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 Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 157 | def testEmptyPy(self): |
| 158 | files = {TESTMOD + ".py": (NOW, "")} |
| 159 | self.doTest(None, files, TESTMOD) |
| 160 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 161 | def testBadMagic(self): |
| 162 | # make pyc magic word invalid, forcing loading from .py |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 163 | badmagic_pyc = bytearray(test_pyc) |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 164 | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 165 | 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 Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 171 | badmagic_pyc = bytearray(test_pyc) |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 172 | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 173 | 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 Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 182 | badtime_pyc = bytearray(test_pyc) |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 183 | 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 Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 185 | 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 Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 197 | packdir2 = packdir + TESTPACK2 + os.sep |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 198 | 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 Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 201 | self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 202 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 203 | 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 Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 219 | self.assertEquals(zi.archive, TEMP_ZIP) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 220 | self.assertEquals(zi.is_package(TESTPACK), True) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 221 | mod = zi.load_module(TESTPACK) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 222 | self.assertEquals(zi.get_filename(TESTPACK), mod.__file__) |
Tim Peters | bc29c1a | 2006-01-23 21:28:42 +0000 | [diff] [blame] | 223 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 224 | 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 Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 228 | mod_path = packdir2 + TESTMOD |
| 229 | mod_name = module_path_to_dotted_name(mod_path) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame^] | 230 | __import__(mod_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 231 | mod = sys.modules[mod_name] |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 232 | self.assertEquals(zi.get_source(TESTPACK), None) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 233 | self.assertEquals(zi.get_source(mod_path), None) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 234 | self.assertEquals(zi.get_filename(mod_path), mod.__file__) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 235 | # 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 Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 238 | self.assertEquals(loader.get_filename(mod_name), mod.__file__) |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 239 | |
| 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 Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 244 | finally: |
| 245 | z.close() |
| 246 | os.remove(TEMP_ZIP) |
| 247 | |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 248 | 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 Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 266 | mod = zi.load_module(TESTPACK2) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 267 | self.assertEquals(zi.get_filename(TESTPACK2), mod.__file__) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 268 | |
| 269 | self.assertEquals(zi.is_package(TESTPACK2 + os.sep + '__init__'), False) |
| 270 | self.assertEquals(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False) |
| 271 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 272 | mod_path = TESTPACK2 + os.sep + TESTMOD |
| 273 | mod_name = module_path_to_dotted_name(mod_path) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame^] | 274 | __import__(mod_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 275 | mod = sys.modules[mod_name] |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 276 | self.assertEquals(zi.get_source(TESTPACK2), None) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 277 | self.assertEquals(zi.get_source(mod_path), None) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 278 | self.assertEquals(zi.get_filename(mod_path), mod.__file__) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 279 | # 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 Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 282 | self.assertEquals(loader.get_filename(mod_name), mod.__file__) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 283 | finally: |
| 284 | z.close() |
| 285 | os.remove(TEMP_ZIP) |
| 286 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 287 | def testGetData(self): |
| 288 | z = ZipFile(TEMP_ZIP, "w") |
| 289 | z.compression = self.compression |
| 290 | try: |
| 291 | name = "testdata.dat" |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 292 | data = bytes(x for x in range(256)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 293 | z.writestr(name, data) |
| 294 | z.close() |
| 295 | zi = zipimport.zipimporter(TEMP_ZIP) |
| 296 | self.assertEquals(data, zi.get_data(name)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 297 | self.assertIn('zipimporter object', repr(zi)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 298 | 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 Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 306 | if __loader__.get_data("some.data") != b"some data": |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 307 | raise AssertionError("bad data")\n""" |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 308 | 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 Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 313 | 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 Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 318 | stuff=b"Some Stuff"*31) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 319 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 320 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 364 | 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 Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 376 | s = io.StringIO() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 377 | print_tb(tb, 1, s) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 378 | self.assertTrue(s.getvalue().endswith(raise_src)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 379 | 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 Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 387 | @unittest.skipUnless(zlib, "requires zlib") |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 388 | class CompressedZipImportTestCase(UncompressedZipImportTestCase): |
| 389 | compression = ZIP_DEFLATED |
| 390 | |
| 391 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 392 | class 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 Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 411 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 412 | open(TESTMOD, 'w+').close() |
| 413 | self.assertZipFailure(TESTMOD) |
| 414 | |
| 415 | def testFileUnreadable(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 416 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 417 | fd = os.open(TESTMOD, os.O_CREAT, 000) |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 418 | 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 Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 424 | os.chmod(TESTMOD, 0o666) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 425 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 426 | |
| 427 | def testNotZipFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 428 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 429 | fp = open(TESTMOD, 'w+') |
| 430 | fp.write('a' * 22) |
| 431 | fp.close() |
| 432 | self.assertZipFailure(TESTMOD) |
| 433 | |
Neal Norwitz | dbc95f4 | 2006-01-23 08:48:03 +0000 | [diff] [blame] | 434 | # XXX: disabled until this works on Big-endian machines |
| 435 | def _testBogusZipFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 436 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 437 | 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 | |
| 463 | def 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 Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 474 | def test_main(): |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 475 | cleanup() |
| 476 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 477 | support.run_unittest( |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 478 | UncompressedZipImportTestCase, |
| 479 | CompressedZipImportTestCase, |
| 480 | BadFileZipImportTestCase, |
| 481 | ) |
| 482 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 483 | support.unlink(TESTMOD) |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 484 | |
| 485 | if __name__ == "__main__": |
| 486 | test_main() |