annotate Lib/test/test_zipimport.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents bfceb8c8178a
children
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
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
9 from test import test_support
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
55920
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
12 # some tests can be ran even without zlib
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
13 try:
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
14 import zlib
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
15 except ImportError:
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
16 zlib = None
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
17
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
18 from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
19
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
20 import zipimport
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
21 import linecache
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
22 import doctest
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
23 import inspect
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
24 import StringIO
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
25 from traceback import extract_tb, extract_stack, print_tb
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
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:
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
35 mtime = int(-0x100000000L + long(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 if __debug__:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
47 pyc_ext = ".pyc"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
48 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
49 pyc_ext = ".pyo"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
50
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
51
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
52 TESTMOD = "ziptestmodule"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
53 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
54 TESTPACK2 = "ziptestpackage2"
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
55 TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip")
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
56
55920
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
57
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
58 class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
59
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
60 compression = ZIP_STORED
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
61
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
62 def setUp(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
63 # We're reusing the zip archive path, so we must clear the
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
64 # cached directory info and linecache
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
65 linecache.clearcache()
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
66 zipimport._zip_directory_cache.clear()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
67 ImportHooksBaseTestCase.setUp(self)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
68
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
69 def doTest(self, expected_ext, files, *modules, **kw):
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
70 z = ZipFile(TEMP_ZIP, "w")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
71 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
72 for name, (mtime, data) in files.items():
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
73 zinfo = ZipInfo(name, time.localtime(mtime))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
74 zinfo.compress_type = self.compression
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
75 z.writestr(zinfo, data)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
76 z.close()
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
77
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
78 stuff = kw.get("stuff", None)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
79 if stuff is not None:
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
80 # Prepend 'stuff' to the start of the zipfile
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
81 f = open(TEMP_ZIP, "rb")
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
82 data = f.read()
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
83 f.close()
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
84
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
85 f = open(TEMP_ZIP, "wb")
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
86 f.write(stuff)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
87 f.write(data)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
88 f.close()
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
89
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
90 sys.path.insert(0, TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
91
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
92 mod = __import__(".".join(modules), globals(), locals(),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
93 ["__dummy__"])
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
94
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
95 call = kw.get('call')
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
96 if call is not None:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
97 call(mod)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
98
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
99 if expected_ext:
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
100 file = mod.get_file()
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
101 self.assertEqual(file, os.path.join(TEMP_ZIP,
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
102 *modules) + expected_ext)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
103 finally:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
104 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
105 os.remove(TEMP_ZIP)
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 def testAFakeZlib(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
108 #
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
109 # This could cause a stack overflow before: importing zlib.py
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
110 # from a compressed archive would cause zlib to be imported
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
111 # which would find zlib.py in the archive, which would... etc.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
112 #
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
113 # 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
114 # to trigger zipimport to import zlib (zipimport caches the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
115 # zlib.decompress function object, after which the problem being
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
116 # tested here wouldn't be a problem anymore...
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
117 # (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
118 # item in a list sorted by name, like unittest.makeSuite() does.)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
119 #
30946
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
120 # 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
121 # 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
122 # 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
123 # 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
124 #
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
125 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
126 return
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
127 if "zlib" in sys.modules:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
128 del sys.modules["zlib"]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
129 files = {"zlib.py": (NOW, test_src)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
130 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
131 self.doTest(".py", files, "zlib")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
132 except ImportError:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
133 if self.compression != ZIP_DEFLATED:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
134 self.fail("expected test to not raise ImportError")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
135 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
136 if self.compression != ZIP_STORED:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
137 self.fail("expected test to raise ImportError")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
138
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
139 def testPy(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
140 files = {TESTMOD + ".py": (NOW, test_src)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
141 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
142
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
143 def testPyc(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
144 files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
145 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
146
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
147 def testBoth(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
148 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
149 TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
150 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
151
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
152 def testEmptyPy(self):
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
153 files = {TESTMOD + ".py": (NOW, "")}
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
154 self.doTest(None, files, TESTMOD)
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
155
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
156 def testBadMagic(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
157 # make pyc magic word invalid, forcing loading from .py
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
158 m0 = ord(test_pyc[0])
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
159 m0 ^= 0x04 # flip an arbitrary bit
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
160 badmagic_pyc = chr(m0) + test_pyc[1:]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
161 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
162 TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
163 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
164
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
165 def testBadMagic2(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
166 # make pyc magic word invalid, causing an ImportError
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
167 m0 = ord(test_pyc[0])
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
168 m0 ^= 0x04 # flip an arbitrary bit
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
169 badmagic_pyc = chr(m0) + test_pyc[1:]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
170 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
171 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
172 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
173 except ImportError:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
174 pass
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
175 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
176 self.fail("expected ImportError; import from bad pyc")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
177
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
178 def testBadMTime(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
179 t3 = ord(test_pyc[7])
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
180 t3 ^= 0x02 # flip the second bit -- not the first as that one
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
181 # isn't stored in the .py's mtime in the zip archive.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
182 badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
183 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
184 TESTMOD + pyc_ext: (NOW, badtime_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
185 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
186
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
187 def testPackage(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
188 packdir = TESTPACK + os.sep
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
189 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
190 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
191 self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
192
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
193 def testDeepPackage(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
194 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
195 packdir2 = packdir + TESTPACK2 + os.sep
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
196 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
197 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
198 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
199 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
200
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
201 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
202 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
203 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
204 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
205 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
206 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
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 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
209 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
210 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
211 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
212 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
213 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
214 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
215
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
216 zi = zipimport.zipimporter(TEMP_ZIP)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
217 self.assertEqual(zi.archive, TEMP_ZIP)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
218 self.assertEqual(zi.is_package(TESTPACK), True)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
219 mod = zi.load_module(TESTPACK)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
220 self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
36386
3b4b4454fe86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36384
diff changeset
221
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
222 self.assertEqual(zi.is_package(packdir + '__init__'), False)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
223 self.assertEqual(zi.is_package(packdir + TESTPACK2), True)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
224 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
225
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
226 mod_path = packdir2 + TESTMOD
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
227 mod_name = module_path_to_dotted_name(mod_path)
58770
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58460
diff changeset
228 __import__(mod_name)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
229 mod = sys.modules[mod_name]
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
230 self.assertEqual(zi.get_source(TESTPACK), None)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
231 self.assertEqual(zi.get_source(mod_path), None)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
232 self.assertEqual(zi.get_filename(mod_path), mod.__file__)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
233 # To pass in the module name instead of the path, we must use the right importer
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
234 loader = mod.__loader__
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
235 self.assertEqual(loader.get_source(mod_name), None)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
236 self.assertEqual(loader.get_filename(mod_name), mod.__file__)
44534
2acc652e58a1 #1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 37581
diff changeset
237
2acc652e58a1 #1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 37581
diff changeset
238 # test prefix and archivepath members
2acc652e58a1 #1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 37581
diff changeset
239 zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
240 self.assertEqual(zi2.archive, TEMP_ZIP)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
241 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
242 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
243 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
244 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
245
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
246 def testZipImporterMethodsInSubDirectory(self):
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
247 packdir = TESTPACK + os.sep
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
248 packdir2 = packdir + TESTPACK2 + os.sep
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
249 files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
250 packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
251
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
252 z = ZipFile(TEMP_ZIP, "w")
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
253 try:
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
254 for name, (mtime, data) in files.items():
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
255 zinfo = ZipInfo(name, time.localtime(mtime))
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
256 zinfo.compress_type = self.compression
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
257 z.writestr(zinfo, data)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
258 z.close()
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
259
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
260 zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
261 self.assertEqual(zi.archive, TEMP_ZIP)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
262 self.assertEqual(zi.prefix, packdir)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
263 self.assertEqual(zi.is_package(TESTPACK2), True)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
264 mod = zi.load_module(TESTPACK2)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
265 self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
266
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
267 self.assertEqual(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
268 self.assertEqual(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
269
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
270 mod_path = TESTPACK2 + os.sep + TESTMOD
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
271 mod_name = module_path_to_dotted_name(mod_path)
58770
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 58460
diff changeset
272 __import__(mod_name)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
273 mod = sys.modules[mod_name]
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
274 self.assertEqual(zi.get_source(TESTPACK2), None)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
275 self.assertEqual(zi.get_source(mod_path), None)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
276 self.assertEqual(zi.get_filename(mod_path), mod.__file__)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
277 # To pass in the module name instead of the path, we must use the right importer
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
278 loader = mod.__loader__
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
279 self.assertEqual(loader.get_source(mod_name), None)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
280 self.assertEqual(loader.get_filename(mod_name), mod.__file__)
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
281 finally:
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
282 z.close()
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
283 os.remove(TEMP_ZIP)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
284
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
285 def testGetData(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
286 z = ZipFile(TEMP_ZIP, "w")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
287 z.compression = self.compression
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
288 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
289 name = "testdata.dat"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
290 data = "".join([chr(x) for x in range(256)]) * 500
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
291 z.writestr(name, data)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
292 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
293 zi = zipimport.zipimporter(TEMP_ZIP)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58770
diff changeset
294 self.assertEqual(data, zi.get_data(name))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
295 self.assertIn('zipimporter object', repr(zi))
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
296 finally:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
297 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
298 os.remove(TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
299
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
300 def testImporterAttr(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
301 src = """if 1: # indent hack
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
302 def get_file():
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
303 return __file__
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
304 if __loader__.get_data("some.data") != "some data":
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
305 raise AssertionError, "bad data"\n"""
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
306 pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
307 files = {TESTMOD + pyc_ext: (NOW, pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
308 "some.data": (NOW, "some data")}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
309 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
310
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
311 def testImport_WithStuff(self):
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
312 # 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
313 # 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
314 files = {TESTMOD + ".py": (NOW, test_src)}
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
315 self.doTest(".py", files, TESTMOD,
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
316 stuff="Some Stuff"*31)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
317
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
318 def assertModuleSource(self, module):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
319 self.assertEqual(inspect.getsource(module), test_src)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
320
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
321 def testGetSource(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
322 files = {TESTMOD + ".py": (NOW, test_src)}
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
323 self.doTest(".py", files, TESTMOD, call=self.assertModuleSource)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
324
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
325 def testGetCompiledSource(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
326 pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)
37581
b112d8c7988c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
327 files = {TESTMOD + ".py": (NOW, test_src),
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
328 TESTMOD + pyc_ext: (NOW, pyc)}
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
329 self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
330
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
331 def runDoctest(self, callback):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
332 files = {TESTMOD + ".py": (NOW, test_src),
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
333 "xyz.txt": (NOW, ">>> log.append(True)\n")}
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
334 self.doTest(".py", files, TESTMOD, call=callback)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
335
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
336 def doDoctestFile(self, module):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
337 log = []
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
338 old_master, doctest.master = doctest.master, None
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
339 try:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
340 doctest.testfile(
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
341 'xyz.txt', package=module, module_relative=True,
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
342 globs=locals()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
343 )
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
344 finally:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
345 doctest.master = old_master
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
346 self.assertEqual(log,[True])
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
347
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
348 def testDoctestFile(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
349 self.runDoctest(self.doDoctestFile)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
350
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
351 def doDoctestSuite(self, module):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
352 log = []
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
353 doctest.DocFileTest(
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
354 'xyz.txt', package=module, module_relative=True,
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
355 globs=locals()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
356 ).run()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
357 self.assertEqual(log,[True])
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
358
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
359 def testDoctestSuite(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
360 self.runDoctest(self.doDoctestSuite)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
361
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
362 def doTraceback(self, module):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
363 try:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
364 module.do_raise()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
365 except:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
366 tb = sys.exc_info()[2].tb_next
37581
b112d8c7988c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
367
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
368 f,lno,n,line = extract_tb(tb, 1)[0]
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
369 self.assertEqual(line, raise_src.strip())
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
370
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
371 f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
372 self.assertEqual(line, raise_src.strip())
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
373
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
374 s = StringIO.StringIO()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
375 print_tb(tb, 1, s)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51455
diff changeset
376 self.assertTrue(s.getvalue().endswith(raise_src))
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
377 else:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
378 raise AssertionError("This ought to be impossible")
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
379
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
380 def testTraceback(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
381 files = {TESTMOD + ".py": (NOW, raise_src)}
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
382 self.doTest(None, files, TESTMOD, call=self.doTraceback)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
383
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
384
55920
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
385 @unittest.skipUnless(zlib, "requires zlib")
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
386 class CompressedZipImportTestCase(UncompressedZipImportTestCase):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
387 compression = ZIP_DEFLATED
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
388
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
389
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
390 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
391 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
392 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
393 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
394
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
395 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
396 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
397
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
398 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
399 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
400
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 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
402 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
403 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
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 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
406 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
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 testEmptyFile(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 test_support.unlink(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
410 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
411 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
412
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 def testFileUnreadable(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
414 test_support.unlink(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
415 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
416 try:
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
417 os.close(fd)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
418 self.assertZipFailure(TESTMOD)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
419 finally:
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
420 # 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
421 # delete TESTMOD, and later tests suffer bogus failures.
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
422 os.chmod(TESTMOD, 0666)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
423 test_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
424
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 def testNotZipFile(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
426 test_support.unlink(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
427 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
428 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
429 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
430 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
431
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
432 # 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
433 def _testBogusZipFile(self):
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 test_support.unlink(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
435 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
436 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
437 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
438 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
439 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
440
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 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
442 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
443 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
444 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
445 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
446 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
447 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
448
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 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
450 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
451
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(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
453 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
454 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
455 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
456 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
457 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
458 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
459
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
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
461 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
462 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
463 test_support.run_unittest(
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 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
465 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
466 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
467 )
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 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
469 test_support.unlink(TESTMOD)
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
470
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
471 if __name__ == "__main__":
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
472 test_main()