[Python-checkins] cpython (merge 3.3 -> default): Merge fix for #16163 from 3.3

nick.coghlan python-checkins at python.org
Sun Apr 14 15:01:24 CEST 2013


http://hg.python.org/cpython/rev/3bb5a8a4830e
changeset:   83367:3bb5a8a4830e
parent:      83365:efda51b85b31
parent:      83366:c40b50d65a00
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sun Apr 14 23:01:11 2013 +1000
summary:
  Merge fix for #16163 from 3.3

files:
  Lib/pkgutil.py           |   4 +-
  Lib/test/test_pkgutil.py |  39 ++++++++++++++++++++++++++++
  Misc/NEWS                |   3 ++
  3 files changed, 44 insertions(+), 2 deletions(-)


diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -449,8 +449,8 @@
     if '.' in fullname:
         # Get the containing package's __path__
         pkg_name = fullname.rpartition(".")[0]
-        pkg = importlib.import_module(pkg)
-        path = getattr(sys.modules[pkg], '__path__', None)
+        pkg = importlib.import_module(pkg_name)
+        path = getattr(pkg, '__path__', None)
         if path is None:
             return
     else:
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -2,6 +2,7 @@
 import unittest
 import sys
 import imp
+import importlib
 import pkgutil
 import os
 import os.path
@@ -187,6 +188,44 @@
         del sys.modules['foo.bar']
         del sys.modules['foo.baz']
 
+
+    # Another awful testing hack to be cleaned up once the test_runpy
+    # helpers are factored out to a common location
+    def test_iter_importers(self):
+        iter_importers = pkgutil.iter_importers
+        get_importer = pkgutil.get_importer
+
+        pkgname = 'spam'
+        modname = 'eggs'
+        dirname = self.create_init(pkgname)
+        pathitem = os.path.join(dirname, pkgname)
+        fullname = '{}.{}'.format(pkgname, modname)
+        try:
+            self.create_submodule(dirname, pkgname, modname, 0)
+
+            importlib.import_module(fullname)
+
+            importers = list(iter_importers(fullname))
+            expected_importer = get_importer(pathitem)
+            for finder in importers:
+                self.assertIsInstance(finder, importlib.machinery.FileFinder)
+                self.assertEqual(finder, expected_importer)
+                self.assertIsInstance(finder.find_module(fullname),
+                                      importlib.machinery.SourceFileLoader)
+                self.assertIsNone(finder.find_module(pkgname))
+
+            with self.assertRaises(ImportError):
+                list(iter_importers('invalid.module'))
+
+            with self.assertRaises(ImportError):
+                list(iter_importers('.spam'))
+        finally:
+            shutil.rmtree(dirname)
+            del sys.path[0]
+            del sys.modules['spam']
+            del sys.modules['spam.eggs']
+
+
     def test_mixed_namespace(self):
         pkgname = 'foo'
         dirname_0 = self.create_init(pkgname)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,9 @@
 Library
 -------
 
+- Issue #16163: Make the importlib based version of pkgutil.iter_importers
+  work for submodules. Initial patch by Berker Peksag.
+
 - Issue #16804: Fix a bug in the 'site' module that caused running
   'python -S -m site' to incorrectly throw an exception.
 

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


More information about the Python-checkins mailing list