mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
Close issue #16163: handle submodules in pkgutil.iter_importers
This commit is contained in:
parent
8157459d6b
commit
c4e0d982f3
3 changed files with 44 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ from test.support import run_unittest, unload, check_warnings
|
|||
import unittest
|
||||
import sys
|
||||
import imp
|
||||
import importlib
|
||||
import pkgutil
|
||||
import os
|
||||
import os.path
|
||||
|
|
@ -187,6 +188,44 @@ class ExtendPathTests(unittest.TestCase):
|
|||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue