mirror of https://github.com/python/cpython.git
Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
This commit is contained in:
parent
e6bdc8f2dd
commit
e51a36922f
|
@ -1090,6 +1090,7 @@ def find_module(self, fullname):
|
||||||
def find_loader(self, fullname):
|
def find_loader(self, fullname):
|
||||||
"""Try to find a loader for the specified module, or the namespace
|
"""Try to find a loader for the specified module, or the namespace
|
||||||
package portions. Returns (loader, list-of-portions)."""
|
package portions. Returns (loader, list-of-portions)."""
|
||||||
|
is_namespace = False
|
||||||
tail_module = fullname.rpartition('.')[2]
|
tail_module = fullname.rpartition('.')[2]
|
||||||
try:
|
try:
|
||||||
mtime = _os.stat(self.path).st_mtime
|
mtime = _os.stat(self.path).st_mtime
|
||||||
|
@ -1115,14 +1116,17 @@ def find_loader(self, fullname):
|
||||||
if _path_isfile(full_path):
|
if _path_isfile(full_path):
|
||||||
return (loader(fullname, full_path), [base_path])
|
return (loader(fullname, full_path), [base_path])
|
||||||
else:
|
else:
|
||||||
# A namespace package, return the path
|
# A namespace package, return the path if we don't also
|
||||||
return (None, [base_path])
|
# find a module in the next section.
|
||||||
|
is_namespace = True
|
||||||
# Check for a file w/ a proper suffix exists.
|
# Check for a file w/ a proper suffix exists.
|
||||||
for suffix, loader in self.modules:
|
for suffix, loader in self.modules:
|
||||||
if cache_module + suffix in cache:
|
if cache_module + suffix in cache:
|
||||||
full_path = _path_join(self.path, tail_module + suffix)
|
full_path = _path_join(self.path, tail_module + suffix)
|
||||||
if _path_isfile(full_path):
|
if _path_isfile(full_path):
|
||||||
return (loader(fullname, full_path), [])
|
return (loader(fullname, full_path), [])
|
||||||
|
if is_namespace:
|
||||||
|
return (None, [base_path])
|
||||||
return (None, [])
|
return (None, [])
|
||||||
|
|
||||||
def _fill_cache(self):
|
def _fill_cache(self):
|
||||||
|
|
|
@ -110,7 +110,7 @@ def test_package_in_package(self):
|
||||||
def test_package_over_module(self):
|
def test_package_over_module(self):
|
||||||
name = '_temp'
|
name = '_temp'
|
||||||
loader = self.run_test(name, {'{0}.__init__'.format(name), name})
|
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):
|
def test_failure(self):
|
||||||
with source_util.create_modules('blah') as mapping:
|
with source_util.create_modules('blah') as mapping:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
attr = 'in module'
|
|
@ -276,6 +276,14 @@ def test_present_directory(self):
|
||||||
self.assertEqual(bar.two.attr, 'missing_directory foo two')
|
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():
|
def test_main():
|
||||||
run_unittest(*NamespacePackageTest.__subclasses__())
|
run_unittest(*NamespacePackageTest.__subclasses__())
|
||||||
|
|
||||||
|
|
|
@ -991,6 +991,8 @@ LIBSUBDIRS= tkinter tkinter/test tkinter/test/test_tkinter \
|
||||||
test/namespace_pkgs/project3 \
|
test/namespace_pkgs/project3 \
|
||||||
test/namespace_pkgs/project3/parent \
|
test/namespace_pkgs/project3/parent \
|
||||||
test/namespace_pkgs/project3/parent/child \
|
test/namespace_pkgs/project3/parent/child \
|
||||||
|
test/namespace_pkgs/module_and_file \
|
||||||
|
test/namespace_pkgs/module_and_file/a_test \
|
||||||
collections concurrent concurrent/futures encodings \
|
collections concurrent concurrent/futures encodings \
|
||||||
email email/mime test/test_email test/test_email/data \
|
email email/mime test/test_email test/test_email/data \
|
||||||
html json test/json_tests http dbm xmlrpc \
|
html json test/json_tests http dbm xmlrpc \
|
||||||
|
|
1927
Python/importlib.h
1927
Python/importlib.h
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue