mirror of
https://github.com/python/cpython.git
synced 2025-09-04 16:01:10 +00:00
Merge fix for #16163 from 3.3
This commit is contained in:
commit
685bd4a37a
3 changed files with 44 additions and 2 deletions
|
@ -449,8 +449,8 @@ def iter_importers(fullname=""):
|
||||||
if '.' in fullname:
|
if '.' in fullname:
|
||||||
# Get the containing package's __path__
|
# Get the containing package's __path__
|
||||||
pkg_name = fullname.rpartition(".")[0]
|
pkg_name = fullname.rpartition(".")[0]
|
||||||
pkg = importlib.import_module(pkg)
|
pkg = importlib.import_module(pkg_name)
|
||||||
path = getattr(sys.modules[pkg], '__path__', None)
|
path = getattr(pkg, '__path__', None)
|
||||||
if path is None:
|
if path is None:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -2,6 +2,7 @@ from test.support import run_unittest, unload, check_warnings
|
||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
import imp
|
import imp
|
||||||
|
import importlib
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
|
@ -187,6 +188,44 @@ class ExtendPathTests(unittest.TestCase):
|
||||||
del sys.modules['foo.bar']
|
del sys.modules['foo.bar']
|
||||||
del sys.modules['foo.baz']
|
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):
|
def test_mixed_namespace(self):
|
||||||
pkgname = 'foo'
|
pkgname = 'foo'
|
||||||
dirname_0 = self.create_init(pkgname)
|
dirname_0 = self.create_init(pkgname)
|
||||||
|
|
|
@ -46,6 +46,9 @@ Core and Builtins
|
||||||
Library
|
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
|
- Issue #16804: Fix a bug in the 'site' module that caused running
|
||||||
'python -S -m site' to incorrectly throw an exception.
|
'python -S -m site' to incorrectly throw an exception.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue