Issue #5330: C functions called with keyword arguments were not reported by
[python.git] / Lib / test / test_zipimport_support.py
blobb416b818f50f3fbf5b2e624b24eaed4ca6195016
1 # This test module covers support in various parts of the standard library
2 # for working with modules located inside zipfiles
3 # The tests are centralised in this fashion to make it easy to drop them
4 # if a platform doesn't support zipimport
5 import unittest
6 import test.test_support
7 import os
8 import os.path
9 import sys
10 import textwrap
11 import zipfile
12 import zipimport
13 import doctest
14 import inspect
15 import linecache
16 import pdb
18 verbose = test.test_support.verbose
20 # Library modules covered by this test set
21 # pdb (Issue 4201)
22 # inspect (Issue 4223)
23 # doctest (Issue 4197)
25 # Other test modules with zipimport related tests
26 # test_zipimport (of course!)
27 # test_cmd_line_script (covers the zipimport support in runpy)
29 # Retrieve some helpers from other test cases
30 from test import test_doctest, sample_doctest
31 from test.test_importhooks import ImportHooksBaseTestCase
32 from test.test_cmd_line_script import temp_dir, _run_python, \
33 _spawn_python, _kill_python, \
34 _make_test_script, \
35 _compile_test_script, \
36 _make_test_zip, _make_test_pkg
39 def _run_object_doctest(obj, module):
40 # Direct doctest output (normally just errors) to real stdout; doctest
41 # output shouldn't be compared by regrtest.
42 save_stdout = sys.stdout
43 sys.stdout = test.test_support.get_original_stdout()
44 try:
45 finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
46 runner = doctest.DocTestRunner(verbose=verbose)
47 # Use the object's fully qualified name if it has one
48 # Otherwise, use the module's name
49 try:
50 name = "%s.%s" % (obj.__module__, obj.__name__)
51 except AttributeError:
52 name = module.__name__
53 for example in finder.find(obj, name, module):
54 runner.run(example)
55 f, t = runner.failures, runner.tries
56 if f:
57 raise test.test_support.TestFailed("%d of %d doctests failed" % (f, t))
58 finally:
59 sys.stdout = save_stdout
60 if verbose:
61 print 'doctest (%s) ... %d tests with zero failures' % (module.__name__, t)
62 return f, t
66 class ZipSupportTests(ImportHooksBaseTestCase):
67 # We use the ImportHooksBaseTestCase to restore
68 # the state of the import related information
69 # in the sys module after each test
70 # We also clear the linecache and zipimport cache
71 # just to avoid any bogus errors due to name reuse in the tests
72 def setUp(self):
73 linecache.clearcache()
74 zipimport._zip_directory_cache.clear()
75 ImportHooksBaseTestCase.setUp(self)
78 def test_inspect_getsource_issue4223(self):
79 test_src = "def foo(): pass\n"
80 with temp_dir() as d:
81 init_name = _make_test_script(d, '__init__', test_src)
82 name_in_zip = os.path.join('zip_pkg',
83 os.path.basename(init_name))
84 zip_name, run_name = _make_test_zip(d, 'test_zip',
85 init_name, name_in_zip)
86 os.remove(init_name)
87 sys.path.insert(0, zip_name)
88 import zip_pkg
89 self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
91 def test_doctest_issue4197(self):
92 # To avoid having to keep two copies of the doctest module's
93 # unit tests in sync, this test works by taking the source of
94 # test_doctest itself, rewriting it a bit to cope with a new
95 # location, and then throwing it in a zip file to make sure
96 # everything still works correctly
97 test_src = inspect.getsource(test_doctest)
98 test_src = test_src.replace(
99 "from test import test_doctest",
100 "import test_zipped_doctest as test_doctest")
101 test_src = test_src.replace("test.test_doctest",
102 "test_zipped_doctest")
103 test_src = test_src.replace("test.sample_doctest",
104 "sample_zipped_doctest")
105 sample_src = inspect.getsource(sample_doctest)
106 sample_src = sample_src.replace("test.test_doctest",
107 "test_zipped_doctest")
108 with temp_dir() as d:
109 script_name = _make_test_script(d, 'test_zipped_doctest',
110 test_src)
111 zip_name, run_name = _make_test_zip(d, 'test_zip',
112 script_name)
113 z = zipfile.ZipFile(zip_name, 'a')
114 z.writestr("sample_zipped_doctest.py", sample_src)
115 z.close()
116 if verbose:
117 zip_file = zipfile.ZipFile(zip_name, 'r')
118 print 'Contents of %r:' % zip_name
119 zip_file.printdir()
120 zip_file.close()
121 os.remove(script_name)
122 sys.path.insert(0, zip_name)
123 import test_zipped_doctest
124 # Some of the doc tests depend on the colocated text files
125 # which aren't available to the zipped version (the doctest
126 # module currently requires real filenames for non-embedded
127 # tests). So we're forced to be selective about which tests
128 # to run.
129 # doctest could really use some APIs which take a text
130 # string or a file object instead of a filename...
131 known_good_tests = [
132 test_zipped_doctest.SampleClass,
133 test_zipped_doctest.SampleClass.NestedClass,
134 test_zipped_doctest.SampleClass.NestedClass.__init__,
135 test_zipped_doctest.SampleClass.__init__,
136 test_zipped_doctest.SampleClass.a_classmethod,
137 test_zipped_doctest.SampleClass.a_property,
138 test_zipped_doctest.SampleClass.a_staticmethod,
139 test_zipped_doctest.SampleClass.double,
140 test_zipped_doctest.SampleClass.get,
141 test_zipped_doctest.SampleNewStyleClass,
142 test_zipped_doctest.SampleNewStyleClass.__init__,
143 test_zipped_doctest.SampleNewStyleClass.double,
144 test_zipped_doctest.SampleNewStyleClass.get,
145 test_zipped_doctest.old_test1,
146 test_zipped_doctest.old_test2,
147 test_zipped_doctest.old_test3,
148 test_zipped_doctest.old_test4,
149 test_zipped_doctest.sample_func,
150 test_zipped_doctest.test_DocTest,
151 test_zipped_doctest.test_DocTestParser,
152 test_zipped_doctest.test_DocTestRunner.basics,
153 test_zipped_doctest.test_DocTestRunner.exceptions,
154 test_zipped_doctest.test_DocTestRunner.option_directives,
155 test_zipped_doctest.test_DocTestRunner.optionflags,
156 test_zipped_doctest.test_DocTestRunner.verbose_flag,
157 test_zipped_doctest.test_Example,
158 test_zipped_doctest.test_debug,
159 test_zipped_doctest.test_pdb_set_trace,
160 test_zipped_doctest.test_pdb_set_trace_nested,
161 test_zipped_doctest.test_testsource,
162 test_zipped_doctest.test_trailing_space_in_test,
163 test_zipped_doctest.test_DocTestSuite,
164 test_zipped_doctest.test_DocTestFinder,
166 # These remaining tests are the ones which need access
167 # to the data files, so we don't run them
168 fail_due_to_missing_data_files = [
169 test_zipped_doctest.test_DocFileSuite,
170 test_zipped_doctest.test_testfile,
171 test_zipped_doctest.test_unittest_reportflags,
173 for obj in known_good_tests:
174 _run_object_doctest(obj, test_zipped_doctest)
176 def test_doctest_main_issue4197(self):
177 test_src = textwrap.dedent("""\
178 class Test:
179 ">>> 'line 2'"
180 pass
182 import doctest
183 doctest.testmod()
184 """)
185 pattern = 'File "%s", line 2, in %s'
186 with temp_dir() as d:
187 script_name = _make_test_script(d, 'script', test_src)
188 exit_code, data = _run_python(script_name)
189 expected = pattern % (script_name, "__main__.Test")
190 if verbose:
191 print "Expected line", expected
192 print "Got stdout:"
193 print data
194 self.assert_(expected in data)
195 zip_name, run_name = _make_test_zip(d, "test_zip",
196 script_name, '__main__.py')
197 exit_code, data = _run_python(zip_name)
198 expected = pattern % (run_name, "__main__.Test")
199 if verbose:
200 print "Expected line", expected
201 print "Got stdout:"
202 print data
203 self.assert_(expected in data)
205 def test_pdb_issue4201(self):
206 test_src = textwrap.dedent("""\
207 def f():
208 pass
210 import pdb
211 pdb.runcall(f)
212 """)
213 with temp_dir() as d:
214 script_name = _make_test_script(d, 'script', test_src)
215 p = _spawn_python(script_name)
216 p.stdin.write('l\n')
217 data = _kill_python(p)
218 self.assert_(script_name in data)
219 zip_name, run_name = _make_test_zip(d, "test_zip",
220 script_name, '__main__.py')
221 p = _spawn_python(zip_name)
222 p.stdin.write('l\n')
223 data = _kill_python(p)
224 self.assert_(run_name in data)
227 def test_main():
228 test.test_support.run_unittest(ZipSupportTests)
229 test.test_support.reap_children()
231 if __name__ == '__main__':
232 test_main()