[Python-checkins] cpython: Fixes issue 15039: namespace packages are no longer imported in preference to

eric.smith python-checkins at python.org
Mon Jun 25 01:14:05 CEST 2012


http://hg.python.org/cpython/rev/2051fead1933
changeset:   77757:2051fead1933
user:        Eric V. Smith <eric at trueblade.com>
date:        Sun Jun 24 19:13:55 2012 -0400
summary:
  Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.

files:
  Lib/importlib/_bootstrap.py                          |     8 +-
  Lib/importlib/test/source/test_finder.py             |     2 +-
  Lib/test/namespace_pkgs/module_and_file/a_test.py    |     1 +
  Lib/test/namespace_pkgs/module_and_file/a_test/empty |     0 
  Lib/test/test_namespace_pkgs.py                      |     8 +
  Makefile.pre.in                                      |     2 +
  Python/importlib.h                                   |  1933 +++++----
  7 files changed, 986 insertions(+), 968 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1090,6 +1090,7 @@
     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 @@
                     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
--- a/Lib/importlib/test/source/test_finder.py
+++ b/Lib/importlib/test/source/test_finder.py
@@ -110,7 +110,7 @@
     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
--- /dev/null
+++ b/Lib/test/namespace_pkgs/module_and_file/a_test.py
@@ -0,0 +1,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
diff --git a/Lib/test/test_namespace_pkgs.py b/Lib/test/test_namespace_pkgs.py
--- a/Lib/test/test_namespace_pkgs.py
+++ b/Lib/test/test_namespace_pkgs.py
@@ -276,6 +276,14 @@
         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__())
 
diff --git a/Makefile.pre.in b/Makefile.pre.in
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -991,6 +991,8 @@
 		test/namespace_pkgs/project3 \
 		test/namespace_pkgs/project3/parent \
 		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 \
 		email email/mime test/test_email test/test_email/data \
 		html json test/json_tests http dbm xmlrpc \
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list