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 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 51 | TESTMOD = "ziptestmodule" |
| 52 | TESTPACK = "ziptestpackage" |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 53 | TESTPACK2 = "ziptestpackage2" |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 54 | TEMP_ZIP = os.path.abspath("junk95142.zip") |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 55 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame^] | 56 | pyc_file = imp.cache_from_source(TESTMOD + '.py') |
| 57 | pyc_ext = ('.pyc' if __debug__ else '.pyo') |
| 58 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 59 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 60 | class UncompressedZipImportTestCase(ImportHooksBaseTestCase): |
| 61 | |
| 62 | compression = ZIP_STORED |
| 63 | |
| 64 | def setUp(self): |
| 65 | # We're reusing the zip archive path, so we must clear the |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | # cached directory info and linecache |
| 67 | linecache.clearcache() |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 68 | zipimport._zip_directory_cache.clear() |
| 69 | ImportHooksBaseTestCase.setUp(self) |
| 70 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 71 | def doTest(self, expected_ext, files, *modules, **kw): |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 72 | z = ZipFile(TEMP_ZIP, "w") |
| 73 | try: |
| 74 | for name, (mtime, data) in files.items(): |
| 75 | zinfo = ZipInfo(name, time.localtime(mtime)) |
| 76 | zinfo.compress_type = self.compression |
| 77 | z.writestr(zinfo, data) |
| 78 | z.close() |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 79 | |
| 80 | stuff = kw.get("stuff", None) |
| 81 | if stuff is not None: |
| 82 | # Prepend 'stuff' to the start of the zipfile |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame^] | 83 | with open(TEMP_ZIP, "rb") as f: |
| 84 | data = f.read() |
| 85 | with open(TEMP_ZIP, "wb") as f: |
| 86 | f.write(stuff) |
| 87 | f.write(data) |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 88 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 89 | sys.path.insert(0, TEMP_ZIP) |
| 90 | |
| 91 | mod = __import__(".".join(modules), globals(), locals(), |
| 92 | ["__dummy__"]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | |
| 94 | call = kw.get('call') |
| 95 | if call is not None: |
| 96 | call(mod) |
| 97 | |
Just van Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 98 | if expected_ext: |
| 99 | file = mod.get_file() |
| 100 | self.assertEquals(file, os.path.join(TEMP_ZIP, |
Just van Rossum | 6706c4d | 2003-01-09 22:27:10 +0000 | [diff] [blame] | 101 | *modules) + expected_ext) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 102 | finally: |
| 103 | z.close() |
| 104 | os.remove(TEMP_ZIP) |
| 105 | |
| 106 | def testAFakeZlib(self): |
| 107 | # |
| 108 | # This could cause a stack overflow before: importing zlib.py |
| 109 | # from a compressed archive would cause zlib to be imported |
| 110 | # which would find zlib.py in the archive, which would... etc. |
| 111 | # |
| 112 | # This test *must* be executed first: it must be the first one |
| 113 | # to trigger zipimport to import zlib (zipimport caches the |
| 114 | # zlib.decompress function object, after which the problem being |
| 115 | # tested here wouldn't be a problem anymore... |
| 116 | # (Hence the 'A' in the test method name: to make it the first |
| 117 | # item in a list sorted by name, like unittest.makeSuite() does.) |
| 118 | # |
Just van Rossum | 5949854 | 2003-11-18 23:00:55 +0000 | [diff] [blame] | 119 | # This test fails on platforms on which the zlib module is |
| 120 | # statically linked, but the problem it tests for can't |
| 121 | # occur in that case (builtin modules are always found first), |
| 122 | # so we'll simply skip it then. Bug #765456. |
| 123 | # |
| 124 | if "zlib" in sys.builtin_module_names: |
| 125 | return |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 126 | if "zlib" in sys.modules: |
| 127 | del sys.modules["zlib"] |
| 128 | files = {"zlib.py": (NOW, test_src)} |
| 129 | try: |
| 130 | self.doTest(".py", files, "zlib") |
| 131 | except ImportError: |
| 132 | if self.compression != ZIP_DEFLATED: |
| 133 | self.fail("expected test to not raise ImportError") |
| 134 | else: |
| 135 | if self.compression != ZIP_STORED: |
| 136 | self.fail("expected test to raise ImportError") |
| 137 | |
| 138 | def testPy(self): |
| 139 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 140 | self.doTest(".py", files, TESTMOD) |
| 141 | |
| 142 | def testPyc(self): |
| 143 | files = {TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 144 | self.doTest(pyc_ext, files, TESTMOD) |
| 145 | |
| 146 | def testBoth(self): |
| 147 | files = {TESTMOD + ".py": (NOW, test_src), |
| 148 | TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 149 | self.doTest(pyc_ext, files, TESTMOD) |
| 150 | |
Just van Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 151 | def testEmptyPy(self): |
| 152 | files = {TESTMOD + ".py": (NOW, "")} |
| 153 | self.doTest(None, files, TESTMOD) |
| 154 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 155 | def testBadMagic(self): |
| 156 | # make pyc magic word invalid, forcing loading from .py |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 157 | badmagic_pyc = bytearray(test_pyc) |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 158 | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 159 | files = {TESTMOD + ".py": (NOW, test_src), |
| 160 | TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |
| 161 | self.doTest(".py", files, TESTMOD) |
| 162 | |
| 163 | def testBadMagic2(self): |
| 164 | # make pyc magic word invalid, causing an ImportError |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 165 | badmagic_pyc = bytearray(test_pyc) |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 166 | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 167 | files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |
| 168 | try: |
| 169 | self.doTest(".py", files, TESTMOD) |
| 170 | except ImportError: |
| 171 | pass |
| 172 | else: |
| 173 | self.fail("expected ImportError; import from bad pyc") |
| 174 | |
| 175 | def testBadMTime(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 176 | badtime_pyc = bytearray(test_pyc) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame^] | 177 | # flip the second bit -- not the first as that one isn't stored in the |
| 178 | # .py's mtime in the zip archive. |
| 179 | badtime_pyc[7] ^= 0x02 |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 180 | files = {TESTMOD + ".py": (NOW, test_src), |
| 181 | TESTMOD + pyc_ext: (NOW, badtime_pyc)} |
| 182 | self.doTest(".py", files, TESTMOD) |
| 183 | |
| 184 | def testPackage(self): |
| 185 | packdir = TESTPACK + os.sep |
| 186 | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
| 187 | packdir + TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 188 | self.doTest(pyc_ext, files, TESTPACK, TESTMOD) |
| 189 | |
| 190 | def testDeepPackage(self): |
| 191 | packdir = TESTPACK + os.sep |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 192 | packdir2 = packdir + TESTPACK2 + os.sep |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 193 | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
| 194 | packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
| 195 | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 196 | self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 197 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 198 | def testZipImporterMethods(self): |
| 199 | packdir = TESTPACK + os.sep |
| 200 | packdir2 = packdir + TESTPACK2 + os.sep |
| 201 | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
| 202 | packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
| 203 | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 204 | |
| 205 | z = ZipFile(TEMP_ZIP, "w") |
| 206 | try: |
| 207 | for name, (mtime, data) in files.items(): |
| 208 | zinfo = ZipInfo(name, time.localtime(mtime)) |
| 209 | zinfo.compress_type = self.compression |
| 210 | z.writestr(zinfo, data) |
| 211 | z.close() |
| 212 | |
| 213 | zi = zipimport.zipimporter(TEMP_ZIP) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 214 | self.assertEquals(zi.archive, TEMP_ZIP) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 215 | self.assertEquals(zi.is_package(TESTPACK), True) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 216 | mod = zi.load_module(TESTPACK) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 217 | self.assertEquals(zi.get_filename(TESTPACK), mod.__file__) |
Tim Peters | bc29c1a | 2006-01-23 21:28:42 +0000 | [diff] [blame] | 218 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 219 | self.assertEquals(zi.is_package(packdir + '__init__'), False) |
| 220 | self.assertEquals(zi.is_package(packdir + TESTPACK2), True) |
| 221 | self.assertEquals(zi.is_package(packdir2 + TESTMOD), False) |
| 222 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 223 | mod_path = packdir2 + TESTMOD |
| 224 | mod_name = module_path_to_dotted_name(mod_path) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 225 | __import__(mod_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 226 | mod = sys.modules[mod_name] |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 227 | self.assertEquals(zi.get_source(TESTPACK), None) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 228 | self.assertEquals(zi.get_source(mod_path), None) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 229 | self.assertEquals(zi.get_filename(mod_path), mod.__file__) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame^] | 230 | # To pass in the module name instead of the path, we must use the |
| 231 | # right importer |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 232 | loader = mod.__loader__ |
| 233 | self.assertEquals(loader.get_source(mod_name), None) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 234 | self.assertEquals(loader.get_filename(mod_name), mod.__file__) |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 235 | |
| 236 | # test prefix and archivepath members |
| 237 | zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK) |
| 238 | self.assertEquals(zi2.archive, TEMP_ZIP) |
| 239 | self.assertEquals(zi2.prefix, TESTPACK + os.sep) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 240 | finally: |
| 241 | z.close() |
| 242 | os.remove(TEMP_ZIP) |
| 243 | |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 244 | def testZipImporterMethodsInSubDirectory(self): |
| 245 | packdir = TESTPACK + os.sep |
| 246 | packdir2 = packdir + TESTPACK2 + os.sep |
| 247 | files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
| 248 | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 249 | |
| 250 | z = ZipFile(TEMP_ZIP, "w") |
| 251 | try: |
| 252 | for name, (mtime, data) in files.items(): |
| 253 | zinfo = ZipInfo(name, time.localtime(mtime)) |
| 254 | zinfo.compress_type = self.compression |
| 255 | z.writestr(zinfo, data) |
| 256 | z.close() |
| 257 | |
| 258 | zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir) |
| 259 | self.assertEquals(zi.archive, TEMP_ZIP) |
| 260 | self.assertEquals(zi.prefix, packdir) |
| 261 | self.assertEquals(zi.is_package(TESTPACK2), True) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 262 | mod = zi.load_module(TESTPACK2) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 263 | self.assertEquals(zi.get_filename(TESTPACK2), mod.__file__) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 264 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame^] | 265 | self.assertEquals( |
| 266 | zi.is_package(TESTPACK2 + os.sep + '__init__'), False) |
| 267 | self.assertEquals( |
| 268 | zi.is_package(TESTPACK2 + os.sep + TESTMOD), False) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 269 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 270 | mod_path = TESTPACK2 + os.sep + TESTMOD |
| 271 | mod_name = module_path_to_dotted_name(mod_path) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 272 | __import__(mod_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 273 | mod = sys.modules[mod_name] |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 274 | self.assertEquals(zi.get_source(TESTPACK2), None) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 275 | self.assertEquals(zi.get_source(mod_path), None) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 276 | self.assertEquals(zi.get_filename(mod_path), mod.__file__) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame^] | 277 | # To pass in the module name instead of the path, we must use the |
| 278 | # right importer |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 279 | loader = mod.__loader__ |
| 280 | self.assertEquals(loader.get_source(mod_name), None) |
Nick Coghlan | 9a1d6e3 | 2009-02-08 03:37:27 +0000 | [diff] [blame] | 281 | self.assertEquals(loader.get_filename(mod_name), mod.__file__) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 282 | finally: |
| 283 | z.close() |
| 284 | os.remove(TEMP_ZIP) |
| 285 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 286 | def testGetData(self): |
| 287 | z = ZipFile(TEMP_ZIP, "w") |
| 288 | z.compression = self.compression |
| 289 | try: |
| 290 | name = "testdata.dat" |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 291 | data = bytes(x for x in range(256)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 292 | z.writestr(name, data) |
| 293 | z.close() |
| 294 | zi = zipimport.zipimporter(TEMP_ZIP) |
| 295 | self.assertEquals(data, zi.get_data(name)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 296 | self.assertIn('zipimporter object', repr(zi)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 297 | finally: |
| 298 | z.close() |
| 299 | os.remove(TEMP_ZIP) |
| 300 | |
| 301 | def testImporterAttr(self): |
| 302 | src = """if 1: # indent hack |
| 303 | def get_file(): |
| 304 | return __file__ |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 305 | if __loader__.get_data("some.data") != b"some data": |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 306 | raise AssertionError("bad data")\n""" |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 307 | pyc = make_pyc(compile(src, "<???>", "exec"), NOW) |
| 308 | files = {TESTMOD + pyc_ext: (NOW, pyc), |
| 309 | "some.data": (NOW, "some data")} |
| 310 | self.doTest(pyc_ext, files, TESTMOD) |
| 311 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 312 | def testImport_WithStuff(self): |
| 313 | # try importing from a zipfile which contains additional |
| 314 | # stuff at the beginning of the file |
| 315 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 316 | self.doTest(".py", files, TESTMOD, |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 317 | stuff=b"Some Stuff"*31) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 318 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 319 | def assertModuleSource(self, module): |
| 320 | self.assertEqual(inspect.getsource(module), test_src) |
| 321 | |
| 322 | def testGetSource(self): |
| 323 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 324 | self.doTest(".py", files, TESTMOD, call=self.assertModuleSource) |
| 325 | |
| 326 | def testGetCompiledSource(self): |
| 327 | pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW) |
| 328 | files = {TESTMOD + ".py": (NOW, test_src), |
| 329 | TESTMOD + pyc_ext: (NOW, pyc)} |
| 330 | self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource) |
| 331 | |
| 332 | def runDoctest(self, callback): |
| 333 | files = {TESTMOD + ".py": (NOW, test_src), |
| 334 | "xyz.txt": (NOW, ">>> log.append(True)\n")} |
| 335 | self.doTest(".py", files, TESTMOD, call=callback) |
| 336 | |
| 337 | def doDoctestFile(self, module): |
| 338 | log = [] |
| 339 | old_master, doctest.master = doctest.master, None |
| 340 | try: |
| 341 | doctest.testfile( |
| 342 | 'xyz.txt', package=module, module_relative=True, |
| 343 | globs=locals() |
| 344 | ) |
| 345 | finally: |
| 346 | doctest.master = old_master |
| 347 | self.assertEqual(log,[True]) |
| 348 | |
| 349 | def testDoctestFile(self): |
| 350 | self.runDoctest(self.doDoctestFile) |
| 351 | |
| 352 | def doDoctestSuite(self, module): |
| 353 | log = [] |
| 354 | doctest.DocFileTest( |
| 355 | 'xyz.txt', package=module, module_relative=True, |
| 356 | globs=locals() |
| 357 | ).run() |
| 358 | self.assertEqual(log,[True]) |
| 359 | |
| 360 | def testDoctestSuite(self): |
| 361 | self.runDoctest(self.doDoctestSuite) |
| 362 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 363 | def doTraceback(self, module): |
| 364 | try: |
| 365 | module.do_raise() |
| 366 | except: |
| 367 | tb = sys.exc_info()[2].tb_next |
| 368 | |
| 369 | f,lno,n,line = extract_tb(tb, 1)[0] |
| 370 | self.assertEqual(line, raise_src.strip()) |
| 371 | |
| 372 | f,lno,n,line = extract_stack(tb.tb_frame, 1)[0] |
| 373 | self.assertEqual(line, raise_src.strip()) |
| 374 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 375 | s = io.StringIO() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 376 | print_tb(tb, 1, s) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 377 | self.assertTrue(s.getvalue().endswith(raise_src)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 378 | else: |
| 379 | raise AssertionError("This ought to be impossible") |
| 380 | |
| 381 | def testTraceback(self): |
| 382 | files = {TESTMOD + ".py": (NOW, raise_src)} |
| 383 | self.doTest(None, files, TESTMOD, call=self.doTraceback) |
| 384 | |
| 385 | |
Ezio Melotti | 78ea202 | 2009-09-12 18:41:20 +0000 | [diff] [blame] | 386 | @unittest.skipUnless(zlib, "requires zlib") |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 387 | class CompressedZipImportTestCase(UncompressedZipImportTestCase): |
| 388 | compression = ZIP_DEFLATED |
| 389 | |
| 390 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 391 | class BadFileZipImportTestCase(unittest.TestCase): |
| 392 | def assertZipFailure(self, filename): |
| 393 | self.assertRaises(zipimport.ZipImportError, |
| 394 | zipimport.zipimporter, filename) |
| 395 | |
| 396 | def testNoFile(self): |
| 397 | self.assertZipFailure('AdfjdkFJKDFJjdklfjs') |
| 398 | |
| 399 | def testEmptyFilename(self): |
| 400 | self.assertZipFailure('') |
| 401 | |
| 402 | def testBadArgs(self): |
| 403 | self.assertRaises(TypeError, zipimport.zipimporter, None) |
| 404 | self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None) |
| 405 | |
| 406 | def testFilenameTooLong(self): |
| 407 | self.assertZipFailure('A' * 33000) |
| 408 | |
| 409 | def testEmptyFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 410 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 411 | open(TESTMOD, 'w+').close() |
| 412 | self.assertZipFailure(TESTMOD) |
| 413 | |
| 414 | def testFileUnreadable(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 415 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 416 | fd = os.open(TESTMOD, os.O_CREAT, 000) |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 417 | try: |
| 418 | os.close(fd) |
| 419 | self.assertZipFailure(TESTMOD) |
| 420 | finally: |
| 421 | # If we leave "the read-only bit" set on Windows, nothing can |
| 422 | # delete TESTMOD, and later tests suffer bogus failures. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 423 | os.chmod(TESTMOD, 0o666) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 424 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 425 | |
| 426 | def testNotZipFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 427 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 428 | fp = open(TESTMOD, 'w+') |
| 429 | fp.write('a' * 22) |
| 430 | fp.close() |
| 431 | self.assertZipFailure(TESTMOD) |
| 432 | |
Neal Norwitz | dbc95f4 | 2006-01-23 08:48:03 +0000 | [diff] [blame] | 433 | # XXX: disabled until this works on Big-endian machines |
| 434 | def _testBogusZipFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 435 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 436 | fp = open(TESTMOD, 'w+') |
| 437 | fp.write(struct.pack('=I', 0x06054B50)) |
| 438 | fp.write('a' * 18) |
| 439 | fp.close() |
| 440 | z = zipimport.zipimporter(TESTMOD) |
| 441 | |
| 442 | try: |
| 443 | self.assertRaises(TypeError, z.find_module, None) |
| 444 | self.assertRaises(TypeError, z.load_module, None) |
| 445 | self.assertRaises(TypeError, z.is_package, None) |
| 446 | self.assertRaises(TypeError, z.get_code, None) |
| 447 | self.assertRaises(TypeError, z.get_data, None) |
| 448 | self.assertRaises(TypeError, z.get_source, None) |
| 449 | |
| 450 | error = zipimport.ZipImportError |
| 451 | self.assertEqual(z.find_module('abc'), None) |
| 452 | |
| 453 | self.assertRaises(error, z.load_module, 'abc') |
| 454 | self.assertRaises(error, z.get_code, 'abc') |
| 455 | self.assertRaises(IOError, z.get_data, 'abc') |
| 456 | self.assertRaises(error, z.get_source, 'abc') |
| 457 | self.assertRaises(error, z.is_package, 'abc') |
| 458 | finally: |
| 459 | zipimport._zip_directory_cache.clear() |
| 460 | |
| 461 | |
| 462 | def cleanup(): |
| 463 | # this is necessary if test is run repeated (like when finding leaks) |
| 464 | global test_imported |
| 465 | if test_imported: |
| 466 | zipimport._zip_directory_cache.clear() |
| 467 | if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'): |
| 468 | delattr(UncompressedZipImportTestCase, 'testAFakeZlib') |
| 469 | if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'): |
| 470 | delattr(CompressedZipImportTestCase, 'testAFakeZlib') |
| 471 | test_imported = True |
| 472 | |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 473 | def test_main(): |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 474 | cleanup() |
| 475 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 476 | support.run_unittest( |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 477 | UncompressedZipImportTestCase, |
| 478 | CompressedZipImportTestCase, |
| 479 | BadFileZipImportTestCase, |
| 480 | ) |
| 481 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 482 | support.unlink(TESTMOD) |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 483 | |
| 484 | if __name__ == "__main__": |
| 485 | test_main() |