annotate Lib/test/test_zipimport.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 117e152e513a
children 6768b2350c57
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 import zlib # implied prerequisite
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
10 from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
11 from test import test_support
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
12 from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
13
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
14 import zipimport
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
15 import linecache
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
16 import doctest
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
17 import inspect
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
18 import StringIO
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
19 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
20 raise_src = 'def do_raise(): raise TypeError\n'
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
21
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
22 # so we only run testAFakeZlib once if this test is run repeatedly
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
23 # which happens when we look for ref leaks
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
24 test_imported = False
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
25
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
26
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
27 def make_pyc(co, mtime):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
28 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
29 if type(mtime) is type(0.0):
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
30 # Mac mtimes need a bit of special casing
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
31 if mtime < 0x7fffffff:
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
32 mtime = int(mtime)
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
33 else:
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27915
diff changeset
34 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
35 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
36 return pyc
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
37
36393
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
38 def module_path_to_dotted_name(path):
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
39 return path.replace(os.sep, '.')
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
40
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
41 NOW = time.time()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
42 test_pyc = make_pyc(test_co, NOW)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
43
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 if __debug__:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
46 pyc_ext = ".pyc"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
47 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
48 pyc_ext = ".pyo"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
49
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 TESTMOD = "ziptestmodule"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
52 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
53 TESTPACK2 = "ziptestpackage2"
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28917
diff changeset
54 TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip")
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
55
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
56 class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
57
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
58 compression = ZIP_STORED
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 def setUp(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
61 # 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
62 # 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
63 linecache.clearcache()
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
64 zipimport._zip_directory_cache.clear()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
65 ImportHooksBaseTestCase.setUp(self)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
66
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
67 def doTest(self, expected_ext, files, *modules, **kw):
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
68 z = ZipFile(TEMP_ZIP, "w")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
69 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
70 for name, (mtime, data) in files.items():
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
71 zinfo = ZipInfo(name, time.localtime(mtime))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
72 zinfo.compress_type = self.compression
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
73 z.writestr(zinfo, data)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
74 z.close()
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
75
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
76 stuff = kw.get("stuff", None)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
77 if stuff is not None:
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
78 # 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
79 f = open(TEMP_ZIP, "rb")
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
80 data = f.read()
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
81 f.close()
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
82
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
83 f = open(TEMP_ZIP, "wb")
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
84 f.write(stuff)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
85 f.write(data)
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
86 f.close()
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
87
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
88 sys.path.insert(0, TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
89
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
90 mod = __import__(".".join(modules), globals(), locals(),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
91 ["__dummy__"])
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
92
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
93 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
94 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
95 call(mod)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
96
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
97 if expected_ext:
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
98 file = mod.get_file()
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
99 self.assertEquals(file, os.path.join(TEMP_ZIP,
27188
bcae2a70e1ec cleaned up Jack's Mac OS9 changes
Just van Rossum <just@letterror.com>
parents: 27168
diff changeset
100 *modules) + expected_ext)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
101 finally:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
102 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
103 os.remove(TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
104
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
105 def testAFakeZlib(self):
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 could cause a stack overflow before: importing zlib.py
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
108 # from a compressed archive would cause zlib to be imported
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
109 # which would find zlib.py in the archive, which would... etc.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
110 #
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
111 # 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
112 # to trigger zipimport to import zlib (zipimport caches the
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
113 # zlib.decompress function object, after which the problem being
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
114 # tested here wouldn't be a problem anymore...
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
115 # (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
116 # item in a list sorted by name, like unittest.makeSuite() does.)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
117 #
30946
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
118 # 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
119 # 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
120 # 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
121 # 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
122 #
f729c4d53048 Backport of fix for [ 765456 ]: testAFakeZlib failed on platforms that use
Just van Rossum <just@letterror.com>
parents: 29973
diff changeset
123 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
124 return
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
125 if "zlib" in sys.modules:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
126 del sys.modules["zlib"]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
127 files = {"zlib.py": (NOW, test_src)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
128 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
129 self.doTest(".py", files, "zlib")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
130 except ImportError:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
131 if self.compression != ZIP_DEFLATED:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
132 self.fail("expected test to not raise ImportError")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
133 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
134 if self.compression != ZIP_STORED:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
135 self.fail("expected test to raise ImportError")
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 testPy(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
138 files = {TESTMOD + ".py": (NOW, test_src)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
139 self.doTest(".py", 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 testPyc(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
142 files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
143 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
144
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
145 def testBoth(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
146 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
147 TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
148 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
149
27047
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
150 def testEmptyPy(self):
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
151 files = {TESTMOD + ".py": (NOW, "")}
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
152 self.doTest(None, files, TESTMOD)
737a5476396b Fix for bug #661136
Just van Rossum <just@letterror.com>
parents: 27021
diff changeset
153
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
154 def testBadMagic(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
155 # make pyc magic word invalid, forcing loading from .py
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
156 m0 = ord(test_pyc[0])
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
157 m0 ^= 0x04 # flip an arbitrary bit
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
158 badmagic_pyc = chr(m0) + test_pyc[1:]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
159 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
160 TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
161 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
162
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
163 def testBadMagic2(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
164 # make pyc magic word invalid, causing an ImportError
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
165 m0 = ord(test_pyc[0])
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
166 m0 ^= 0x04 # flip an arbitrary bit
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
167 badmagic_pyc = chr(m0) + test_pyc[1:]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
168 files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
169 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
170 self.doTest(".py", files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
171 except ImportError:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
172 pass
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
173 else:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
174 self.fail("expected ImportError; import from bad pyc")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
175
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
176 def testBadMTime(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
177 t3 = ord(test_pyc[7])
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
178 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
179 # 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
180 badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
181 files = {TESTMOD + ".py": (NOW, test_src),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
182 TESTMOD + pyc_ext: (NOW, badtime_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
183 self.doTest(".py", files, 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 testPackage(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
186 packdir = TESTPACK + os.sep
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
187 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
188 packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
189 self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
190
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
191 def testDeepPackage(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
192 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
193 packdir2 = packdir + TESTPACK2 + os.sep
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
194 files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
195 packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
196 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
197 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
198
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
199 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
200 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
201 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
202 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
203 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
204 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
205
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 = 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
207 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
208 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
209 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
210 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
211 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
212 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
213
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 zi = zipimport.zipimporter(TEMP_ZIP)
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
215 self.assertEquals(zi.archive, TEMP_ZIP)
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
216 self.assertEquals(zi.is_package(TESTPACK), True)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
217 mod = zi.load_module(TESTPACK)
51455
117e152e513a Issue #4512 closeout: Make ZipImport.get_filename() a public method
Nick Coghlan <ncoghlan@gmail.com>
parents: 50162
diff changeset
218 self.assertEquals(zi.get_filename(TESTPACK), mod.__file__)
36386
3b4b4454fe86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36384
diff changeset
219
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
220 self.assertEquals(zi.is_package(packdir + '__init__'), False)
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
221 self.assertEquals(zi.is_package(packdir + TESTPACK2), True)
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
222 self.assertEquals(zi.is_package(packdir2 + TESTMOD), False)
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
223
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
224 mod_path = packdir2 + TESTMOD
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
225 mod_name = module_path_to_dotted_name(mod_path)
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
226 pkg = __import__(mod_name)
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
227 mod = sys.modules[mod_name]
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
228 self.assertEquals(zi.get_source(TESTPACK), None)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
229 self.assertEquals(zi.get_source(mod_path), None)
51455
117e152e513a Issue #4512 closeout: Make ZipImport.get_filename() a public method
Nick Coghlan <ncoghlan@gmail.com>
parents: 50162
diff changeset
230 self.assertEquals(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
231 # 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
232 loader = mod.__loader__
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
233 self.assertEquals(loader.get_source(mod_name), None)
51455
117e152e513a Issue #4512 closeout: Make ZipImport.get_filename() a public method
Nick Coghlan <ncoghlan@gmail.com>
parents: 50162
diff changeset
234 self.assertEquals(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
235
2acc652e58a1 #1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 37581
diff changeset
236 # 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
237 zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
2acc652e58a1 #1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 37581
diff changeset
238 self.assertEquals(zi2.archive, TEMP_ZIP)
2acc652e58a1 #1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 37581
diff changeset
239 self.assertEquals(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
240 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
241 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
242 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
243
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
244 def testZipImporterMethodsInSubDirectory(self):
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
245 packdir = TESTPACK + os.sep
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
246 packdir2 = packdir + TESTPACK2 + os.sep
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
247 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
248 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
249
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
250 z = ZipFile(TEMP_ZIP, "w")
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
251 try:
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
252 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
253 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
254 zinfo.compress_type = self.compression
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
255 z.writestr(zinfo, data)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
256 z.close()
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
257
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
258 zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
259 self.assertEquals(zi.archive, TEMP_ZIP)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
260 self.assertEquals(zi.prefix, packdir)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
261 self.assertEquals(zi.is_package(TESTPACK2), True)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
262 mod = zi.load_module(TESTPACK2)
51455
117e152e513a Issue #4512 closeout: Make ZipImport.get_filename() a public method
Nick Coghlan <ncoghlan@gmail.com>
parents: 50162
diff changeset
263 self.assertEquals(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
264
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
265 self.assertEquals(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
266 self.assertEquals(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
267
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
268 mod_path = TESTPACK2 + os.sep + TESTMOD
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
269 mod_name = module_path_to_dotted_name(mod_path)
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
270 pkg = __import__(mod_name)
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
271 mod = sys.modules[mod_name]
46888
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
272 self.assertEquals(zi.get_source(TESTPACK2), None)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
273 self.assertEquals(zi.get_source(mod_path), None)
51455
117e152e513a Issue #4512 closeout: Make ZipImport.get_filename() a public method
Nick Coghlan <ncoghlan@gmail.com>
parents: 50162
diff changeset
274 self.assertEquals(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
275 # 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
276 loader = mod.__loader__
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46888
diff changeset
277 self.assertEquals(loader.get_source(mod_name), None)
51455
117e152e513a Issue #4512 closeout: Make ZipImport.get_filename() a public method
Nick Coghlan <ncoghlan@gmail.com>
parents: 50162
diff changeset
278 self.assertEquals(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
279 finally:
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
280 z.close()
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
281 os.remove(TEMP_ZIP)
dedce7cb7a5d #1326: document and test zipimporter.archive and zipimporter.prefix.
Georg Brandl <georg@python.org>
parents: 44534
diff changeset
282
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
283 def testGetData(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
284 z = ZipFile(TEMP_ZIP, "w")
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
285 z.compression = self.compression
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
286 try:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
287 name = "testdata.dat"
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
288 data = "".join([chr(x) for x in range(256)]) * 500
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
289 z.writestr(name, data)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
290 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
291 zi = zipimport.zipimporter(TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
292 self.assertEquals(data, zi.get_data(name))
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
293 self.assert_('zipimporter object' in repr(zi))
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
294 finally:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
295 z.close()
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
296 os.remove(TEMP_ZIP)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
297
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
298 def testImporterAttr(self):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
299 src = """if 1: # indent hack
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
300 def get_file():
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
301 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
302 if __loader__.get_data("some.data") != "some data":
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
303 raise AssertionError, "bad data"\n"""
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
304 pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
305 files = {TESTMOD + pyc_ext: (NOW, pyc),
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
306 "some.data": (NOW, "some data")}
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
307 self.doTest(pyc_ext, files, TESTMOD)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
308
29973
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
309 def testImport_WithStuff(self):
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
310 # 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
311 # 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
312 files = {TESTMOD + ".py": (NOW, test_src)}
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
313 self.doTest(".py", files, TESTMOD,
b9d0a0ee2206 Change the zipimport implementation to accept files containing
Thomas Heller <theller@ctypes.org>
parents: 29066
diff changeset
314 stuff="Some Stuff"*31)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
315
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
316 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
317 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
318
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
319 def testGetSource(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
320 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
321 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
322
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
323 def testGetCompiledSource(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
324 pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)
37581
b112d8c7988c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
325 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
326 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
327 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
328
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
329 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
330 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
331 "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
332 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
333
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
334 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
335 log = []
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
336 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
337 try:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
338 doctest.testfile(
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
339 '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
340 globs=locals()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
341 )
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
342 finally:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
343 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
344 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
345
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
346 def testDoctestFile(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
347 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
348
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
349 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
350 log = []
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
351 doctest.DocFileTest(
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
352 '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
353 globs=locals()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
354 ).run()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
355 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
356
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
357 def testDoctestSuite(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
358 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
359
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
360
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
361 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
362 try:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
363 module.do_raise()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
364 except:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
365 tb = sys.exc_info()[2].tb_next
37581
b112d8c7988c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37576
diff changeset
366
37576
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
367 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
368 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
369
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
370 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
371 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
372
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
373 s = StringIO.StringIO()
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
374 print_tb(tb, 1, s)
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
375 self.failUnless(s.getvalue().endswith(raise_src))
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
376 else:
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
377 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
378
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
379 def testTraceback(self):
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
380 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
381 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
382
d11366f7e1e7 Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents: 36393
diff changeset
383
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
384 class CompressedZipImportTestCase(UncompressedZipImportTestCase):
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
385 compression = ZIP_DEFLATED
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
386
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents:
diff changeset
387
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
388 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
389 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
390 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
391 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
392
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 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
394 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
395
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 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
397 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
398
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 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
400 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
401 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
402
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 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
404 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
405
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 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
407 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
408 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
409 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
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 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
412 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
413 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
414 try:
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
415 os.close(fd)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
416 self.assertZipFailure(TESTMOD)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
417 finally:
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
418 # 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
419 # 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
420 os.chmod(TESTMOD, 0666)
3114eb060d0a Repaired new test failures on Windows:
Tim Peters <tim.peters@gmail.com>
parents: 36386
diff changeset
421 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
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 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
424 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
425 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
426 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
427 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
428 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
429
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
430 # 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
431 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
432 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
433 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
434 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
435 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
436 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
437 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
438
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 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
440 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
441 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
442 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
443 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
444 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
445 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
446
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 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
448 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
449
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.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
451 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
452 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
453 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
454 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
455 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
456 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
457
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 def cleanup():
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 # this is necessary if test is run repeated (like when finding leaks)
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 global test_imported
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 if test_imported:
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 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
464 if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'):
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 delattr(UncompressedZipImportTestCase, 'testAFakeZlib')
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 if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'):
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 delattr(CompressedZipImportTestCase, 'testAFakeZlib')
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 test_imported = True
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
27915
a6dfabf3b92b Use correct function name to PyArg_ParseTuple("is_package").
Neal Norwitz <nnorwitz@gmail.com>
parents: 27188
diff changeset
470 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
471 cleanup()
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:
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
473 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
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:
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
479 test_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()