mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #13959: Have
importlib.abc.FileLoader.load_module()/get_filename() and importlib.machinery.ExtensionFileLoader.load_module() have their single argument be optional as the loader's constructor has all the ncessary information. This allows for the deprecation of imp.load_source()/load_compile()/load_package().
This commit is contained in:
parent
0c59b039b8
commit
c049952de7
9 changed files with 2485 additions and 2392 deletions
|
@ -1,4 +1,4 @@
|
|||
from importlib import _bootstrap
|
||||
from importlib import machinery
|
||||
from . import util as ext_util
|
||||
from .. import abc
|
||||
from .. import util
|
||||
|
@ -11,10 +11,20 @@ class LoaderTests(abc.LoaderTests):
|
|||
|
||||
"""Test load_module() for extension modules."""
|
||||
|
||||
def setUp(self):
|
||||
self.loader = machinery.ExtensionFileLoader(ext_util.NAME,
|
||||
ext_util.FILEPATH)
|
||||
|
||||
def load_module(self, fullname):
|
||||
loader = _bootstrap.ExtensionFileLoader(ext_util.NAME,
|
||||
ext_util.FILEPATH)
|
||||
return loader.load_module(fullname)
|
||||
return self.loader.load_module(fullname)
|
||||
|
||||
def test_load_module_API(self):
|
||||
# Test the default argument for load_module().
|
||||
self.loader.load_module()
|
||||
self.loader.load_module(None)
|
||||
with self.assertRaises(ImportError):
|
||||
self.load_module('XXX')
|
||||
|
||||
|
||||
def test_module(self):
|
||||
with util.uncache(ext_util.NAME):
|
||||
|
@ -25,7 +35,7 @@ class LoaderTests(abc.LoaderTests):
|
|||
self.assertEqual(getattr(module, attr), value)
|
||||
self.assertTrue(ext_util.NAME in sys.modules)
|
||||
self.assertTrue(isinstance(module.__loader__,
|
||||
_bootstrap.ExtensionFileLoader))
|
||||
machinery.ExtensionFileLoader))
|
||||
|
||||
def test_package(self):
|
||||
# Extensions are not found in packages.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from ... import _bootstrap
|
||||
import importlib
|
||||
import importlib.abc
|
||||
from .. import abc
|
||||
from .. import util
|
||||
from . import util as source_util
|
||||
|
@ -24,6 +25,40 @@ class SimpleTest(unittest.TestCase):
|
|||
|
||||
"""
|
||||
|
||||
def test_load_module_API(self):
|
||||
# If fullname is not specified that assume self.name is desired.
|
||||
class TesterMixin(importlib.abc.Loader):
|
||||
def load_module(self, fullname): return fullname
|
||||
|
||||
class Tester(importlib.abc.FileLoader, TesterMixin):
|
||||
def get_code(self, _): pass
|
||||
def get_source(self, _): pass
|
||||
def is_package(self, _): pass
|
||||
|
||||
name = 'mod_name'
|
||||
loader = Tester(name, 'some_path')
|
||||
self.assertEqual(name, loader.load_module())
|
||||
self.assertEqual(name, loader.load_module(None))
|
||||
self.assertEqual(name, loader.load_module(name))
|
||||
with self.assertRaises(ImportError):
|
||||
loader.load_module(loader.name + 'XXX')
|
||||
|
||||
def test_get_filename_API(self):
|
||||
# If fullname is not set then assume self.path is desired.
|
||||
class Tester(importlib.abc.FileLoader):
|
||||
def get_code(self, _): pass
|
||||
def get_source(self, _): pass
|
||||
def is_package(self, _): pass
|
||||
|
||||
path = 'some_path'
|
||||
name = 'some_name'
|
||||
loader = Tester(name, path)
|
||||
self.assertEqual(path, loader.get_filename(name))
|
||||
self.assertEqual(path, loader.get_filename())
|
||||
self.assertEqual(path, loader.get_filename(None))
|
||||
with self.assertRaises(ImportError):
|
||||
loader.get_filename(name + 'XXX')
|
||||
|
||||
# [basic]
|
||||
def test_module(self):
|
||||
with source_util.create_modules('_temp') as mapping:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue