| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -449,8 +449,8 @@ def iter_importers(fullname=""): | |||
| 449 | 449 | if '.' in fullname: | |
| 450 | 450 | # Get the containing package's __path__ | |
| 451 | 451 | pkg_name = fullname.rpartition(".")[0] | |
| 452 | - pkg = importlib.import_module(pkg) | ||
| 453 | - path = getattr(sys.modules[pkg], '__path__', None) | ||
| 452 | + pkg = importlib.import_module(pkg_name) | ||
| 453 | + path = getattr(pkg, '__path__', None) | ||
| 454 | 454 | if path is None: | |
| 455 | 455 | return | |
| 456 | 456 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | import unittest | |
| 3 | 3 | import sys | |
| 4 | 4 | import imp | |
| 5 | + import importlib | ||
| 5 | 6 | import pkgutil | |
| 6 | 7 | import os | |
| 7 | 8 | import os.path | |
@@ -187,6 +188,44 @@ def test_simple(self): | |||
| 187 | 188 | del sys.modules['foo.bar'] | |
| 188 | 189 | del sys.modules['foo.baz'] | |
| 189 | 190 | ||
| 191 | + | ||
| 192 | + # Another awful testing hack to be cleaned up once the test_runpy | ||
| 193 | + # helpers are factored out to a common location | ||
| 194 | + def test_iter_importers(self): | ||
| 195 | + iter_importers = pkgutil.iter_importers | ||
| 196 | + get_importer = pkgutil.get_importer | ||
| 197 | + | ||
| 198 | + pkgname = 'spam' | ||
| 199 | + modname = 'eggs' | ||
| 200 | + dirname = self.create_init(pkgname) | ||
| 201 | + pathitem = os.path.join(dirname, pkgname) | ||
| 202 | + fullname = '{}.{}'.format(pkgname, modname) | ||
| 203 | + try: | ||
| 204 | + self.create_submodule(dirname, pkgname, modname, 0) | ||
| 205 | + | ||
| 206 | + importlib.import_module(fullname) | ||
| 207 | + | ||
| 208 | + importers = list(iter_importers(fullname)) | ||
| 209 | + expected_importer = get_importer(pathitem) | ||
| 210 | + for finder in importers: | ||
| 211 | + self.assertIsInstance(finder, importlib.machinery.FileFinder) | ||
| 212 | + self.assertEqual(finder, expected_importer) | ||
| 213 | + self.assertIsInstance(finder.find_module(fullname), | ||
| 214 | + importlib.machinery.SourceFileLoader) | ||
| 215 | + self.assertIsNone(finder.find_module(pkgname)) | ||
| 216 | + | ||
| 217 | + with self.assertRaises(ImportError): | ||
| 218 | + list(iter_importers('invalid.module')) | ||
| 219 | + | ||
| 220 | + with self.assertRaises(ImportError): | ||
| 221 | + list(iter_importers('.spam')) | ||
| 222 | + finally: | ||
| 223 | + shutil.rmtree(dirname) | ||
| 224 | + del sys.path[0] | ||
| 225 | + del sys.modules['spam'] | ||
| 226 | + del sys.modules['spam.eggs'] | ||
| 227 | + | ||
| 228 | + | ||
| 190 | 229 | def test_mixed_namespace(self): | |
| 191 | 230 | pkgname = 'foo' | |
| 192 | 231 | dirname_0 = self.create_init(pkgname) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,6 +46,9 @@ Core and Builtins | |||
| 46 | 46 | Library | |
| 47 | 47 | ------- | |
| 48 | 48 | ||
| 49 | + - Issue #16163: Make the importlib based version of pkgutil.iter_importers | ||
| 50 | + work for submodules. Initial patch by Berker Peksag. | ||
| 51 | + | ||
| 49 | 52 | - Issue #16804: Fix a bug in the 'site' module that caused running | |
| 50 | 53 | 'python -S -m site' to incorrectly throw an exception. | |
| 51 | 54 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments