Brett Cannon | f254a75 | 2009-01-30 00:22:35 +0000 | [diff] [blame] | 1 | from .. import abc |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 2 | from . import util as source_util |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 3 | |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 4 | from importlib import machinery |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 5 | import errno |
Brett Cannon | 938d44d | 2012-04-22 19:58:33 -0400 | [diff] [blame] | 6 | import imp |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 7 | import os |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 8 | import py_compile |
Brett Cannon | a9976b3 | 2013-01-11 15:40:12 -0500 | [diff] [blame] | 9 | import stat |
| 10 | import sys |
| 11 | import tempfile |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 12 | from test.support import make_legacy_pyc |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 13 | import unittest |
| 14 | import warnings |
| 15 | |
| 16 | |
Brett Cannon | f254a75 | 2009-01-30 00:22:35 +0000 | [diff] [blame] | 17 | class FinderTests(abc.FinderTests): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 18 | |
| 19 | """For a top-level module, it should just be found directly in the |
| 20 | directory being searched. This is true for a directory with source |
| 21 | [top-level source], bytecode [top-level bc], or both [top-level both]. |
| 22 | There is also the possibility that it is a package [top-level package], in |
| 23 | which case there will be a directory with the module name and an |
| 24 | __init__.py file. If there is a directory without an __init__.py an |
| 25 | ImportWarning is returned [empty dir]. |
| 26 | |
| 27 | For sub-modules and sub-packages, the same happens as above but only use |
| 28 | the tail end of the name [sub module] [sub package] [sub empty]. |
| 29 | |
| 30 | When there is a conflict between a package and module having the same name |
| 31 | in the same directory, the package wins out [package over module]. This is |
| 32 | so that imports of modules within the package can occur rather than trigger |
| 33 | an import error. |
| 34 | |
| 35 | When there is a package and module with the same name, always pick the |
| 36 | package over the module [package over module]. This is so that imports from |
| 37 | the package have the possibility of succeeding. |
| 38 | |
| 39 | """ |
| 40 | |
Nick Coghlan | 48fec05 | 2012-08-20 13:18:15 +1000 | [diff] [blame] | 41 | def get_finder(self, root): |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 42 | loader_details = [(machinery.SourceFileLoader, |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 43 | machinery.SOURCE_SUFFIXES), |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 44 | (machinery.SourcelessFileLoader, |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 45 | machinery.BYTECODE_SUFFIXES)] |
Nick Coghlan | 48fec05 | 2012-08-20 13:18:15 +1000 | [diff] [blame] | 46 | return machinery.FileFinder(root, *loader_details) |
| 47 | |
| 48 | def import_(self, root, module): |
| 49 | return self.get_finder(root).find_module(module) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 50 | |
| 51 | def run_test(self, test, create=None, *, compile_=None, unlink=None): |
| 52 | """Test the finding of 'test' with the creation of modules listed in |
| 53 | 'create'. |
| 54 | |
| 55 | Any names listed in 'compile_' are byte-compiled. Modules |
| 56 | listed in 'unlink' have their source files deleted. |
| 57 | |
| 58 | """ |
| 59 | if create is None: |
| 60 | create = {test} |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 61 | with source_util.create_modules(*create) as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 62 | if compile_: |
| 63 | for name in compile_: |
| 64 | py_compile.compile(mapping[name]) |
| 65 | if unlink: |
| 66 | for name in unlink: |
| 67 | os.unlink(mapping[name]) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 68 | try: |
| 69 | make_legacy_pyc(mapping[name]) |
| 70 | except OSError as error: |
| 71 | # Some tests do not set compile_=True so the source |
| 72 | # module will not get compiled and there will be no |
| 73 | # PEP 3147 pyc file to rename. |
| 74 | if error.errno != errno.ENOENT: |
| 75 | raise |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 76 | loader = self.import_(mapping['.root'], test) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 77 | self.assertTrue(hasattr(loader, 'load_module')) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 78 | return loader |
| 79 | |
| 80 | def test_module(self): |
| 81 | # [top-level source] |
| 82 | self.run_test('top_level') |
| 83 | # [top-level bc] |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 84 | self.run_test('top_level', compile_={'top_level'}, |
| 85 | unlink={'top_level'}) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 86 | # [top-level both] |
| 87 | self.run_test('top_level', compile_={'top_level'}) |
| 88 | |
| 89 | # [top-level package] |
| 90 | def test_package(self): |
| 91 | # Source. |
| 92 | self.run_test('pkg', {'pkg.__init__'}) |
| 93 | # Bytecode. |
| 94 | self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'}, |
| 95 | unlink={'pkg.__init__'}) |
| 96 | # Both. |
| 97 | self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'}) |
| 98 | |
| 99 | # [sub module] |
| 100 | def test_module_in_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 101 | with source_util.create_modules('pkg.__init__', 'pkg.sub') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 102 | pkg_dir = os.path.dirname(mapping['pkg.__init__']) |
| 103 | loader = self.import_(pkg_dir, 'pkg.sub') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 104 | self.assertTrue(hasattr(loader, 'load_module')) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 105 | |
| 106 | # [sub package] |
| 107 | def test_package_in_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 108 | context = source_util.create_modules('pkg.__init__', 'pkg.sub.__init__') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 109 | with context as mapping: |
| 110 | pkg_dir = os.path.dirname(mapping['pkg.__init__']) |
| 111 | loader = self.import_(pkg_dir, 'pkg.sub') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 112 | self.assertTrue(hasattr(loader, 'load_module')) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 113 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 114 | # [package over modules] |
| 115 | def test_package_over_module(self): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 116 | name = '_temp' |
| 117 | loader = self.run_test(name, {'{0}.__init__'.format(name), name}) |
Eric V. Smith | e51a369 | 2012-06-24 19:13:55 -0400 | [diff] [blame] | 118 | self.assertIn('__init__', loader.get_filename(name)) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 119 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 120 | def test_failure(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 121 | with source_util.create_modules('blah') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 122 | nothing = self.import_(mapping['.root'], 'sdfsadsadf') |
Eric V. Smith | b109515 | 2012-06-28 06:15:01 -0400 | [diff] [blame] | 123 | self.assertIsNone(nothing) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 124 | |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 125 | def test_empty_string_for_dir(self): |
| 126 | # The empty string from sys.path means to search in the cwd. |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 127 | finder = machinery.FileFinder('', (machinery.SourceFileLoader, |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 128 | machinery.SOURCE_SUFFIXES)) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 129 | with open('mod.py', 'w') as file: |
| 130 | file.write("# test file for importlib") |
| 131 | try: |
| 132 | loader = finder.find_module('mod') |
| 133 | self.assertTrue(hasattr(loader, 'load_module')) |
| 134 | finally: |
| 135 | os.unlink('mod.py') |
| 136 | |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 137 | def test_invalidate_caches(self): |
| 138 | # invalidate_caches() should reset the mtime. |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 139 | finder = machinery.FileFinder('', (machinery.SourceFileLoader, |
Brett Cannon | ac9f2f3 | 2012-08-10 13:47:54 -0400 | [diff] [blame] | 140 | machinery.SOURCE_SUFFIXES)) |
Brett Cannon | b46a179 | 2012-02-27 18:15:42 -0500 | [diff] [blame] | 141 | finder._path_mtime = 42 |
| 142 | finder.invalidate_caches() |
| 143 | self.assertEqual(finder._path_mtime, -1) |
| 144 | |
Nick Coghlan | 48fec05 | 2012-08-20 13:18:15 +1000 | [diff] [blame] | 145 | # Regression test for http://bugs.python.org/issue14846 |
| 146 | def test_dir_removal_handling(self): |
| 147 | mod = 'mod' |
| 148 | with source_util.create_modules(mod) as mapping: |
| 149 | finder = self.get_finder(mapping['.root']) |
| 150 | self.assertIsNotNone(finder.find_module(mod)) |
| 151 | self.assertIsNone(finder.find_module(mod)) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 152 | |
Brett Cannon | a9976b3 | 2013-01-11 15:40:12 -0500 | [diff] [blame] | 153 | @unittest.skipUnless(sys.platform != 'win32', |
| 154 | 'os.chmod() does not support the needed arguments under Windows') |
| 155 | def test_no_read_directory(self): |
| 156 | # Issue #16730 |
| 157 | tempdir = tempfile.TemporaryDirectory() |
| 158 | original_mode = os.stat(tempdir.name).st_mode |
| 159 | def cleanup(tempdir): |
| 160 | """Cleanup function for the temporary directory. |
| 161 | |
| 162 | Since we muck with the permissions, we want to set them back to |
| 163 | their original values to make sure the directory can be properly |
| 164 | cleaned up. |
| 165 | |
| 166 | """ |
| 167 | os.chmod(tempdir.name, original_mode) |
| 168 | # If this is not explicitly called then the __del__ method is used, |
| 169 | # but since already mucking around might as well explicitly clean |
| 170 | # up. |
| 171 | tempdir.__exit__(None, None, None) |
| 172 | self.addCleanup(cleanup, tempdir) |
| 173 | os.chmod(tempdir.name, stat.S_IWUSR | stat.S_IXUSR) |
| 174 | finder = self.get_finder(tempdir.name) |
| 175 | self.assertEqual((None, []), finder.find_loader('doesnotexist')) |
| 176 | |
| 177 | def test_ignore_file(self): |
| 178 | # If a directory got changed to a file from underneath us, then don't |
| 179 | # worry about looking for submodules. |
| 180 | with tempfile.NamedTemporaryFile() as file_obj: |
| 181 | finder = self.get_finder(file_obj.name) |
| 182 | self.assertEqual((None, []), finder.find_loader('doesnotexist')) |
| 183 | |
| 184 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 185 | def test_main(): |
| 186 | from test.support import run_unittest |
| 187 | run_unittest(FinderTests) |
| 188 | |
| 189 | |
| 190 | if __name__ == '__main__': |
| 191 | test_main() |