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 z = zipfile.ZipFile(zip_name, 'a') 126 for mod_name, src in sample_sources.items(): 127 z.writestr(mod_name + ".py", src) 128 z.close() 129 if verbose: 130 zip_file = zipfile.ZipFile(zip_name, 'r') 131 print ('Contents of %r:' % zip_name) 132 zip_file.printdir() 133 zip_file.close() 134 os.remove(script_name) 135 sys.path.insert(0, zip_name) 136 import test_zipped_doctest 137 try: 138 # Some of the doc tests depend on the colocated text files 139 # which aren't available to the zipped version (the doctest 140 # module currently requires real filenames for non-embedded 141 # tests). So we're forced to be selective about which tests 142 # to run. 143 # doctest could really use some APIs which take a text 144 # string or a file object instead of a filename... 145 known_good_tests = [ 146 test_zipped_doctest.SampleClass, 147 test_zipped_doctest.SampleClass.NestedClass, 148 test_zipped_doctest.SampleClass.NestedClass.__init__, 149 test_zipped_doctest.SampleClass.__init__, 150 test_zipped_doctest.SampleClass.a_classmethod, 151 test_zipped_doctest.SampleClass.a_property, 152 test_zipped_doctest.SampleClass.a_staticmethod, 153 test_zipped_doctest.SampleClass.double, 154 test_zipped_doctest.SampleClass.get, 155 test_zipped_doctest.SampleNewStyleClass, 156 test_zipped_doctest.SampleNewStyleClass.__init__, 157 test_zipped_doctest.SampleNewStyleClass.double, 158 test_zipped_doctest.SampleNewStyleClass.get, 159 test_zipped_doctest.sample_func, 160 test_zipped_doctest.test_DocTest, 161 test_zipped_doctest.test_DocTestParser, 162 test_zipped_doctest.test_DocTestRunner.basics, 163 test_zipped_doctest.test_DocTestRunner.exceptions, 164 test_zipped_doctest.test_DocTestRunner.option_directives, 165 test_zipped_doctest.test_DocTestRunner.optionflags, 166 test_zipped_doctest.test_DocTestRunner.verbose_flag, 167 test_zipped_doctest.test_Example, 168 test_zipped_doctest.test_debug, 169 test_zipped_doctest.test_testsource, 170 test_zipped_doctest.test_trailing_space_in_test, 171 test_zipped_doctest.test_DocTestSuite, 172 test_zipped_doctest.test_DocTestFinder, 173 ] 174 # These tests are the ones which need access 175 # to the data files, so we don't run them 176 fail_due_to_missing_data_files = [ 177 test_zipped_doctest.test_DocFileSuite, 178 test_zipped_doctest.test_testfile, 179 test_zipped_doctest.test_unittest_reportflags, 180 ] 181 182 for obj in known_good_tests: 183 _run_object_doctest(obj, test_zipped_doctest) 184 finally: 185 del sys.modules["test_zipped_doctest"] 186 187 def test_doctest_main_issue4197(self): 188 test_src = textwrap.dedent("""\ 189 class Test: 190 ">>> 'line 2'" 191 pass 192 193 import doctest 194 doctest.testmod() 195 """) 196 pattern = 'File "%s", line 2, in %s' 197 with test.support.temp_dir() as d: 198 script_name = make_script(d, 'script', test_src) 199 rc, out, err = assert_python_ok(script_name) 200 expected = pattern % (script_name, "__main__.Test") 201 if verbose: 202 print ("Expected line", expected) 203 print ("Got stdout:") 204 print (ascii(out)) 205 self.assertIn(expected.encode('utf-8'), out) 206 zip_name, run_name = make_zip_script(d, "test_zip", 207 script_name, '__main__.py') 208 rc, out, err = assert_python_ok(zip_name) 209 expected = pattern % (run_name, "__main__.Test") 210 if verbose: 211 print ("Expected line", expected) 212 print ("Got stdout:") 213 print (ascii(out)) 214 self.assertIn(expected.encode('utf-8'), out) 215 216 def test_pdb_issue4201(self): 217 test_src = textwrap.dedent("""\ 218 def f(): 219 pass 220 221 import pdb 222 pdb.Pdb(nosigint=True).runcall(f) 223 """) 224 with test.support.temp_dir() as d: 225 script_name = make_script(d, 'script', test_src) 226 p = spawn_python(script_name) 227 p.stdin.write(b'l\n') 228 data = kill_python(p) 229 # bdb/pdb applies normcase to its filename before displaying 230 self.assertIn(os.path.normcase(script_name.encode('utf-8')), data) 231 zip_name, run_name = make_zip_script(d, "test_zip", 232 script_name, '__main__.py') 233 p = spawn_python(zip_name) 234 p.stdin.write(b'l\n') 235 data = kill_python(p) 236 # bdb/pdb applies normcase to its filename before displaying 237 self.assertIn(os.path.normcase(run_name.encode('utf-8')), data) 238 239 240def tearDownModule(): 241 test.support.reap_children() 242 243if __name__ == '__main__': 244 unittest.main() 245