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 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 22 | def make_pyc(co, mtime, size): |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 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)) |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 30 | pyc = imp.get_magic() + struct.pack("<ii", int(mtime), size & 0xFFFFFFFF) + 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() |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 37 | test_pyc = make_pyc(test_co, NOW, len(test_src)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | 0e6b7b5 | 2013-02-16 17:43:45 +0200 | [diff] [blame^] | 199 | zinfo.comment = b"spam" |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 200 | z.writestr(zinfo, data) |
| 201 | z.close() |
| 202 | |
| 203 | zi = zipimport.zipimporter(TEMP_ZIP) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 204 | self.assertEqual(zi.archive, TEMP_ZIP) |
| 205 | self.assertEqual(zi.is_package(TESTPACK), True) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 206 | mod = zi.load_module(TESTPACK) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 207 | self.assertEqual(zi.get_filename(TESTPACK), mod.__file__) |
Tim Peters | bc29c1a | 2006-01-23 21:28:42 +0000 | [diff] [blame] | 208 | |
R David Murray | a971905 | 2012-03-12 21:16:33 -0400 | [diff] [blame] | 209 | existing_pack_path = __import__(TESTPACK).__path__[0] |
| 210 | expected_path_path = os.path.join(TEMP_ZIP, TESTPACK) |
| 211 | self.assertEqual(existing_pack_path, expected_path_path) |
| 212 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 213 | self.assertEqual(zi.is_package(packdir + '__init__'), False) |
| 214 | self.assertEqual(zi.is_package(packdir + TESTPACK2), True) |
| 215 | self.assertEqual(zi.is_package(packdir2 + TESTMOD), False) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 216 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 217 | mod_path = packdir2 + TESTMOD |
| 218 | mod_name = module_path_to_dotted_name(mod_path) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 219 | __import__(mod_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 220 | mod = sys.modules[mod_name] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 221 | self.assertEqual(zi.get_source(TESTPACK), None) |
| 222 | self.assertEqual(zi.get_source(mod_path), None) |
| 223 | self.assertEqual(zi.get_filename(mod_path), mod.__file__) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 224 | # To pass in the module name instead of the path, we must use the |
| 225 | # right importer |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 226 | loader = mod.__loader__ |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 227 | self.assertEqual(loader.get_source(mod_name), None) |
| 228 | self.assertEqual(loader.get_filename(mod_name), mod.__file__) |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 229 | |
| 230 | # test prefix and archivepath members |
| 231 | zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 232 | self.assertEqual(zi2.archive, TEMP_ZIP) |
| 233 | self.assertEqual(zi2.prefix, TESTPACK + os.sep) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 234 | finally: |
| 235 | z.close() |
| 236 | os.remove(TEMP_ZIP) |
| 237 | |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 238 | def testZipImporterMethodsInSubDirectory(self): |
| 239 | packdir = TESTPACK + os.sep |
| 240 | packdir2 = packdir + TESTPACK2 + os.sep |
| 241 | files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
| 242 | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 243 | |
| 244 | z = ZipFile(TEMP_ZIP, "w") |
| 245 | try: |
| 246 | for name, (mtime, data) in files.items(): |
| 247 | zinfo = ZipInfo(name, time.localtime(mtime)) |
| 248 | zinfo.compress_type = self.compression |
Serhiy Storchaka | 0e6b7b5 | 2013-02-16 17:43:45 +0200 | [diff] [blame^] | 249 | zinfo.comment = b"eggs" |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 250 | z.writestr(zinfo, data) |
| 251 | z.close() |
| 252 | |
| 253 | zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 254 | self.assertEqual(zi.archive, TEMP_ZIP) |
| 255 | self.assertEqual(zi.prefix, packdir) |
| 256 | self.assertEqual(zi.is_package(TESTPACK2), True) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 257 | mod = zi.load_module(TESTPACK2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 258 | self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 259 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 260 | self.assertEqual( |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 261 | zi.is_package(TESTPACK2 + os.sep + '__init__'), False) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 262 | self.assertEqual( |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 263 | zi.is_package(TESTPACK2 + os.sep + TESTMOD), False) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 264 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 265 | mod_path = TESTPACK2 + os.sep + TESTMOD |
| 266 | mod_name = module_path_to_dotted_name(mod_path) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 267 | __import__(mod_name) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 268 | mod = sys.modules[mod_name] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 269 | self.assertEqual(zi.get_source(TESTPACK2), None) |
| 270 | self.assertEqual(zi.get_source(mod_path), None) |
| 271 | self.assertEqual(zi.get_filename(mod_path), mod.__file__) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 272 | # To pass in the module name instead of the path, we must use the |
| 273 | # right importer |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 274 | loader = mod.__loader__ |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 275 | self.assertEqual(loader.get_source(mod_name), None) |
| 276 | self.assertEqual(loader.get_filename(mod_name), mod.__file__) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 277 | finally: |
| 278 | z.close() |
| 279 | os.remove(TEMP_ZIP) |
| 280 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 281 | def testGetData(self): |
| 282 | z = ZipFile(TEMP_ZIP, "w") |
| 283 | z.compression = self.compression |
| 284 | try: |
| 285 | name = "testdata.dat" |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 286 | data = bytes(x for x in range(256)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 287 | z.writestr(name, data) |
| 288 | z.close() |
| 289 | zi = zipimport.zipimporter(TEMP_ZIP) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 290 | self.assertEqual(data, zi.get_data(name)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 291 | self.assertIn('zipimporter object', repr(zi)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 292 | finally: |
| 293 | z.close() |
| 294 | os.remove(TEMP_ZIP) |
| 295 | |
| 296 | def testImporterAttr(self): |
| 297 | src = """if 1: # indent hack |
| 298 | def get_file(): |
| 299 | return __file__ |
Guido van Rossum | ad8d300 | 2007-08-03 18:40:49 +0000 | [diff] [blame] | 300 | if __loader__.get_data("some.data") != b"some data": |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 301 | raise AssertionError("bad data")\n""" |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 302 | pyc = make_pyc(compile(src, "<???>", "exec"), NOW, len(src)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 303 | files = {TESTMOD + pyc_ext: (NOW, pyc), |
| 304 | "some.data": (NOW, "some data")} |
| 305 | self.doTest(pyc_ext, files, TESTMOD) |
| 306 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 307 | def testImport_WithStuff(self): |
| 308 | # try importing from a zipfile which contains additional |
| 309 | # stuff at the beginning of the file |
| 310 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 311 | self.doTest(".py", files, TESTMOD, |
Guido van Rossum | 85825dc | 2007-08-27 17:03:28 +0000 | [diff] [blame] | 312 | stuff=b"Some Stuff"*31) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 313 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 314 | def assertModuleSource(self, module): |
| 315 | self.assertEqual(inspect.getsource(module), test_src) |
| 316 | |
| 317 | def testGetSource(self): |
| 318 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 319 | self.doTest(".py", files, TESTMOD, call=self.assertModuleSource) |
| 320 | |
| 321 | def testGetCompiledSource(self): |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 322 | pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW, len(test_src)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | files = {TESTMOD + ".py": (NOW, test_src), |
| 324 | TESTMOD + pyc_ext: (NOW, pyc)} |
| 325 | self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource) |
| 326 | |
| 327 | def runDoctest(self, callback): |
| 328 | files = {TESTMOD + ".py": (NOW, test_src), |
| 329 | "xyz.txt": (NOW, ">>> log.append(True)\n")} |
| 330 | self.doTest(".py", files, TESTMOD, call=callback) |
| 331 | |
| 332 | def doDoctestFile(self, module): |
| 333 | log = [] |
| 334 | old_master, doctest.master = doctest.master, None |
| 335 | try: |
| 336 | doctest.testfile( |
| 337 | 'xyz.txt', package=module, module_relative=True, |
| 338 | globs=locals() |
| 339 | ) |
| 340 | finally: |
| 341 | doctest.master = old_master |
| 342 | self.assertEqual(log,[True]) |
| 343 | |
| 344 | def testDoctestFile(self): |
| 345 | self.runDoctest(self.doDoctestFile) |
| 346 | |
| 347 | def doDoctestSuite(self, module): |
| 348 | log = [] |
| 349 | doctest.DocFileTest( |
| 350 | 'xyz.txt', package=module, module_relative=True, |
| 351 | globs=locals() |
| 352 | ).run() |
| 353 | self.assertEqual(log,[True]) |
| 354 | |
| 355 | def testDoctestSuite(self): |
| 356 | self.runDoctest(self.doDoctestSuite) |
| 357 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | def doTraceback(self, module): |
| 359 | try: |
| 360 | module.do_raise() |
| 361 | except: |
| 362 | tb = sys.exc_info()[2].tb_next |
| 363 | |
| 364 | f,lno,n,line = extract_tb(tb, 1)[0] |
| 365 | self.assertEqual(line, raise_src.strip()) |
| 366 | |
| 367 | f,lno,n,line = extract_stack(tb.tb_frame, 1)[0] |
| 368 | self.assertEqual(line, raise_src.strip()) |
| 369 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 370 | s = io.StringIO() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 371 | print_tb(tb, 1, s) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 372 | self.assertTrue(s.getvalue().endswith(raise_src)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 373 | else: |
| 374 | raise AssertionError("This ought to be impossible") |
| 375 | |
| 376 | def testTraceback(self): |
| 377 | files = {TESTMOD + ".py": (NOW, raise_src)} |
| 378 | self.doTest(None, files, TESTMOD, call=self.doTraceback) |
| 379 | |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 380 | @unittest.skipIf(support.TESTFN_UNENCODABLE is None, |
| 381 | "need an unencodable filename") |
Victor Stinner | 36e7911 | 2010-08-17 00:44:11 +0000 | [diff] [blame] | 382 | def testUnencodable(self): |
Victor Stinner | 2460a43 | 2010-08-16 17:54:28 +0000 | [diff] [blame] | 383 | filename = support.TESTFN_UNENCODABLE + ".zip" |
| 384 | z = ZipFile(filename, "w") |
| 385 | zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW)) |
| 386 | zinfo.compress_type = self.compression |
| 387 | z.writestr(zinfo, test_src) |
| 388 | z.close() |
| 389 | try: |
| 390 | zipimport.zipimporter(filename) |
| 391 | finally: |
| 392 | os.remove(filename) |
| 393 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 394 | |
Ezio Melotti | 975077a | 2011-05-19 22:03:22 +0300 | [diff] [blame] | 395 | @support.requires_zlib |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 396 | class CompressedZipImportTestCase(UncompressedZipImportTestCase): |
| 397 | compression = ZIP_DEFLATED |
| 398 | |
| 399 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 400 | class BadFileZipImportTestCase(unittest.TestCase): |
| 401 | def assertZipFailure(self, filename): |
| 402 | self.assertRaises(zipimport.ZipImportError, |
| 403 | zipimport.zipimporter, filename) |
| 404 | |
| 405 | def testNoFile(self): |
| 406 | self.assertZipFailure('AdfjdkFJKDFJjdklfjs') |
| 407 | |
| 408 | def testEmptyFilename(self): |
| 409 | self.assertZipFailure('') |
| 410 | |
| 411 | def testBadArgs(self): |
| 412 | self.assertRaises(TypeError, zipimport.zipimporter, None) |
| 413 | self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None) |
| 414 | |
| 415 | def testFilenameTooLong(self): |
| 416 | self.assertZipFailure('A' * 33000) |
| 417 | |
| 418 | def testEmptyFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 419 | support.unlink(TESTMOD) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 420 | support.create_empty_file(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 421 | self.assertZipFailure(TESTMOD) |
| 422 | |
| 423 | def testFileUnreadable(self): |
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 | fd = os.open(TESTMOD, os.O_CREAT, 000) |
Tim Peters | 68f2d00 | 2006-01-23 22:19:24 +0000 | [diff] [blame] | 426 | try: |
| 427 | os.close(fd) |
| 428 | self.assertZipFailure(TESTMOD) |
| 429 | finally: |
| 430 | # If we leave "the read-only bit" set on Windows, nothing can |
| 431 | # delete TESTMOD, and later tests suffer bogus failures. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 432 | os.chmod(TESTMOD, 0o666) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 433 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 434 | |
| 435 | def testNotZipFile(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('a' * 22) |
| 439 | fp.close() |
| 440 | self.assertZipFailure(TESTMOD) |
| 441 | |
Neal Norwitz | dbc95f4 | 2006-01-23 08:48:03 +0000 | [diff] [blame] | 442 | # XXX: disabled until this works on Big-endian machines |
| 443 | def _testBogusZipFile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 444 | support.unlink(TESTMOD) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 445 | fp = open(TESTMOD, 'w+') |
| 446 | fp.write(struct.pack('=I', 0x06054B50)) |
| 447 | fp.write('a' * 18) |
| 448 | fp.close() |
| 449 | z = zipimport.zipimporter(TESTMOD) |
| 450 | |
| 451 | try: |
| 452 | self.assertRaises(TypeError, z.find_module, None) |
| 453 | self.assertRaises(TypeError, z.load_module, None) |
| 454 | self.assertRaises(TypeError, z.is_package, None) |
| 455 | self.assertRaises(TypeError, z.get_code, None) |
| 456 | self.assertRaises(TypeError, z.get_data, None) |
| 457 | self.assertRaises(TypeError, z.get_source, None) |
| 458 | |
| 459 | error = zipimport.ZipImportError |
| 460 | self.assertEqual(z.find_module('abc'), None) |
| 461 | |
| 462 | self.assertRaises(error, z.load_module, 'abc') |
| 463 | self.assertRaises(error, z.get_code, 'abc') |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 464 | self.assertRaises(OSError, z.get_data, 'abc') |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 465 | self.assertRaises(error, z.get_source, 'abc') |
| 466 | self.assertRaises(error, z.is_package, 'abc') |
| 467 | finally: |
| 468 | zipimport._zip_directory_cache.clear() |
| 469 | |
| 470 | |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 471 | def test_main(): |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 472 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 473 | support.run_unittest( |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame] | 474 | UncompressedZipImportTestCase, |
| 475 | CompressedZipImportTestCase, |
| 476 | BadFileZipImportTestCase, |
| 477 | ) |
| 478 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 479 | support.unlink(TESTMOD) |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 480 | |
| 481 | if __name__ == "__main__": |
| 482 | test_main() |