• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import test.support
6import os
7import os.path
8import sys
9import textwrap
10import zipfile
11import zipimport
12import doctest
13import inspect
14import linecache
15import unittest
16from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
17                                        make_script, make_zip_script)
18
19verbose = test.support.verbose
20
21# Library modules covered by this test set
22#  pdb (Issue 4201)
23#  inspect (Issue 4223)
24#  doctest (Issue 4197)
25
26# Other test modules with zipimport related tests
27#  test_zipimport (of course!)
28#  test_cmd_line_script (covers the zipimport support in runpy)
29
30# Retrieve some helpers from other test cases
31from test import (test_doctest, sample_doctest, sample_doctest_no_doctests,
32                  sample_doctest_no_docstrings)
33
34
35def _run_object_doctest(obj, module):
36    finder = doctest.DocTestFinder(verbose=verbose, recurse=False)
37    runner = doctest.DocTestRunner(verbose=verbose)
38    # Use the object's fully qualified name if it has one
39    # Otherwise, use the module's name
40    try:
41        name = "%s.%s" % (obj.__module__, obj.__qualname__)
42    except AttributeError:
43        name = module.__name__
44    for example in finder.find(obj, name, module):
45        runner.run(example)
46    f, t = runner.failures, runner.tries
47    if f:
48        raise test.support.TestFailed("%d of %d doctests failed" % (f, t))
49    if verbose:
50        print ('doctest (%s) ... %d tests with zero failures' % (module.__name__, t))
51    return f, t
52
53
54
55class ZipSupportTests(unittest.TestCase):
56    # This used to use the ImportHooksBaseTestCase to restore
57    # the state of the import related information
58    # in the sys module after each test. However, that restores
59    # *too much* information and breaks for the invocation
60    # of test_doctest. So we do our own thing and leave
61    # sys.modules alone.
62    # We also clear the linecache and zipimport cache
63    # just to avoid any bogus errors due to name reuse in the tests
64    def setUp(self):
65        linecache.clearcache()
66        zipimport._zip_directory_cache.clear()
67        self.path = sys.path[:]
68        self.meta_path = sys.meta_path[:]
69        self.path_hooks = sys.path_hooks[:]
70        sys.path_importer_cache.clear()
71
72    def tearDown(self):
73        sys.path[:] = self.path
74        sys.meta_path[:] = self.meta_path
75        sys.path_hooks[:] = self.path_hooks
76        sys.path_importer_cache.clear()
77
78    def test_inspect_getsource_issue4223(self):
79        test_src = "def foo(): pass\n"
80        with test.support.temp_dir() as d:
81            init_name = make_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_zip_script(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            try:
90                self.assertEqual(inspect.getsource(zip_pkg.foo), test_src)
91            finally:
92                del sys.modules["zip_pkg"]
93
94    def test_doctest_issue4197(self):
95        # To avoid having to keep two copies of the doctest module's
96        # unit tests in sync, this test works by taking the source of
97        # test_doctest itself, rewriting it a bit to cope with a new
98        # location, and then throwing it in a zip file to make sure
99        # everything still works correctly
100        test_src = inspect.getsource(test_doctest)
101        test_src = test_src.replace(
102                         "from test import test_doctest",
103                         "import test_zipped_doctest as test_doctest")
104        test_src = test_src.replace("test.test_doctest",
105                                    "test_zipped_doctest")
106        test_src = test_src.replace("test.sample_doctest",
107                                    "sample_zipped_doctest")
108        # The sample doctest files rewritten to include in the zipped version.
109        sample_sources = {}
110        for mod in [sample_doctest, sample_doctest_no_doctests,
111                    sample_doctest_no_docstrings]:
112            src = inspect.getsource(mod)
113            src = src.replace("test.test_doctest", "test_zipped_doctest")
114            # Rewrite the module name so that, for example,
115            # "test.sample_doctest" becomes "sample_zipped_doctest".
116            mod_name = mod.__name__.split(".")[-1]
117            mod_name = mod_name.replace("sample_", "sample_zipped_")
118            sample_sources[mod_name] = src
119
120        with test.support.temp_dir() as d:
121            script_name = make_script(d, 'test_zipped_doctest',
122                                            test_src)
123            zip_name, run_name = make_zip_script(d, 'test_zip',
124                                                script_name)
125            with zipfile.ZipFile(zip_name, 'a') as z:
126                for mod_name, src in sample_sources.items():
127                    z.writestr(mod_name + ".py", src)
128            if verbose:
129                with zipfile.ZipFile(zip_name, 'r') as zip_file:
130                    print ('Contents of %r:' % zip_name)
131                    zip_file.printdir()
132            os.remove(script_name)
133            sys.path.insert(0, zip_name)
134            import test_zipped_doctest
135            try:
136                # Some of the doc tests depend on the colocated text files
137                # which aren't available to the zipped version (the doctest
138                # module currently requires real filenames for non-embedded
139                # tests). So we're forced to be selective about which tests
140                # to run.
141                # doctest could really use some APIs which take a text
142                # string or a file object instead of a filename...
143                known_good_tests = [
144                    test_zipped_doctest.SampleClass,
145                    test_zipped_doctest.SampleClass.NestedClass,
146                    test_zipped_doctest.SampleClass.NestedClass.__init__,
147                    test_zipped_doctest.SampleClass.__init__,
148                    test_zipped_doctest.SampleClass.a_classmethod,
149                    test_zipped_doctest.SampleClass.a_property,
150                    test_zipped_doctest.SampleClass.a_staticmethod,
151                    test_zipped_doctest.SampleClass.double,
152                    test_zipped_doctest.SampleClass.get,
153                    test_zipped_doctest.SampleNewStyleClass,
154                    test_zipped_doctest.SampleNewStyleClass.__init__,
155                    test_zipped_doctest.SampleNewStyleClass.double,
156                    test_zipped_doctest.SampleNewStyleClass.get,
157                    test_zipped_doctest.sample_func,
158                    test_zipped_doctest.test_DocTest,
159                    test_zipped_doctest.test_DocTestParser,
160                    test_zipped_doctest.test_DocTestRunner.basics,
161                    test_zipped_doctest.test_DocTestRunner.exceptions,
162                    test_zipped_doctest.test_DocTestRunner.option_directives,
163                    test_zipped_doctest.test_DocTestRunner.optionflags,
164                    test_zipped_doctest.test_DocTestRunner.verbose_flag,
165                    test_zipped_doctest.test_Example,
166                    test_zipped_doctest.test_debug,
167                    test_zipped_doctest.test_testsource,
168                    test_zipped_doctest.test_trailing_space_in_test,
169                    test_zipped_doctest.test_DocTestSuite,
170                    test_zipped_doctest.test_DocTestFinder,
171                ]
172                # These tests are the ones which need access
173                # to the data files, so we don't run them
174                fail_due_to_missing_data_files = [
175                    test_zipped_doctest.test_DocFileSuite,
176                    test_zipped_doctest.test_testfile,
177                    test_zipped_doctest.test_unittest_reportflags,
178                ]
179
180                for obj in known_good_tests:
181                    _run_object_doctest(obj, test_zipped_doctest)
182            finally:
183                del sys.modules["test_zipped_doctest"]
184
185    def test_doctest_main_issue4197(self):
186        test_src = textwrap.dedent("""\
187                    class Test:
188                        ">>> 'line 2'"
189                        pass
190
191                    import doctest
192                    doctest.testmod()
193                    """)
194        pattern = 'File "%s", line 2, in %s'
195        with test.support.temp_dir() as d:
196            script_name = make_script(d, 'script', test_src)
197            rc, out, err = assert_python_ok(script_name)
198            expected = pattern % (script_name, "__main__.Test")
199            if verbose:
200                print ("Expected line", expected)
201                print ("Got stdout:")
202                print (ascii(out))
203            self.assertIn(expected.encode('utf-8'), out)
204            zip_name, run_name = make_zip_script(d, "test_zip",
205                                                script_name, '__main__.py')
206            rc, out, err = assert_python_ok(zip_name)
207            expected = pattern % (run_name, "__main__.Test")
208            if verbose:
209                print ("Expected line", expected)
210                print ("Got stdout:")
211                print (ascii(out))
212            self.assertIn(expected.encode('utf-8'), out)
213
214    def test_pdb_issue4201(self):
215        test_src = textwrap.dedent("""\
216                    def f():
217                        pass
218
219                    import pdb
220                    pdb.Pdb(nosigint=True).runcall(f)
221                    """)
222        with test.support.temp_dir() as d:
223            script_name = make_script(d, 'script', test_src)
224            p = spawn_python(script_name)
225            p.stdin.write(b'l\n')
226            data = kill_python(p)
227            # bdb/pdb applies normcase to its filename before displaying
228            self.assertIn(os.path.normcase(script_name.encode('utf-8')), data)
229            zip_name, run_name = make_zip_script(d, "test_zip",
230                                                script_name, '__main__.py')
231            p = spawn_python(zip_name)
232            p.stdin.write(b'l\n')
233            data = kill_python(p)
234            # bdb/pdb applies normcase to its filename before displaying
235            self.assertIn(os.path.normcase(run_name.encode('utf-8')), data)
236
237
238def tearDownModule():
239    test.support.reap_children()
240
241if __name__ == '__main__':
242    unittest.main()
243