diff options
author | Eric V. Smith <eric@trueblade.com> | 2012-06-24 23:13:55 (GMT) |
---|---|---|
committer | Eric V. Smith <eric@trueblade.com> | 2012-06-24 23:13:55 (GMT) |
commit | e51a36922ffcce8c5e45cc88dc95a9d33ead0f11 (patch) | |
tree | 77a4d11d13469e4aed6530c841252d98819ef198 /Lib | |
parent | e6bdc8f2dd0d8cb495e61d08f1db9e0e19c03b1d (diff) | |
download | cpython-e51a36922ffcce8c5e45cc88dc95a9d33ead0f11.zip cpython-e51a36922ffcce8c5e45cc88dc95a9d33ead0f11.tar.gz cpython-e51a36922ffcce8c5e45cc88dc95a9d33ead0f11.tar.bz2 |
Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 8 | ||||
-rw-r--r-- | Lib/importlib/test/source/test_finder.py | 2 | ||||
-rw-r--r-- | Lib/test/namespace_pkgs/module_and_file/a_test.py | 1 | ||||
-rw-r--r-- | Lib/test/namespace_pkgs/module_and_file/a_test/empty | 0 | ||||
-rw-r--r-- | Lib/test/test_namespace_pkgs.py | 8 |
5 files changed, 16 insertions, 3 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 40d500a..36c0e88 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -1090,6 +1090,7 @@ class FileFinder: def find_loader(self, fullname): """Try to find a loader for the specified module, or the namespace package portions. Returns (loader, list-of-portions).""" + is_namespace = False tail_module = fullname.rpartition('.')[2] try: mtime = _os.stat(self.path).st_mtime @@ -1115,14 +1116,17 @@ class FileFinder: if _path_isfile(full_path): return (loader(fullname, full_path), [base_path]) else: - # A namespace package, return the path - return (None, [base_path]) + # A namespace package, return the path if we don't also + # find a module in the next section. + is_namespace = True # Check for a file w/ a proper suffix exists. for suffix, loader in self.modules: if cache_module + suffix in cache: full_path = _path_join(self.path, tail_module + suffix) if _path_isfile(full_path): return (loader(fullname, full_path), []) + if is_namespace: + return (None, [base_path]) return (None, []) def _fill_cache(self): diff --git a/Lib/importlib/test/source/test_finder.py b/Lib/importlib/test/source/test_finder.py index a3fa21d..b22c103 100644 --- a/Lib/importlib/test/source/test_finder.py +++ b/Lib/importlib/test/source/test_finder.py @@ -110,7 +110,7 @@ class FinderTests(abc.FinderTests): def test_package_over_module(self): name = '_temp' loader = self.run_test(name, {'{0}.__init__'.format(name), name}) - self.assertTrue('__init__' in loader.get_filename(name)) + self.assertIn('__init__', loader.get_filename(name)) def test_failure(self): with source_util.create_modules('blah') as mapping: diff --git a/Lib/test/namespace_pkgs/module_and_file/a_test.py b/Lib/test/namespace_pkgs/module_and_file/a_test.py new file mode 100644 index 0000000..43cbedb --- /dev/null +++ b/Lib/test/namespace_pkgs/module_and_file/a_test.py @@ -0,0 +1 @@ +attr = 'in module' diff --git a/Lib/test/namespace_pkgs/module_and_file/a_test/empty b/Lib/test/namespace_pkgs/module_and_file/a_test/empty new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Lib/test/namespace_pkgs/module_and_file/a_test/empty diff --git a/Lib/test/test_namespace_pkgs.py b/Lib/test/test_namespace_pkgs.py index 02b5528..7ad511f 100644 --- a/Lib/test/test_namespace_pkgs.py +++ b/Lib/test/test_namespace_pkgs.py @@ -276,6 +276,14 @@ class ZipWithMissingDirectory(NamespacePackageTest): self.assertEqual(bar.two.attr, 'missing_directory foo two') +class ModuleAndFileInSameDir(NamespacePackageTest): + paths = ['module_and_file'] + + def test_module_before_namespace_package(self): + import a_test + self.assertEqual(a_test.attr, 'in module') + + def test_main(): run_unittest(*NamespacePackageTest.__subclasses__()) |