Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e51a369

Browse files
committed
Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
1 parent e6bdc8f commit e51a369

7 files changed

Lines changed: 975 additions & 957 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ def find_module(self, fullname):
10901090
def find_loader(self, fullname):
10911091
"""Try to find a loader for the specified module, or the namespace
10921092
package portions. Returns (loader, list-of-portions)."""
1093+
is_namespace = False
10931094
tail_module = fullname.rpartition('.')[2]
10941095
try:
10951096
mtime = _os.stat(self.path).st_mtime
@@ -1115,14 +1116,17 @@ def find_loader(self, fullname):
11151116
if _path_isfile(full_path):
11161117
return (loader(fullname, full_path), [base_path])
11171118
else:
1118-
# A namespace package, return the path
1119-
return (None, [base_path])
1119+
# A namespace package, return the path if we don't also
1120+
# find a module in the next section.
1121+
is_namespace = True
11201122
# Check for a file w/ a proper suffix exists.
11211123
for suffix, loader in self.modules:
11221124
if cache_module + suffix in cache:
11231125
full_path = _path_join(self.path, tail_module + suffix)
11241126
if _path_isfile(full_path):
11251127
return (loader(fullname, full_path), [])
1128+
if is_namespace:
1129+
return (None, [base_path])
11261130
return (None, [])
11271131

11281132
def _fill_cache(self):

Lib/importlib/test/source/test_finder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_package_in_package(self):
110110
def test_package_over_module(self):
111111
name = '_temp'
112112
loader = self.run_test(name, {'{0}.__init__'.format(name), name})
113-
self.assertTrue('__init__' in loader.get_filename(name))
113+
self.assertIn('__init__', loader.get_filename(name))
114114

115115
def test_failure(self):
116116
with source_util.create_modules('blah') as mapping:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
attr = 'in module'

Lib/test/namespace_pkgs/module_and_file/a_test/empty

Whitespace-only changes.

Lib/test/test_namespace_pkgs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@ def test_present_directory(self):
276276
self.assertEqual(bar.two.attr, 'missing_directory foo two')
277277

278278

279+
class ModuleAndFileInSameDir(NamespacePackageTest):
280+
paths = ['module_and_file']
281+
282+
def test_module_before_namespace_package(self):
283+
import a_test
284+
self.assertEqual(a_test.attr, 'in module')
285+
286+
279287
def test_main():
280288
run_unittest(*NamespacePackageTest.__subclasses__())
281289

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,8 @@ LIBSUBDIRS= tkinter tkinter/test tkinter/test/test_tkinter \
991991
test/namespace_pkgs/project3 \
992992
test/namespace_pkgs/project3/parent \
993993
test/namespace_pkgs/project3/parent/child \
994+
test/namespace_pkgs/module_and_file \
995+
test/namespace_pkgs/module_and_file/a_test \
994996
collections concurrent concurrent/futures encodings \
995997
email email/mime test/test_email test/test_email/data \
996998
html json test/json_tests http dbm xmlrpc \

Python/importlib.h

Lines changed: 957 additions & 954 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)