1 | n/a | import sys |
---|
2 | n/a | import os |
---|
3 | n/a | import marshal |
---|
4 | n/a | import importlib |
---|
5 | n/a | import importlib.util |
---|
6 | n/a | import struct |
---|
7 | n/a | import time |
---|
8 | n/a | import unittest |
---|
9 | n/a | |
---|
10 | n/a | from test import support |
---|
11 | n/a | |
---|
12 | n/a | from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED |
---|
13 | n/a | |
---|
14 | n/a | import zipimport |
---|
15 | n/a | import linecache |
---|
16 | n/a | import doctest |
---|
17 | n/a | import inspect |
---|
18 | n/a | import io |
---|
19 | n/a | from traceback import extract_tb, extract_stack, print_tb |
---|
20 | n/a | |
---|
21 | n/a | test_src = """\ |
---|
22 | n/a | def get_name(): |
---|
23 | n/a | return __name__ |
---|
24 | n/a | def get_file(): |
---|
25 | n/a | return __file__ |
---|
26 | n/a | """ |
---|
27 | n/a | test_co = compile(test_src, "<???>", "exec") |
---|
28 | n/a | raise_src = 'def do_raise(): raise TypeError\n' |
---|
29 | n/a | |
---|
30 | n/a | def make_pyc(co, mtime, size): |
---|
31 | n/a | data = marshal.dumps(co) |
---|
32 | n/a | if type(mtime) is type(0.0): |
---|
33 | n/a | # Mac mtimes need a bit of special casing |
---|
34 | n/a | if mtime < 0x7fffffff: |
---|
35 | n/a | mtime = int(mtime) |
---|
36 | n/a | else: |
---|
37 | n/a | mtime = int(-0x100000000 + int(mtime)) |
---|
38 | n/a | pyc = (importlib.util.MAGIC_NUMBER + |
---|
39 | n/a | struct.pack("<ii", int(mtime), size & 0xFFFFFFFF) + data) |
---|
40 | n/a | return pyc |
---|
41 | n/a | |
---|
42 | n/a | def module_path_to_dotted_name(path): |
---|
43 | n/a | return path.replace(os.sep, '.') |
---|
44 | n/a | |
---|
45 | n/a | NOW = time.time() |
---|
46 | n/a | test_pyc = make_pyc(test_co, NOW, len(test_src)) |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | TESTMOD = "ziptestmodule" |
---|
50 | n/a | TESTPACK = "ziptestpackage" |
---|
51 | n/a | TESTPACK2 = "ziptestpackage2" |
---|
52 | n/a | TEMP_DIR = os.path.abspath("junk95142") |
---|
53 | n/a | TEMP_ZIP = os.path.abspath("junk95142.zip") |
---|
54 | n/a | |
---|
55 | n/a | pyc_file = importlib.util.cache_from_source(TESTMOD + '.py') |
---|
56 | n/a | pyc_ext = '.pyc' |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | class ImportHooksBaseTestCase(unittest.TestCase): |
---|
60 | n/a | |
---|
61 | n/a | def setUp(self): |
---|
62 | n/a | self.path = sys.path[:] |
---|
63 | n/a | self.meta_path = sys.meta_path[:] |
---|
64 | n/a | self.path_hooks = sys.path_hooks[:] |
---|
65 | n/a | sys.path_importer_cache.clear() |
---|
66 | n/a | self.modules_before = support.modules_setup() |
---|
67 | n/a | |
---|
68 | n/a | def tearDown(self): |
---|
69 | n/a | sys.path[:] = self.path |
---|
70 | n/a | sys.meta_path[:] = self.meta_path |
---|
71 | n/a | sys.path_hooks[:] = self.path_hooks |
---|
72 | n/a | sys.path_importer_cache.clear() |
---|
73 | n/a | support.modules_cleanup(*self.modules_before) |
---|
74 | n/a | |
---|
75 | n/a | |
---|
76 | n/a | class UncompressedZipImportTestCase(ImportHooksBaseTestCase): |
---|
77 | n/a | |
---|
78 | n/a | compression = ZIP_STORED |
---|
79 | n/a | |
---|
80 | n/a | def setUp(self): |
---|
81 | n/a | # We're reusing the zip archive path, so we must clear the |
---|
82 | n/a | # cached directory info and linecache. |
---|
83 | n/a | linecache.clearcache() |
---|
84 | n/a | zipimport._zip_directory_cache.clear() |
---|
85 | n/a | ImportHooksBaseTestCase.setUp(self) |
---|
86 | n/a | |
---|
87 | n/a | def makeTree(self, files, dirName=TEMP_DIR): |
---|
88 | n/a | # Create a filesystem based set of modules/packages |
---|
89 | n/a | # defined by files under the directory dirName. |
---|
90 | n/a | self.addCleanup(support.rmtree, dirName) |
---|
91 | n/a | |
---|
92 | n/a | for name, (mtime, data) in files.items(): |
---|
93 | n/a | path = os.path.join(dirName, name) |
---|
94 | n/a | if path[-1] == os.sep: |
---|
95 | n/a | if not os.path.isdir(path): |
---|
96 | n/a | os.makedirs(path) |
---|
97 | n/a | else: |
---|
98 | n/a | dname = os.path.dirname(path) |
---|
99 | n/a | if not os.path.isdir(dname): |
---|
100 | n/a | os.makedirs(dname) |
---|
101 | n/a | with open(path, 'wb') as fp: |
---|
102 | n/a | fp.write(data) |
---|
103 | n/a | |
---|
104 | n/a | def makeZip(self, files, zipName=TEMP_ZIP, **kw): |
---|
105 | n/a | # Create a zip archive based set of modules/packages |
---|
106 | n/a | # defined by files in the zip file zipName. If the |
---|
107 | n/a | # key 'stuff' exists in kw it is prepended to the archive. |
---|
108 | n/a | self.addCleanup(support.unlink, zipName) |
---|
109 | n/a | |
---|
110 | n/a | with ZipFile(zipName, "w") as z: |
---|
111 | n/a | for name, (mtime, data) in files.items(): |
---|
112 | n/a | zinfo = ZipInfo(name, time.localtime(mtime)) |
---|
113 | n/a | zinfo.compress_type = self.compression |
---|
114 | n/a | z.writestr(zinfo, data) |
---|
115 | n/a | |
---|
116 | n/a | stuff = kw.get("stuff", None) |
---|
117 | n/a | if stuff is not None: |
---|
118 | n/a | # Prepend 'stuff' to the start of the zipfile |
---|
119 | n/a | with open(zipName, "rb") as f: |
---|
120 | n/a | data = f.read() |
---|
121 | n/a | with open(zipName, "wb") as f: |
---|
122 | n/a | f.write(stuff) |
---|
123 | n/a | f.write(data) |
---|
124 | n/a | |
---|
125 | n/a | def doTest(self, expected_ext, files, *modules, **kw): |
---|
126 | n/a | self.makeZip(files, **kw) |
---|
127 | n/a | |
---|
128 | n/a | sys.path.insert(0, TEMP_ZIP) |
---|
129 | n/a | |
---|
130 | n/a | mod = importlib.import_module(".".join(modules)) |
---|
131 | n/a | |
---|
132 | n/a | call = kw.get('call') |
---|
133 | n/a | if call is not None: |
---|
134 | n/a | call(mod) |
---|
135 | n/a | |
---|
136 | n/a | if expected_ext: |
---|
137 | n/a | file = mod.get_file() |
---|
138 | n/a | self.assertEqual(file, os.path.join(TEMP_ZIP, |
---|
139 | n/a | *modules) + expected_ext) |
---|
140 | n/a | |
---|
141 | n/a | def testAFakeZlib(self): |
---|
142 | n/a | # |
---|
143 | n/a | # This could cause a stack overflow before: importing zlib.py |
---|
144 | n/a | # from a compressed archive would cause zlib to be imported |
---|
145 | n/a | # which would find zlib.py in the archive, which would... etc. |
---|
146 | n/a | # |
---|
147 | n/a | # This test *must* be executed first: it must be the first one |
---|
148 | n/a | # to trigger zipimport to import zlib (zipimport caches the |
---|
149 | n/a | # zlib.decompress function object, after which the problem being |
---|
150 | n/a | # tested here wouldn't be a problem anymore... |
---|
151 | n/a | # (Hence the 'A' in the test method name: to make it the first |
---|
152 | n/a | # item in a list sorted by name, like unittest.makeSuite() does.) |
---|
153 | n/a | # |
---|
154 | n/a | # This test fails on platforms on which the zlib module is |
---|
155 | n/a | # statically linked, but the problem it tests for can't |
---|
156 | n/a | # occur in that case (builtin modules are always found first), |
---|
157 | n/a | # so we'll simply skip it then. Bug #765456. |
---|
158 | n/a | # |
---|
159 | n/a | if "zlib" in sys.builtin_module_names: |
---|
160 | n/a | self.skipTest('zlib is a builtin module') |
---|
161 | n/a | if "zlib" in sys.modules: |
---|
162 | n/a | del sys.modules["zlib"] |
---|
163 | n/a | files = {"zlib.py": (NOW, test_src)} |
---|
164 | n/a | try: |
---|
165 | n/a | self.doTest(".py", files, "zlib") |
---|
166 | n/a | except ImportError: |
---|
167 | n/a | if self.compression != ZIP_DEFLATED: |
---|
168 | n/a | self.fail("expected test to not raise ImportError") |
---|
169 | n/a | else: |
---|
170 | n/a | if self.compression != ZIP_STORED: |
---|
171 | n/a | self.fail("expected test to raise ImportError") |
---|
172 | n/a | |
---|
173 | n/a | def testPy(self): |
---|
174 | n/a | files = {TESTMOD + ".py": (NOW, test_src)} |
---|
175 | n/a | self.doTest(".py", files, TESTMOD) |
---|
176 | n/a | |
---|
177 | n/a | def testPyc(self): |
---|
178 | n/a | files = {TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
179 | n/a | self.doTest(pyc_ext, files, TESTMOD) |
---|
180 | n/a | |
---|
181 | n/a | def testBoth(self): |
---|
182 | n/a | files = {TESTMOD + ".py": (NOW, test_src), |
---|
183 | n/a | TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
184 | n/a | self.doTest(pyc_ext, files, TESTMOD) |
---|
185 | n/a | |
---|
186 | n/a | def testEmptyPy(self): |
---|
187 | n/a | files = {TESTMOD + ".py": (NOW, "")} |
---|
188 | n/a | self.doTest(None, files, TESTMOD) |
---|
189 | n/a | |
---|
190 | n/a | def testBadMagic(self): |
---|
191 | n/a | # make pyc magic word invalid, forcing loading from .py |
---|
192 | n/a | badmagic_pyc = bytearray(test_pyc) |
---|
193 | n/a | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
---|
194 | n/a | files = {TESTMOD + ".py": (NOW, test_src), |
---|
195 | n/a | TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |
---|
196 | n/a | self.doTest(".py", files, TESTMOD) |
---|
197 | n/a | |
---|
198 | n/a | def testBadMagic2(self): |
---|
199 | n/a | # make pyc magic word invalid, causing an ImportError |
---|
200 | n/a | badmagic_pyc = bytearray(test_pyc) |
---|
201 | n/a | badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit |
---|
202 | n/a | files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |
---|
203 | n/a | try: |
---|
204 | n/a | self.doTest(".py", files, TESTMOD) |
---|
205 | n/a | except ImportError: |
---|
206 | n/a | pass |
---|
207 | n/a | else: |
---|
208 | n/a | self.fail("expected ImportError; import from bad pyc") |
---|
209 | n/a | |
---|
210 | n/a | def testBadMTime(self): |
---|
211 | n/a | badtime_pyc = bytearray(test_pyc) |
---|
212 | n/a | # flip the second bit -- not the first as that one isn't stored in the |
---|
213 | n/a | # .py's mtime in the zip archive. |
---|
214 | n/a | badtime_pyc[7] ^= 0x02 |
---|
215 | n/a | files = {TESTMOD + ".py": (NOW, test_src), |
---|
216 | n/a | TESTMOD + pyc_ext: (NOW, badtime_pyc)} |
---|
217 | n/a | self.doTest(".py", files, TESTMOD) |
---|
218 | n/a | |
---|
219 | n/a | def testPackage(self): |
---|
220 | n/a | packdir = TESTPACK + os.sep |
---|
221 | n/a | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
---|
222 | n/a | packdir + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
223 | n/a | self.doTest(pyc_ext, files, TESTPACK, TESTMOD) |
---|
224 | n/a | |
---|
225 | n/a | def testSubPackage(self): |
---|
226 | n/a | # Test that subpackages function when loaded from zip |
---|
227 | n/a | # archives. |
---|
228 | n/a | packdir = TESTPACK + os.sep |
---|
229 | n/a | packdir2 = packdir + TESTPACK2 + os.sep |
---|
230 | n/a | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
---|
231 | n/a | packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
---|
232 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
233 | n/a | self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) |
---|
234 | n/a | |
---|
235 | n/a | def testSubNamespacePackage(self): |
---|
236 | n/a | # Test that implicit namespace subpackages function |
---|
237 | n/a | # when loaded from zip archives. |
---|
238 | n/a | packdir = TESTPACK + os.sep |
---|
239 | n/a | packdir2 = packdir + TESTPACK2 + os.sep |
---|
240 | n/a | # The first two files are just directory entries (so have no data). |
---|
241 | n/a | files = {packdir: (NOW, ""), |
---|
242 | n/a | packdir2: (NOW, ""), |
---|
243 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
244 | n/a | self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) |
---|
245 | n/a | |
---|
246 | n/a | def testMixedNamespacePackage(self): |
---|
247 | n/a | # Test implicit namespace packages spread between a |
---|
248 | n/a | # real filesystem and a zip archive. |
---|
249 | n/a | packdir = TESTPACK + os.sep |
---|
250 | n/a | packdir2 = packdir + TESTPACK2 + os.sep |
---|
251 | n/a | packdir3 = packdir2 + TESTPACK + '3' + os.sep |
---|
252 | n/a | files1 = {packdir: (NOW, ""), |
---|
253 | n/a | packdir + TESTMOD + pyc_ext: (NOW, test_pyc), |
---|
254 | n/a | packdir2: (NOW, ""), |
---|
255 | n/a | packdir3: (NOW, ""), |
---|
256 | n/a | packdir3 + TESTMOD + pyc_ext: (NOW, test_pyc), |
---|
257 | n/a | packdir2 + TESTMOD + '3' + pyc_ext: (NOW, test_pyc), |
---|
258 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
259 | n/a | files2 = {packdir: (NOW, ""), |
---|
260 | n/a | packdir + TESTMOD + '2' + pyc_ext: (NOW, test_pyc), |
---|
261 | n/a | packdir2: (NOW, ""), |
---|
262 | n/a | packdir2 + TESTMOD + '2' + pyc_ext: (NOW, test_pyc), |
---|
263 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
264 | n/a | |
---|
265 | n/a | zip1 = os.path.abspath("path1.zip") |
---|
266 | n/a | self.makeZip(files1, zip1) |
---|
267 | n/a | |
---|
268 | n/a | zip2 = TEMP_DIR |
---|
269 | n/a | self.makeTree(files2, zip2) |
---|
270 | n/a | |
---|
271 | n/a | # zip2 should override zip1. |
---|
272 | n/a | sys.path.insert(0, zip1) |
---|
273 | n/a | sys.path.insert(0, zip2) |
---|
274 | n/a | |
---|
275 | n/a | mod = importlib.import_module(TESTPACK) |
---|
276 | n/a | |
---|
277 | n/a | # if TESTPACK is functioning as a namespace pkg then |
---|
278 | n/a | # there should be two entries in the __path__. |
---|
279 | n/a | # First should be path2 and second path1. |
---|
280 | n/a | self.assertEqual(2, len(mod.__path__)) |
---|
281 | n/a | p1, p2 = mod.__path__ |
---|
282 | n/a | self.assertEqual(os.path.basename(TEMP_DIR), p1.split(os.sep)[-2]) |
---|
283 | n/a | self.assertEqual("path1.zip", p2.split(os.sep)[-2]) |
---|
284 | n/a | |
---|
285 | n/a | # packdir3 should import as a namespace package. |
---|
286 | n/a | # Its __path__ is an iterable of 1 element from zip1. |
---|
287 | n/a | mod = importlib.import_module(packdir3.replace(os.sep, '.')[:-1]) |
---|
288 | n/a | self.assertEqual(1, len(mod.__path__)) |
---|
289 | n/a | mpath = list(mod.__path__)[0].split('path1.zip' + os.sep)[1] |
---|
290 | n/a | self.assertEqual(packdir3[:-1], mpath) |
---|
291 | n/a | |
---|
292 | n/a | # TESTPACK/TESTMOD only exists in path1. |
---|
293 | n/a | mod = importlib.import_module('.'.join((TESTPACK, TESTMOD))) |
---|
294 | n/a | self.assertEqual("path1.zip", mod.__file__.split(os.sep)[-3]) |
---|
295 | n/a | |
---|
296 | n/a | # And TESTPACK/(TESTMOD + '2') only exists in path2. |
---|
297 | n/a | mod = importlib.import_module('.'.join((TESTPACK, TESTMOD + '2'))) |
---|
298 | n/a | self.assertEqual(os.path.basename(TEMP_DIR), |
---|
299 | n/a | mod.__file__.split(os.sep)[-3]) |
---|
300 | n/a | |
---|
301 | n/a | # One level deeper... |
---|
302 | n/a | subpkg = '.'.join((TESTPACK, TESTPACK2)) |
---|
303 | n/a | mod = importlib.import_module(subpkg) |
---|
304 | n/a | self.assertEqual(2, len(mod.__path__)) |
---|
305 | n/a | p1, p2 = mod.__path__ |
---|
306 | n/a | self.assertEqual(os.path.basename(TEMP_DIR), p1.split(os.sep)[-3]) |
---|
307 | n/a | self.assertEqual("path1.zip", p2.split(os.sep)[-3]) |
---|
308 | n/a | |
---|
309 | n/a | # subpkg.TESTMOD exists in both zips should load from zip2. |
---|
310 | n/a | mod = importlib.import_module('.'.join((subpkg, TESTMOD))) |
---|
311 | n/a | self.assertEqual(os.path.basename(TEMP_DIR), |
---|
312 | n/a | mod.__file__.split(os.sep)[-4]) |
---|
313 | n/a | |
---|
314 | n/a | # subpkg.TESTMOD + '2' only exists in zip2. |
---|
315 | n/a | mod = importlib.import_module('.'.join((subpkg, TESTMOD + '2'))) |
---|
316 | n/a | self.assertEqual(os.path.basename(TEMP_DIR), |
---|
317 | n/a | mod.__file__.split(os.sep)[-4]) |
---|
318 | n/a | |
---|
319 | n/a | # Finally subpkg.TESTMOD + '3' only exists in zip1. |
---|
320 | n/a | mod = importlib.import_module('.'.join((subpkg, TESTMOD + '3'))) |
---|
321 | n/a | self.assertEqual('path1.zip', mod.__file__.split(os.sep)[-4]) |
---|
322 | n/a | |
---|
323 | n/a | def testNamespacePackage(self): |
---|
324 | n/a | # Test implicit namespace packages spread between multiple zip |
---|
325 | n/a | # archives. |
---|
326 | n/a | packdir = TESTPACK + os.sep |
---|
327 | n/a | packdir2 = packdir + TESTPACK2 + os.sep |
---|
328 | n/a | packdir3 = packdir2 + TESTPACK + '3' + os.sep |
---|
329 | n/a | files1 = {packdir: (NOW, ""), |
---|
330 | n/a | packdir + TESTMOD + pyc_ext: (NOW, test_pyc), |
---|
331 | n/a | packdir2: (NOW, ""), |
---|
332 | n/a | packdir3: (NOW, ""), |
---|
333 | n/a | packdir3 + TESTMOD + pyc_ext: (NOW, test_pyc), |
---|
334 | n/a | packdir2 + TESTMOD + '3' + pyc_ext: (NOW, test_pyc), |
---|
335 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
336 | n/a | zip1 = os.path.abspath("path1.zip") |
---|
337 | n/a | self.makeZip(files1, zip1) |
---|
338 | n/a | |
---|
339 | n/a | files2 = {packdir: (NOW, ""), |
---|
340 | n/a | packdir + TESTMOD + '2' + pyc_ext: (NOW, test_pyc), |
---|
341 | n/a | packdir2: (NOW, ""), |
---|
342 | n/a | packdir2 + TESTMOD + '2' + pyc_ext: (NOW, test_pyc), |
---|
343 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
344 | n/a | zip2 = os.path.abspath("path2.zip") |
---|
345 | n/a | self.makeZip(files2, zip2) |
---|
346 | n/a | |
---|
347 | n/a | # zip2 should override zip1. |
---|
348 | n/a | sys.path.insert(0, zip1) |
---|
349 | n/a | sys.path.insert(0, zip2) |
---|
350 | n/a | |
---|
351 | n/a | mod = importlib.import_module(TESTPACK) |
---|
352 | n/a | |
---|
353 | n/a | # if TESTPACK is functioning as a namespace pkg then |
---|
354 | n/a | # there should be two entries in the __path__. |
---|
355 | n/a | # First should be path2 and second path1. |
---|
356 | n/a | self.assertEqual(2, len(mod.__path__)) |
---|
357 | n/a | p1, p2 = mod.__path__ |
---|
358 | n/a | self.assertEqual("path2.zip", p1.split(os.sep)[-2]) |
---|
359 | n/a | self.assertEqual("path1.zip", p2.split(os.sep)[-2]) |
---|
360 | n/a | |
---|
361 | n/a | # packdir3 should import as a namespace package. |
---|
362 | n/a | # Tts __path__ is an iterable of 1 element from zip1. |
---|
363 | n/a | mod = importlib.import_module(packdir3.replace(os.sep, '.')[:-1]) |
---|
364 | n/a | self.assertEqual(1, len(mod.__path__)) |
---|
365 | n/a | mpath = list(mod.__path__)[0].split('path1.zip' + os.sep)[1] |
---|
366 | n/a | self.assertEqual(packdir3[:-1], mpath) |
---|
367 | n/a | |
---|
368 | n/a | # TESTPACK/TESTMOD only exists in path1. |
---|
369 | n/a | mod = importlib.import_module('.'.join((TESTPACK, TESTMOD))) |
---|
370 | n/a | self.assertEqual("path1.zip", mod.__file__.split(os.sep)[-3]) |
---|
371 | n/a | |
---|
372 | n/a | # And TESTPACK/(TESTMOD + '2') only exists in path2. |
---|
373 | n/a | mod = importlib.import_module('.'.join((TESTPACK, TESTMOD + '2'))) |
---|
374 | n/a | self.assertEqual("path2.zip", mod.__file__.split(os.sep)[-3]) |
---|
375 | n/a | |
---|
376 | n/a | # One level deeper... |
---|
377 | n/a | subpkg = '.'.join((TESTPACK, TESTPACK2)) |
---|
378 | n/a | mod = importlib.import_module(subpkg) |
---|
379 | n/a | self.assertEqual(2, len(mod.__path__)) |
---|
380 | n/a | p1, p2 = mod.__path__ |
---|
381 | n/a | self.assertEqual("path2.zip", p1.split(os.sep)[-3]) |
---|
382 | n/a | self.assertEqual("path1.zip", p2.split(os.sep)[-3]) |
---|
383 | n/a | |
---|
384 | n/a | # subpkg.TESTMOD exists in both zips should load from zip2. |
---|
385 | n/a | mod = importlib.import_module('.'.join((subpkg, TESTMOD))) |
---|
386 | n/a | self.assertEqual('path2.zip', mod.__file__.split(os.sep)[-4]) |
---|
387 | n/a | |
---|
388 | n/a | # subpkg.TESTMOD + '2' only exists in zip2. |
---|
389 | n/a | mod = importlib.import_module('.'.join((subpkg, TESTMOD + '2'))) |
---|
390 | n/a | self.assertEqual('path2.zip', mod.__file__.split(os.sep)[-4]) |
---|
391 | n/a | |
---|
392 | n/a | # Finally subpkg.TESTMOD + '3' only exists in zip1. |
---|
393 | n/a | mod = importlib.import_module('.'.join((subpkg, TESTMOD + '3'))) |
---|
394 | n/a | self.assertEqual('path1.zip', mod.__file__.split(os.sep)[-4]) |
---|
395 | n/a | |
---|
396 | n/a | def testZipImporterMethods(self): |
---|
397 | n/a | packdir = TESTPACK + os.sep |
---|
398 | n/a | packdir2 = packdir + TESTPACK2 + os.sep |
---|
399 | n/a | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
---|
400 | n/a | packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
---|
401 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc), |
---|
402 | n/a | "spam" + pyc_ext: (NOW, test_pyc)} |
---|
403 | n/a | |
---|
404 | n/a | z = ZipFile(TEMP_ZIP, "w") |
---|
405 | n/a | try: |
---|
406 | n/a | for name, (mtime, data) in files.items(): |
---|
407 | n/a | zinfo = ZipInfo(name, time.localtime(mtime)) |
---|
408 | n/a | zinfo.compress_type = self.compression |
---|
409 | n/a | zinfo.comment = b"spam" |
---|
410 | n/a | z.writestr(zinfo, data) |
---|
411 | n/a | z.close() |
---|
412 | n/a | |
---|
413 | n/a | zi = zipimport.zipimporter(TEMP_ZIP) |
---|
414 | n/a | self.assertEqual(zi.archive, TEMP_ZIP) |
---|
415 | n/a | self.assertEqual(zi.is_package(TESTPACK), True) |
---|
416 | n/a | |
---|
417 | n/a | find_mod = zi.find_module('spam') |
---|
418 | n/a | self.assertIsNotNone(find_mod) |
---|
419 | n/a | self.assertIsInstance(find_mod, zipimport.zipimporter) |
---|
420 | n/a | self.assertFalse(find_mod.is_package('spam')) |
---|
421 | n/a | load_mod = find_mod.load_module('spam') |
---|
422 | n/a | self.assertEqual(find_mod.get_filename('spam'), load_mod.__file__) |
---|
423 | n/a | |
---|
424 | n/a | mod = zi.load_module(TESTPACK) |
---|
425 | n/a | self.assertEqual(zi.get_filename(TESTPACK), mod.__file__) |
---|
426 | n/a | |
---|
427 | n/a | existing_pack_path = importlib.import_module(TESTPACK).__path__[0] |
---|
428 | n/a | expected_path_path = os.path.join(TEMP_ZIP, TESTPACK) |
---|
429 | n/a | self.assertEqual(existing_pack_path, expected_path_path) |
---|
430 | n/a | |
---|
431 | n/a | self.assertEqual(zi.is_package(packdir + '__init__'), False) |
---|
432 | n/a | self.assertEqual(zi.is_package(packdir + TESTPACK2), True) |
---|
433 | n/a | self.assertEqual(zi.is_package(packdir2 + TESTMOD), False) |
---|
434 | n/a | |
---|
435 | n/a | mod_path = packdir2 + TESTMOD |
---|
436 | n/a | mod_name = module_path_to_dotted_name(mod_path) |
---|
437 | n/a | mod = importlib.import_module(mod_name) |
---|
438 | n/a | self.assertTrue(mod_name in sys.modules) |
---|
439 | n/a | self.assertEqual(zi.get_source(TESTPACK), None) |
---|
440 | n/a | self.assertEqual(zi.get_source(mod_path), None) |
---|
441 | n/a | self.assertEqual(zi.get_filename(mod_path), mod.__file__) |
---|
442 | n/a | # To pass in the module name instead of the path, we must use the |
---|
443 | n/a | # right importer |
---|
444 | n/a | loader = mod.__loader__ |
---|
445 | n/a | self.assertEqual(loader.get_source(mod_name), None) |
---|
446 | n/a | self.assertEqual(loader.get_filename(mod_name), mod.__file__) |
---|
447 | n/a | |
---|
448 | n/a | # test prefix and archivepath members |
---|
449 | n/a | zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK) |
---|
450 | n/a | self.assertEqual(zi2.archive, TEMP_ZIP) |
---|
451 | n/a | self.assertEqual(zi2.prefix, TESTPACK + os.sep) |
---|
452 | n/a | finally: |
---|
453 | n/a | z.close() |
---|
454 | n/a | os.remove(TEMP_ZIP) |
---|
455 | n/a | |
---|
456 | n/a | def testZipImporterMethodsInSubDirectory(self): |
---|
457 | n/a | packdir = TESTPACK + os.sep |
---|
458 | n/a | packdir2 = packdir + TESTPACK2 + os.sep |
---|
459 | n/a | files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
---|
460 | n/a | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
---|
461 | n/a | |
---|
462 | n/a | z = ZipFile(TEMP_ZIP, "w") |
---|
463 | n/a | try: |
---|
464 | n/a | for name, (mtime, data) in files.items(): |
---|
465 | n/a | zinfo = ZipInfo(name, time.localtime(mtime)) |
---|
466 | n/a | zinfo.compress_type = self.compression |
---|
467 | n/a | zinfo.comment = b"eggs" |
---|
468 | n/a | z.writestr(zinfo, data) |
---|
469 | n/a | z.close() |
---|
470 | n/a | |
---|
471 | n/a | zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir) |
---|
472 | n/a | self.assertEqual(zi.archive, TEMP_ZIP) |
---|
473 | n/a | self.assertEqual(zi.prefix, packdir) |
---|
474 | n/a | self.assertEqual(zi.is_package(TESTPACK2), True) |
---|
475 | n/a | mod = zi.load_module(TESTPACK2) |
---|
476 | n/a | self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__) |
---|
477 | n/a | |
---|
478 | n/a | self.assertEqual( |
---|
479 | n/a | zi.is_package(TESTPACK2 + os.sep + '__init__'), False) |
---|
480 | n/a | self.assertEqual( |
---|
481 | n/a | zi.is_package(TESTPACK2 + os.sep + TESTMOD), False) |
---|
482 | n/a | |
---|
483 | n/a | pkg_path = TEMP_ZIP + os.sep + packdir + TESTPACK2 |
---|
484 | n/a | zi2 = zipimport.zipimporter(pkg_path) |
---|
485 | n/a | find_mod_dotted = zi2.find_module(TESTMOD) |
---|
486 | n/a | self.assertIsNotNone(find_mod_dotted) |
---|
487 | n/a | self.assertIsInstance(find_mod_dotted, zipimport.zipimporter) |
---|
488 | n/a | self.assertFalse(zi2.is_package(TESTMOD)) |
---|
489 | n/a | load_mod = find_mod_dotted.load_module(TESTMOD) |
---|
490 | n/a | self.assertEqual( |
---|
491 | n/a | find_mod_dotted.get_filename(TESTMOD), load_mod.__file__) |
---|
492 | n/a | |
---|
493 | n/a | mod_path = TESTPACK2 + os.sep + TESTMOD |
---|
494 | n/a | mod_name = module_path_to_dotted_name(mod_path) |
---|
495 | n/a | mod = importlib.import_module(mod_name) |
---|
496 | n/a | self.assertTrue(mod_name in sys.modules) |
---|
497 | n/a | self.assertEqual(zi.get_source(TESTPACK2), None) |
---|
498 | n/a | self.assertEqual(zi.get_source(mod_path), None) |
---|
499 | n/a | self.assertEqual(zi.get_filename(mod_path), mod.__file__) |
---|
500 | n/a | # To pass in the module name instead of the path, we must use the |
---|
501 | n/a | # right importer. |
---|
502 | n/a | loader = mod.__loader__ |
---|
503 | n/a | self.assertEqual(loader.get_source(mod_name), None) |
---|
504 | n/a | self.assertEqual(loader.get_filename(mod_name), mod.__file__) |
---|
505 | n/a | finally: |
---|
506 | n/a | z.close() |
---|
507 | n/a | os.remove(TEMP_ZIP) |
---|
508 | n/a | |
---|
509 | n/a | def testGetData(self): |
---|
510 | n/a | z = ZipFile(TEMP_ZIP, "w") |
---|
511 | n/a | z.compression = self.compression |
---|
512 | n/a | try: |
---|
513 | n/a | name = "testdata.dat" |
---|
514 | n/a | data = bytes(x for x in range(256)) |
---|
515 | n/a | z.writestr(name, data) |
---|
516 | n/a | z.close() |
---|
517 | n/a | zi = zipimport.zipimporter(TEMP_ZIP) |
---|
518 | n/a | self.assertEqual(data, zi.get_data(name)) |
---|
519 | n/a | self.assertIn('zipimporter object', repr(zi)) |
---|
520 | n/a | finally: |
---|
521 | n/a | z.close() |
---|
522 | n/a | os.remove(TEMP_ZIP) |
---|
523 | n/a | |
---|
524 | n/a | def testImporterAttr(self): |
---|
525 | n/a | src = """if 1: # indent hack |
---|
526 | n/a | def get_file(): |
---|
527 | n/a | return __file__ |
---|
528 | n/a | if __loader__.get_data("some.data") != b"some data": |
---|
529 | n/a | raise AssertionError("bad data")\n""" |
---|
530 | n/a | pyc = make_pyc(compile(src, "<???>", "exec"), NOW, len(src)) |
---|
531 | n/a | files = {TESTMOD + pyc_ext: (NOW, pyc), |
---|
532 | n/a | "some.data": (NOW, "some data")} |
---|
533 | n/a | self.doTest(pyc_ext, files, TESTMOD) |
---|
534 | n/a | |
---|
535 | n/a | def testDefaultOptimizationLevel(self): |
---|
536 | n/a | # zipimport should use the default optimization level (#28131) |
---|
537 | n/a | src = """if 1: # indent hack |
---|
538 | n/a | def test(val): |
---|
539 | n/a | assert(val) |
---|
540 | n/a | return val\n""" |
---|
541 | n/a | files = {TESTMOD + '.py': (NOW, src)} |
---|
542 | n/a | self.makeZip(files) |
---|
543 | n/a | sys.path.insert(0, TEMP_ZIP) |
---|
544 | n/a | mod = importlib.import_module(TESTMOD) |
---|
545 | n/a | self.assertEqual(mod.test(1), 1) |
---|
546 | n/a | self.assertRaises(AssertionError, mod.test, False) |
---|
547 | n/a | |
---|
548 | n/a | def testImport_WithStuff(self): |
---|
549 | n/a | # try importing from a zipfile which contains additional |
---|
550 | n/a | # stuff at the beginning of the file |
---|
551 | n/a | files = {TESTMOD + ".py": (NOW, test_src)} |
---|
552 | n/a | self.doTest(".py", files, TESTMOD, |
---|
553 | n/a | stuff=b"Some Stuff"*31) |
---|
554 | n/a | |
---|
555 | n/a | def assertModuleSource(self, module): |
---|
556 | n/a | self.assertEqual(inspect.getsource(module), test_src) |
---|
557 | n/a | |
---|
558 | n/a | def testGetSource(self): |
---|
559 | n/a | files = {TESTMOD + ".py": (NOW, test_src)} |
---|
560 | n/a | self.doTest(".py", files, TESTMOD, call=self.assertModuleSource) |
---|
561 | n/a | |
---|
562 | n/a | def testGetCompiledSource(self): |
---|
563 | n/a | pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW, len(test_src)) |
---|
564 | n/a | files = {TESTMOD + ".py": (NOW, test_src), |
---|
565 | n/a | TESTMOD + pyc_ext: (NOW, pyc)} |
---|
566 | n/a | self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource) |
---|
567 | n/a | |
---|
568 | n/a | def runDoctest(self, callback): |
---|
569 | n/a | files = {TESTMOD + ".py": (NOW, test_src), |
---|
570 | n/a | "xyz.txt": (NOW, ">>> log.append(True)\n")} |
---|
571 | n/a | self.doTest(".py", files, TESTMOD, call=callback) |
---|
572 | n/a | |
---|
573 | n/a | def doDoctestFile(self, module): |
---|
574 | n/a | log = [] |
---|
575 | n/a | old_master, doctest.master = doctest.master, None |
---|
576 | n/a | try: |
---|
577 | n/a | doctest.testfile( |
---|
578 | n/a | 'xyz.txt', package=module, module_relative=True, |
---|
579 | n/a | globs=locals() |
---|
580 | n/a | ) |
---|
581 | n/a | finally: |
---|
582 | n/a | doctest.master = old_master |
---|
583 | n/a | self.assertEqual(log,[True]) |
---|
584 | n/a | |
---|
585 | n/a | def testDoctestFile(self): |
---|
586 | n/a | self.runDoctest(self.doDoctestFile) |
---|
587 | n/a | |
---|
588 | n/a | def doDoctestSuite(self, module): |
---|
589 | n/a | log = [] |
---|
590 | n/a | doctest.DocFileTest( |
---|
591 | n/a | 'xyz.txt', package=module, module_relative=True, |
---|
592 | n/a | globs=locals() |
---|
593 | n/a | ).run() |
---|
594 | n/a | self.assertEqual(log,[True]) |
---|
595 | n/a | |
---|
596 | n/a | def testDoctestSuite(self): |
---|
597 | n/a | self.runDoctest(self.doDoctestSuite) |
---|
598 | n/a | |
---|
599 | n/a | def doTraceback(self, module): |
---|
600 | n/a | try: |
---|
601 | n/a | module.do_raise() |
---|
602 | n/a | except: |
---|
603 | n/a | tb = sys.exc_info()[2].tb_next |
---|
604 | n/a | |
---|
605 | n/a | f,lno,n,line = extract_tb(tb, 1)[0] |
---|
606 | n/a | self.assertEqual(line, raise_src.strip()) |
---|
607 | n/a | |
---|
608 | n/a | f,lno,n,line = extract_stack(tb.tb_frame, 1)[0] |
---|
609 | n/a | self.assertEqual(line, raise_src.strip()) |
---|
610 | n/a | |
---|
611 | n/a | s = io.StringIO() |
---|
612 | n/a | print_tb(tb, 1, s) |
---|
613 | n/a | self.assertTrue(s.getvalue().endswith(raise_src)) |
---|
614 | n/a | else: |
---|
615 | n/a | raise AssertionError("This ought to be impossible") |
---|
616 | n/a | |
---|
617 | n/a | def testTraceback(self): |
---|
618 | n/a | files = {TESTMOD + ".py": (NOW, raise_src)} |
---|
619 | n/a | self.doTest(None, files, TESTMOD, call=self.doTraceback) |
---|
620 | n/a | |
---|
621 | n/a | @unittest.skipIf(support.TESTFN_UNENCODABLE is None, |
---|
622 | n/a | "need an unencodable filename") |
---|
623 | n/a | def testUnencodable(self): |
---|
624 | n/a | filename = support.TESTFN_UNENCODABLE + ".zip" |
---|
625 | n/a | z = ZipFile(filename, "w") |
---|
626 | n/a | zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW)) |
---|
627 | n/a | zinfo.compress_type = self.compression |
---|
628 | n/a | z.writestr(zinfo, test_src) |
---|
629 | n/a | z.close() |
---|
630 | n/a | try: |
---|
631 | n/a | zipimport.zipimporter(filename).load_module(TESTMOD) |
---|
632 | n/a | finally: |
---|
633 | n/a | os.remove(filename) |
---|
634 | n/a | |
---|
635 | n/a | def testBytesPath(self): |
---|
636 | n/a | filename = support.TESTFN + ".zip" |
---|
637 | n/a | self.addCleanup(support.unlink, filename) |
---|
638 | n/a | with ZipFile(filename, "w") as z: |
---|
639 | n/a | zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW)) |
---|
640 | n/a | zinfo.compress_type = self.compression |
---|
641 | n/a | z.writestr(zinfo, test_src) |
---|
642 | n/a | |
---|
643 | n/a | zipimport.zipimporter(filename) |
---|
644 | n/a | zipimport.zipimporter(os.fsencode(filename)) |
---|
645 | n/a | with self.assertWarns(DeprecationWarning): |
---|
646 | n/a | zipimport.zipimporter(bytearray(os.fsencode(filename))) |
---|
647 | n/a | with self.assertWarns(DeprecationWarning): |
---|
648 | n/a | zipimport.zipimporter(memoryview(os.fsencode(filename))) |
---|
649 | n/a | |
---|
650 | n/a | |
---|
651 | n/a | @support.requires_zlib |
---|
652 | n/a | class CompressedZipImportTestCase(UncompressedZipImportTestCase): |
---|
653 | n/a | compression = ZIP_DEFLATED |
---|
654 | n/a | |
---|
655 | n/a | |
---|
656 | n/a | class BadFileZipImportTestCase(unittest.TestCase): |
---|
657 | n/a | def assertZipFailure(self, filename): |
---|
658 | n/a | self.assertRaises(zipimport.ZipImportError, |
---|
659 | n/a | zipimport.zipimporter, filename) |
---|
660 | n/a | |
---|
661 | n/a | def testNoFile(self): |
---|
662 | n/a | self.assertZipFailure('AdfjdkFJKDFJjdklfjs') |
---|
663 | n/a | |
---|
664 | n/a | def testEmptyFilename(self): |
---|
665 | n/a | self.assertZipFailure('') |
---|
666 | n/a | |
---|
667 | n/a | def testBadArgs(self): |
---|
668 | n/a | self.assertRaises(TypeError, zipimport.zipimporter, None) |
---|
669 | n/a | self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None) |
---|
670 | n/a | self.assertRaises(TypeError, zipimport.zipimporter, |
---|
671 | n/a | list(os.fsencode(TESTMOD))) |
---|
672 | n/a | |
---|
673 | n/a | def testFilenameTooLong(self): |
---|
674 | n/a | self.assertZipFailure('A' * 33000) |
---|
675 | n/a | |
---|
676 | n/a | def testEmptyFile(self): |
---|
677 | n/a | support.unlink(TESTMOD) |
---|
678 | n/a | support.create_empty_file(TESTMOD) |
---|
679 | n/a | self.assertZipFailure(TESTMOD) |
---|
680 | n/a | |
---|
681 | n/a | def testFileUnreadable(self): |
---|
682 | n/a | support.unlink(TESTMOD) |
---|
683 | n/a | fd = os.open(TESTMOD, os.O_CREAT, 000) |
---|
684 | n/a | try: |
---|
685 | n/a | os.close(fd) |
---|
686 | n/a | |
---|
687 | n/a | with self.assertRaises(zipimport.ZipImportError) as cm: |
---|
688 | n/a | zipimport.zipimporter(TESTMOD) |
---|
689 | n/a | finally: |
---|
690 | n/a | # If we leave "the read-only bit" set on Windows, nothing can |
---|
691 | n/a | # delete TESTMOD, and later tests suffer bogus failures. |
---|
692 | n/a | os.chmod(TESTMOD, 0o666) |
---|
693 | n/a | support.unlink(TESTMOD) |
---|
694 | n/a | |
---|
695 | n/a | def testNotZipFile(self): |
---|
696 | n/a | support.unlink(TESTMOD) |
---|
697 | n/a | fp = open(TESTMOD, 'w+') |
---|
698 | n/a | fp.write('a' * 22) |
---|
699 | n/a | fp.close() |
---|
700 | n/a | self.assertZipFailure(TESTMOD) |
---|
701 | n/a | |
---|
702 | n/a | # XXX: disabled until this works on Big-endian machines |
---|
703 | n/a | def _testBogusZipFile(self): |
---|
704 | n/a | support.unlink(TESTMOD) |
---|
705 | n/a | fp = open(TESTMOD, 'w+') |
---|
706 | n/a | fp.write(struct.pack('=I', 0x06054B50)) |
---|
707 | n/a | fp.write('a' * 18) |
---|
708 | n/a | fp.close() |
---|
709 | n/a | z = zipimport.zipimporter(TESTMOD) |
---|
710 | n/a | |
---|
711 | n/a | try: |
---|
712 | n/a | self.assertRaises(TypeError, z.find_module, None) |
---|
713 | n/a | self.assertRaises(TypeError, z.load_module, None) |
---|
714 | n/a | self.assertRaises(TypeError, z.is_package, None) |
---|
715 | n/a | self.assertRaises(TypeError, z.get_code, None) |
---|
716 | n/a | self.assertRaises(TypeError, z.get_data, None) |
---|
717 | n/a | self.assertRaises(TypeError, z.get_source, None) |
---|
718 | n/a | |
---|
719 | n/a | error = zipimport.ZipImportError |
---|
720 | n/a | self.assertEqual(z.find_module('abc'), None) |
---|
721 | n/a | |
---|
722 | n/a | self.assertRaises(error, z.load_module, 'abc') |
---|
723 | n/a | self.assertRaises(error, z.get_code, 'abc') |
---|
724 | n/a | self.assertRaises(OSError, z.get_data, 'abc') |
---|
725 | n/a | self.assertRaises(error, z.get_source, 'abc') |
---|
726 | n/a | self.assertRaises(error, z.is_package, 'abc') |
---|
727 | n/a | finally: |
---|
728 | n/a | zipimport._zip_directory_cache.clear() |
---|
729 | n/a | |
---|
730 | n/a | |
---|
731 | n/a | def test_main(): |
---|
732 | n/a | try: |
---|
733 | n/a | support.run_unittest( |
---|
734 | n/a | UncompressedZipImportTestCase, |
---|
735 | n/a | CompressedZipImportTestCase, |
---|
736 | n/a | BadFileZipImportTestCase, |
---|
737 | n/a | ) |
---|
738 | n/a | finally: |
---|
739 | n/a | support.unlink(TESTMOD) |
---|
740 | n/a | |
---|
741 | n/a | if __name__ == "__main__": |
---|
742 | n/a | test_main() |
---|