annotate Lib/test/test_zipimport.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents a043d8e168b3
children 9e13869b7639
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
1 import sys
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
2 import os
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
3 import marshal
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
4 import imp
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
5 import struct
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
6 import time
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
7 import unittest
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
8
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
9 from test import support
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
10 from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
11
55921
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
12 # some tests can be ran even without zlib
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
13 try:
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
14 import zlib
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
15 except ImportError:
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
16 zlib = None
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
17
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
18 from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
19
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
20 import zipimport
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
21 import linecache
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
22 import doctest
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
23 import inspect
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42428
diff changeset
24 import io
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
25 from traceback import extract_tb, extract_stack, print_tb
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
26 raise_src = 'def do_raise(): raise TypeError\n'
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
27
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
28 def make_pyc(co, mtime):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
29 data = marshal.dumps(co)
27168
ccf59f1ab431 Various tweaks to make the test work on the Mac.
Jack Jansen <jack.jansen@cwi.nl>
parents: 27047
diff changeset
30 if type(mtime) is type(0.0):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
31 # Mac mtimes need a bit of special casing
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
32 if mtime < 0x7fffffff:
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
33 mtime = int(mtime)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
34 else:
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
35 mtime = int(-0x100000000 + int(mtime))
27168
ccf59f1ab431 Various tweaks to make the test work on the Mac.
Jack Jansen <jack.jansen@cwi.nl>
parents: 27047
diff changeset
36 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
37 return pyc
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
38
36393
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
39 def module_path_to_dotted_name(path):
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
40 return path.replace(os.sep, '.')
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
41
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
42 NOW = time.time()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
43 test_pyc = make_pyc(test_co, NOW)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
44
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
45
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
46 TESTMOD = "ziptestmodule"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
47 TESTPACK = "ziptestpackage"
27021
727bcb811c50 Ugh, zipimport is virtually broken in 2.3a1 :-( It worked by accident in
Just van Rossum <just@letterror.com>
parents: 26885
diff changeset
48 TESTPACK2 = "ziptestpackage2"
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 42501
diff changeset
49 TEMP_ZIP = os.path.abspath("junk95142.zip")
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
50
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
51 pyc_file = imp.cache_from_source(TESTMOD + '.py')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
52 pyc_ext = ('.pyc' if __debug__ else '.pyo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
53
55921
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
54
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
55 class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
56
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
57 compression = ZIP_STORED
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
58
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
59 def setUp(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
60 # We're reusing the zip archive path, so we must clear the
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
61 # cached directory info and linecache
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
62 linecache.clearcache()
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
63 zipimport._zip_directory_cache.clear()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
64 ImportHooksBaseTestCase.setUp(self)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
65
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
66 def doTest(self, expected_ext, files, *modules, **kw):
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
67 z = ZipFile(TEMP_ZIP, "w")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
68 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
69 for name, (mtime, data) in files.items():
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
70 zinfo = ZipInfo(name, time.localtime(mtime))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
71 zinfo.compress_type = self.compression
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
72 z.writestr(zinfo, data)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
73 z.close()
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
74
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
75 stuff = kw.get("stuff", None)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
76 if stuff is not None:
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
77 # Prepend 'stuff' to the start of the zipfile
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
78 with open(TEMP_ZIP, "rb") as f:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
79 data = f.read()
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
80 with open(TEMP_ZIP, "wb") as f:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
81 f.write(stuff)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
82 f.write(data)
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
83
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
84 sys.path.insert(0, TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
85
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
86 mod = __import__(".".join(modules), globals(), locals(),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
87 ["__dummy__"])
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
88
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
89 call = kw.get('call')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
90 if call is not None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
91 call(mod)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
92
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
93 if expected_ext:
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
94 file = mod.get_file()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
95 self.assertEqual(file, os.path.join(TEMP_ZIP,
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
96 *modules) + expected_ext)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
97 finally:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
98 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
99 os.remove(TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
100
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
101 def testAFakeZlib(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
102 #
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
103 # This could cause a stack overflow before: importing zlib.py
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
104 # from a compressed archive would cause zlib to be imported
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
105 # which would find zlib.py in the archive, which would... etc.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
106 #
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
107 # This test *must* be executed first: it must be the first one
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
108 # to trigger zipimport to import zlib (zipimport caches the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
109 # zlib.decompress function object, after which the problem being
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
110 # tested here wouldn't be a problem anymore...
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
111 # (Hence the 'A' in the test method name: to make it the first
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
112 # item in a list sorted by name, like unittest.makeSuite() does.)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
113 #
30946
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
114 # This test fails on platforms on which the zlib module is
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
115 # statically linked, but the problem it tests for can't
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
116 # occur in that case (builtin modules are always found first),
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
117 # so we'll simply skip it then. Bug #765456.
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
118 #
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
119 if "zlib" in sys.builtin_module_names:
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
120 return
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
121 if "zlib" in sys.modules:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
122 del sys.modules["zlib"]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
123 files = {"zlib.py": (NOW, test_src)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
124 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
125 self.doTest(".py", files, "zlib")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
126 except ImportError:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
127 if self.compression != ZIP_DEFLATED:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
128 self.fail("expected test to not raise ImportError")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
129 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
130 if self.compression != ZIP_STORED:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
131 self.fail("expected test to raise ImportError")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
132
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
133 def testPy(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
134 files = {TESTMOD + ".py": (NOW, test_src)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
135 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
136
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
137 def testPyc(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
138 files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
139 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
140
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
141 def testBoth(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
142 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
143 TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
144 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
145
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
146 def testEmptyPy(self):
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
147 files = {TESTMOD + ".py": (NOW, "")}
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
148 self.doTest(None, files, TESTMOD)
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
149
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
150 def testBadMagic(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
151 # make pyc magic word invalid, forcing loading from .py
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
152 badmagic_pyc = bytearray(test_pyc)
42428
81ade0144247 SF patch# 1766592 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
153 badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
154 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
155 TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
156 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
157
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
158 def testBadMagic2(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
159 # make pyc magic word invalid, causing an ImportError
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
160 badmagic_pyc = bytearray(test_pyc)
42428
81ade0144247 SF patch# 1766592 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
161 badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
162 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
163 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
164 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
165 except ImportError:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
166 pass
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
167 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
168 self.fail("expected ImportError; import from bad pyc")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
169
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
170 def testBadMTime(self):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
171 badtime_pyc = bytearray(test_pyc)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
172 # flip the second bit -- not the first as that one isn't stored in the
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
173 # .py's mtime in the zip archive.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
174 badtime_pyc[7] ^= 0x02
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
175 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
176 TESTMOD + pyc_ext: (NOW, badtime_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
177 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
178
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
179 def testPackage(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
180 packdir = TESTPACK + os.sep
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
181 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
182 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
183 self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
184
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
185 def testDeepPackage(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
186 packdir = TESTPACK + os.sep
27021
727bcb811c50 Ugh, zipimport is virtually broken in 2.3a1 :-( It worked by accident in
Just van Rossum <just@letterror.com>
parents: 26885
diff changeset
187 packdir2 = packdir + TESTPACK2 + os.sep
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
188 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
189 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
190 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
27021
727bcb811c50 Ugh, zipimport is virtually broken in 2.3a1 :-( It worked by accident in
Just van Rossum <just@letterror.com>
parents: 26885
diff changeset
191 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
192
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
193 def testZipImporterMethods(self):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
194 packdir = TESTPACK + os.sep
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
195 packdir2 = packdir + TESTPACK2 + os.sep
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
196 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
197 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
198 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
199
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
200 z = ZipFile(TEMP_ZIP, "w")
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
201 try:
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
202 for name, (mtime, data) in files.items():
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
203 zinfo = ZipInfo(name, time.localtime(mtime))
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
204 zinfo.compress_type = self.compression
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
205 z.writestr(zinfo, data)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
206 z.close()
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
207
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
208 zi = zipimport.zipimporter(TEMP_ZIP)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
209 self.assertEqual(zi.archive, TEMP_ZIP)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
210 self.assertEqual(zi.is_package(TESTPACK), True)
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
211 mod = zi.load_module(TESTPACK)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
212 self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
36386
3b4b4454fe86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36384
diff changeset
213
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
214 self.assertEqual(zi.is_package(packdir + '__init__'), False)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
215 self.assertEqual(zi.is_package(packdir + TESTPACK2), True)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
216 self.assertEqual(zi.is_package(packdir2 + TESTMOD), False)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
217
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
218 mod_path = packdir2 + TESTMOD
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
219 mod_name = module_path_to_dotted_name(mod_path)
59613
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58415
diff changeset
220 __import__(mod_name)
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
221 mod = sys.modules[mod_name]
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
222 self.assertEqual(zi.get_source(TESTPACK), None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
223 self.assertEqual(zi.get_source(mod_path), None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
224 self.assertEqual(zi.get_filename(mod_path), mod.__file__)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
225 # To pass in the module name instead of the path, we must use the
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
226 # right importer
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
227 loader = mod.__loader__
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
228 self.assertEqual(loader.get_source(mod_name), None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
229 self.assertEqual(loader.get_filename(mod_name), mod.__file__)
44536
ac7deb1c1d3c Merged revisions 59774-59783 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43947
diff changeset
230
ac7deb1c1d3c Merged revisions 59774-59783 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43947
diff changeset
231 # test prefix and archivepath members
ac7deb1c1d3c Merged revisions 59774-59783 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43947
diff changeset
232 zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
233 self.assertEqual(zi2.archive, TEMP_ZIP)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
234 self.assertEqual(zi2.prefix, TESTPACK + os.sep)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
235 finally:
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
236 z.close()
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
237 os.remove(TEMP_ZIP)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
238
47072
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
239 def testZipImporterMethodsInSubDirectory(self):
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
240 packdir = TESTPACK + os.sep
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
241 packdir2 = packdir + TESTPACK2 + os.sep
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
242 files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
243 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
244
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
245 z = ZipFile(TEMP_ZIP, "w")
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
246 try:
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
247 for name, (mtime, data) in files.items():
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
248 zinfo = ZipInfo(name, time.localtime(mtime))
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
249 zinfo.compress_type = self.compression
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
250 z.writestr(zinfo, data)
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
251 z.close()
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
252
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
253 zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
254 self.assertEqual(zi.archive, TEMP_ZIP)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
255 self.assertEqual(zi.prefix, packdir)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
256 self.assertEqual(zi.is_package(TESTPACK2), True)
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
257 mod = zi.load_module(TESTPACK2)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
258 self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)
47072
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
259
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64093
diff changeset
260 self.assertEqual(
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
261 zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64093
diff changeset
262 self.assertEqual(
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
263 zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
47072
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
264
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
265 mod_path = TESTPACK2 + os.sep + TESTMOD
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
266 mod_name = module_path_to_dotted_name(mod_path)
59613
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58415
diff changeset
267 __import__(mod_name)
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
268 mod = sys.modules[mod_name]
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
269 self.assertEqual(zi.get_source(TESTPACK2), None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
270 self.assertEqual(zi.get_source(mod_path), None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
271 self.assertEqual(zi.get_filename(mod_path), mod.__file__)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
272 # To pass in the module name instead of the path, we must use the
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59613
diff changeset
273 # right importer
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
274 loader = mod.__loader__
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
275 self.assertEqual(loader.get_source(mod_name), None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
276 self.assertEqual(loader.get_filename(mod_name), mod.__file__)
47072
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
277 finally:
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
278 z.close()
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
279 os.remove(TEMP_ZIP)
0c7142c56d7e Merged revisions 63066-63076,63079,63081-63085,63087-63097,63099,63101-63104 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44536
diff changeset
280
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
281 def testGetData(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
282 z = ZipFile(TEMP_ZIP, "w")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
283 z.compression = self.compression
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
284 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
285 name = "testdata.dat"
42428
81ade0144247 SF patch# 1766592 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
286 data = bytes(x for x in range(256))
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
287 z.writestr(name, data)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
288 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
289 zi = zipimport.zipimporter(TEMP_ZIP)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
290 self.assertEqual(data, zi.get_data(name))
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55921
diff changeset
291 self.assertIn('zipimporter object', repr(zi))
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
292 finally:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
293 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
294 os.remove(TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
295
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
296 def testImporterAttr(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
297 src = """if 1: # indent hack
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
298 def get_file():
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
299 return __file__
42428
81ade0144247 SF patch# 1766592 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
300 if __loader__.get_data("some.data") != b"some data":
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42900
diff changeset
301 raise AssertionError("bad data")\n"""
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
302 pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
303 files = {TESTMOD + pyc_ext: (NOW, pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
304 "some.data": (NOW, "some data")}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
305 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
306
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
307 def testImport_WithStuff(self):
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
308 # try importing from a zipfile which contains additional
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
309 # stuff at the beginning of the file
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
310 files = {TESTMOD + ".py": (NOW, test_src)}
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
311 self.doTest(".py", files, TESTMOD,
42900
19196cce1431 Changes preparing for stricter enforcement of bytes vs. str.
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
312 stuff=b"Some Stuff"*31)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
313
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
314 def assertModuleSource(self, module):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
315 self.assertEqual(inspect.getsource(module), test_src)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
316
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
317 def testGetSource(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
318 files = {TESTMOD + ".py": (NOW, test_src)}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
319 self.doTest(".py", files, TESTMOD, call=self.assertModuleSource)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
320
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
321 def testGetCompiledSource(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
322 pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
323 files = {TESTMOD + ".py": (NOW, test_src),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
324 TESTMOD + pyc_ext: (NOW, pyc)}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
325 self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
326
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
327 def runDoctest(self, callback):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
328 files = {TESTMOD + ".py": (NOW, test_src),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
329 "xyz.txt": (NOW, ">>> log.append(True)\n")}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
330 self.doTest(".py", files, TESTMOD, call=callback)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
331
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
332 def doDoctestFile(self, module):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
333 log = []
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
334 old_master, doctest.master = doctest.master, None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
335 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
336 doctest.testfile(
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
337 'xyz.txt', package=module, module_relative=True,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
338 globs=locals()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
339 )
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
340 finally:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
341 doctest.master = old_master
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
342 self.assertEqual(log,[True])
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
343
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
344 def testDoctestFile(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
345 self.runDoctest(self.doDoctestFile)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
346
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
347 def doDoctestSuite(self, module):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
348 log = []
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
349 doctest.DocFileTest(
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
350 'xyz.txt', package=module, module_relative=True,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
351 globs=locals()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
352 ).run()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
353 self.assertEqual(log,[True])
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
354
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
355 def testDoctestSuite(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
356 self.runDoctest(self.doDoctestSuite)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
357
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
358 def doTraceback(self, module):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
359 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
360 module.do_raise()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
361 except:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
362 tb = sys.exc_info()[2].tb_next
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
363
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
364 f,lno,n,line = extract_tb(tb, 1)[0]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
365 self.assertEqual(line, raise_src.strip())
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
366
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
367 f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
368 self.assertEqual(line, raise_src.strip())
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
369
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42428
diff changeset
370 s = io.StringIO()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
371 print_tb(tb, 1, s)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51456
diff changeset
372 self.assertTrue(s.getvalue().endswith(raise_src))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
373 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
374 raise AssertionError("This ought to be impossible")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
375
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
376 def testTraceback(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
377 files = {TESTMOD + ".py": (NOW, raise_src)}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
378 self.doTest(None, files, TESTMOD, call=self.doTraceback)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
379
64065
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
380 @unittest.skipIf(support.TESTFN_UNENCODABLE is None,
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
381 "need an unencodable filename")
64093
0498d68dad5d test_zipimport: fix test name
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64065
diff changeset
382 def testUnencodable(self):
64065
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
383 filename = support.TESTFN_UNENCODABLE + ".zip"
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
384 z = ZipFile(filename, "w")
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
385 zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW))
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
386 zinfo.compress_type = self.compression
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
387 z.writestr(zinfo, test_src)
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
388 z.close()
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
389 try:
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
390 zipimport.zipimporter(filename)
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
391 finally:
fc89b8deff1e Issue #9425: read_directory() is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60648
diff changeset
392 os.remove(filename)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
393
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36393
diff changeset
394
55921
5111d3299741 Merged revisions 74754 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
395 @unittest.skipUnless(zlib, "requires zlib")
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
396 class CompressedZipImportTestCase(UncompressedZipImportTestCase):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
397 compression = ZIP_DEFLATED
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
398
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
399
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
400 class BadFileZipImportTestCase(unittest.TestCase):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
401 def assertZipFailure(self, filename):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
402 self.assertRaises(zipimport.ZipImportError,
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
403 zipimport.zipimporter, filename)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
404
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
405 def testNoFile(self):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
406 self.assertZipFailure('AdfjdkFJKDFJjdklfjs')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
407
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
408 def testEmptyFilename(self):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
409 self.assertZipFailure('')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
410
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
411 def testBadArgs(self):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
412 self.assertRaises(TypeError, zipimport.zipimporter, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
413 self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
414
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
415 def testFilenameTooLong(self):
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
416 self.assertZipFailure('A' * 33000)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
417
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
418 def testEmptyFile(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
419 support.unlink(TESTMOD)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
420 open(TESTMOD, 'w+').close()
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
421 self.assertZipFailure(TESTMOD)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
422
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
423 def testFileUnreadable(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
424 support.unlink(TESTMOD)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
425 fd = os.open(TESTMOD, os.O_CREAT, 000)
36393
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
426 try:
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
427 os.close(fd)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
428 self.assertZipFailure(TESTMOD)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
429 finally:
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
430 # If we leave "the read-only bit" set on Windows, nothing can
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
431 # delete TESTMOD, and later tests suffer bogus failures.
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
432 os.chmod(TESTMOD, 0o666)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
433 support.unlink(TESTMOD)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
434
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
435 def testNotZipFile(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
436 support.unlink(TESTMOD)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
437 fp = open(TESTMOD, 'w+')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
438 fp.write('a' * 22)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
439 fp.close()
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
440 self.assertZipFailure(TESTMOD)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
441
36384
4f90c7c05364 Disable this test until I can test on big-endian machines and get passing
Neal Norwitz <nnorwitz@gmail.com>
parents: 36383
diff changeset
442 # XXX: disabled until this works on Big-endian machines
4f90c7c05364 Disable this test until I can test on big-endian machines and get passing
Neal Norwitz <nnorwitz@gmail.com>
parents: 36383
diff changeset
443 def _testBogusZipFile(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
444 support.unlink(TESTMOD)
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
445 fp = open(TESTMOD, 'w+')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
446 fp.write(struct.pack('=I', 0x06054B50))
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
447 fp.write('a' * 18)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
448 fp.close()
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
449 z = zipimport.zipimporter(TESTMOD)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
450
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
451 try:
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
452 self.assertRaises(TypeError, z.find_module, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
453 self.assertRaises(TypeError, z.load_module, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
454 self.assertRaises(TypeError, z.is_package, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
455 self.assertRaises(TypeError, z.get_code, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
456 self.assertRaises(TypeError, z.get_data, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
457 self.assertRaises(TypeError, z.get_source, None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
458
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
459 error = zipimport.ZipImportError
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
460 self.assertEqual(z.find_module('abc'), None)
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
461
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
462 self.assertRaises(error, z.load_module, 'abc')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
463 self.assertRaises(error, z.get_code, 'abc')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
464 self.assertRaises(IOError, z.get_data, 'abc')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
465 self.assertRaises(error, z.get_source, 'abc')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
466 self.assertRaises(error, z.is_package, 'abc')
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
467 finally:
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
468 zipimport._zip_directory_cache.clear()
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
469
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
470
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
471 def test_main():
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
472 try:
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
473 support.run_unittest(
36383
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
474 UncompressedZipImportTestCase,
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
475 CompressedZipImportTestCase,
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
476 BadFileZipImportTestCase,
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
477 )
7b3d915b6e9d Test zipimporter a bit more. Also get working with -R :: option for finding ref leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 30946
diff changeset
478 finally:
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47072
diff changeset
479 support.unlink(TESTMOD)
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
480
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
481 if __name__ == "__main__":
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
482 test_main()