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