2009-02-16 22:45:03 -04:00
|
|
|
from importlib import util
|
|
|
|
from . import util as test_util
|
|
|
|
import imp
|
|
|
|
import sys
|
2013-05-28 18:29:34 -03:00
|
|
|
from test import support
|
2009-02-16 22:45:03 -04:00
|
|
|
import types
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2013-05-28 18:29:34 -03:00
|
|
|
class ModuleManagerTests(unittest.TestCase):
|
|
|
|
|
|
|
|
module_name = 'ModuleManagerTest_module'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
support.unload(self.module_name)
|
|
|
|
self.addCleanup(support.unload, self.module_name)
|
|
|
|
|
|
|
|
def test_new_module(self):
|
|
|
|
# Test a new module is created, inserted into sys.modules, has
|
|
|
|
# __initializing__ set to True after entering the context manager,
|
|
|
|
# and __initializing__ set to False after exiting.
|
|
|
|
with util.ModuleManager(self.module_name) as module:
|
|
|
|
self.assertIn(self.module_name, sys.modules)
|
|
|
|
self.assertIs(sys.modules[self.module_name], module)
|
|
|
|
self.assertTrue(module.__initializing__)
|
|
|
|
self.assertFalse(module.__initializing__)
|
|
|
|
|
|
|
|
def test_new_module_failed(self):
|
|
|
|
# Test the module is removed from sys.modules.
|
|
|
|
try:
|
|
|
|
with util.ModuleManager(self.module_name) as module:
|
|
|
|
self.assertIn(self.module_name, sys.modules)
|
|
|
|
raise exception
|
|
|
|
except Exception:
|
|
|
|
self.assertNotIn(self.module_name, sys.modules)
|
|
|
|
else:
|
|
|
|
self.fail('importlib.util.ModuleManager swallowed an exception')
|
|
|
|
|
|
|
|
def test_reload(self):
|
|
|
|
# Test that the same module is in sys.modules.
|
|
|
|
created_module = imp.new_module(self.module_name)
|
|
|
|
sys.modules[self.module_name] = created_module
|
|
|
|
with util.ModuleManager(self.module_name) as module:
|
|
|
|
self.assertIs(module, created_module)
|
|
|
|
|
|
|
|
def test_reload_failed(self):
|
|
|
|
# Test that the module was left in sys.modules.
|
|
|
|
created_module = imp.new_module(self.module_name)
|
|
|
|
sys.modules[self.module_name] = created_module
|
|
|
|
try:
|
|
|
|
with util.ModuleManager(self.module_name) as module:
|
|
|
|
raise Exception
|
|
|
|
except Exception:
|
|
|
|
self.assertIn(self.module_name, sys.modules)
|
|
|
|
else:
|
|
|
|
self.fail('importlib.util.ModuleManager swallowed an exception')
|
|
|
|
|
|
|
|
|
2009-02-16 22:45:03 -04:00
|
|
|
class ModuleForLoaderTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Tests for importlib.util.module_for_loader."""
|
|
|
|
|
|
|
|
def return_module(self, name):
|
|
|
|
fxn = util.module_for_loader(lambda self, module: module)
|
|
|
|
return fxn(self, name)
|
|
|
|
|
|
|
|
def raise_exception(self, name):
|
|
|
|
def to_wrap(self, module):
|
|
|
|
raise ImportError
|
|
|
|
fxn = util.module_for_loader(to_wrap)
|
|
|
|
try:
|
|
|
|
fxn(self, name)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_new_module(self):
|
|
|
|
# Test that when no module exists in sys.modules a new module is
|
|
|
|
# created.
|
|
|
|
module_name = 'a.b.c'
|
|
|
|
with test_util.uncache(module_name):
|
|
|
|
module = self.return_module(module_name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn(module_name, sys.modules)
|
|
|
|
self.assertIsInstance(module, types.ModuleType)
|
2009-02-16 22:45:03 -04:00
|
|
|
self.assertEqual(module.__name__, module_name)
|
|
|
|
|
|
|
|
def test_reload(self):
|
|
|
|
# Test that a module is reused if already in sys.modules.
|
|
|
|
name = 'a.b.c'
|
|
|
|
module = imp.new_module('a.b.c')
|
|
|
|
with test_util.uncache(name):
|
|
|
|
sys.modules[name] = module
|
|
|
|
returned_module = self.return_module(name)
|
2010-06-20 23:49:35 -03:00
|
|
|
self.assertIs(returned_module, sys.modules[name])
|
2009-02-16 22:45:03 -04:00
|
|
|
|
|
|
|
def test_new_module_failure(self):
|
|
|
|
# Test that a module is removed from sys.modules if added but an
|
|
|
|
# exception is raised.
|
|
|
|
name = 'a.b.c'
|
|
|
|
with test_util.uncache(name):
|
|
|
|
self.raise_exception(name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertNotIn(name, sys.modules)
|
2009-02-16 22:45:03 -04:00
|
|
|
|
|
|
|
def test_reload_failure(self):
|
|
|
|
# Test that a failure on reload leaves the module in-place.
|
|
|
|
name = 'a.b.c'
|
|
|
|
module = imp.new_module(name)
|
|
|
|
with test_util.uncache(name):
|
|
|
|
sys.modules[name] = module
|
|
|
|
self.raise_exception(name)
|
2010-06-20 23:49:35 -03:00
|
|
|
self.assertIs(module, sys.modules[name])
|
2009-02-16 22:45:03 -04:00
|
|
|
|
2011-12-15 00:53:13 -04:00
|
|
|
def test_decorator_attrs(self):
|
|
|
|
def fxn(self, module): pass
|
|
|
|
wrapped = util.module_for_loader(fxn)
|
|
|
|
self.assertEqual(wrapped.__name__, fxn.__name__)
|
|
|
|
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
|
2009-02-16 22:45:03 -04:00
|
|
|
|
2012-04-17 22:41:35 -03:00
|
|
|
def test_false_module(self):
|
|
|
|
# If for some odd reason a module is considered false, still return it
|
|
|
|
# from sys.modules.
|
|
|
|
class FalseModule(types.ModuleType):
|
|
|
|
def __bool__(self): return False
|
|
|
|
|
|
|
|
name = 'mod'
|
|
|
|
module = FalseModule(name)
|
|
|
|
with test_util.uncache(name):
|
|
|
|
self.assertFalse(module)
|
|
|
|
sys.modules[name] = module
|
|
|
|
given = self.return_module(name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIs(given, module)
|
2009-03-02 10:38:26 -04:00
|
|
|
|
2012-04-27 18:27:14 -03:00
|
|
|
def test_attributes_set(self):
|
|
|
|
# __name__, __loader__, and __package__ should be set (when
|
|
|
|
# is_package() is defined; undefined implicitly tested elsewhere).
|
|
|
|
class FakeLoader:
|
|
|
|
def __init__(self, is_package):
|
|
|
|
self._pkg = is_package
|
|
|
|
def is_package(self, name):
|
|
|
|
return self._pkg
|
|
|
|
@util.module_for_loader
|
|
|
|
def load_module(self, module):
|
|
|
|
return module
|
|
|
|
|
|
|
|
name = 'pkg.mod'
|
|
|
|
with test_util.uncache(name):
|
|
|
|
loader = FakeLoader(False)
|
|
|
|
module = loader.load_module(name)
|
|
|
|
self.assertEqual(module.__name__, name)
|
|
|
|
self.assertIs(module.__loader__, loader)
|
|
|
|
self.assertEqual(module.__package__, 'pkg')
|
|
|
|
|
|
|
|
name = 'pkg.sub'
|
|
|
|
with test_util.uncache(name):
|
|
|
|
loader = FakeLoader(True)
|
|
|
|
module = loader.load_module(name)
|
|
|
|
self.assertEqual(module.__name__, name)
|
|
|
|
self.assertIs(module.__loader__, loader)
|
|
|
|
self.assertEqual(module.__package__, name)
|
|
|
|
|
2009-03-02 10:38:26 -04:00
|
|
|
|
2012-04-17 22:41:35 -03:00
|
|
|
class SetPackageTests(unittest.TestCase):
|
|
|
|
|
2009-03-04 12:07:00 -04:00
|
|
|
"""Tests for importlib.util.set_package."""
|
2009-03-02 10:38:26 -04:00
|
|
|
|
|
|
|
def verify(self, module, expect):
|
|
|
|
"""Verify the module has the expected value for __package__ after
|
2009-03-04 12:07:00 -04:00
|
|
|
passing through set_package."""
|
2009-03-02 10:38:26 -04:00
|
|
|
fxn = lambda: module
|
2009-03-04 12:07:00 -04:00
|
|
|
wrapped = util.set_package(fxn)
|
2009-03-02 10:38:26 -04:00
|
|
|
wrapped()
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(hasattr(module, '__package__'))
|
2009-03-02 10:38:26 -04:00
|
|
|
self.assertEqual(expect, module.__package__)
|
|
|
|
|
|
|
|
def test_top_level(self):
|
|
|
|
# __package__ should be set to the empty string if a top-level module.
|
|
|
|
# Implicitly tests when package is set to None.
|
|
|
|
module = imp.new_module('module')
|
|
|
|
module.__package__ = None
|
|
|
|
self.verify(module, '')
|
|
|
|
|
|
|
|
def test_package(self):
|
|
|
|
# Test setting __package__ for a package.
|
|
|
|
module = imp.new_module('pkg')
|
|
|
|
module.__path__ = ['<path>']
|
|
|
|
module.__package__ = None
|
|
|
|
self.verify(module, 'pkg')
|
|
|
|
|
|
|
|
def test_submodule(self):
|
|
|
|
# Test __package__ for a module in a package.
|
|
|
|
module = imp.new_module('pkg.mod')
|
|
|
|
module.__package__ = None
|
|
|
|
self.verify(module, 'pkg')
|
|
|
|
|
|
|
|
def test_setting_if_missing(self):
|
|
|
|
# __package__ should be set if it is missing.
|
|
|
|
module = imp.new_module('mod')
|
|
|
|
if hasattr(module, '__package__'):
|
|
|
|
delattr(module, '__package__')
|
|
|
|
self.verify(module, '')
|
|
|
|
|
|
|
|
def test_leaving_alone(self):
|
|
|
|
# If __package__ is set and not None then leave it alone.
|
|
|
|
for value in (True, False):
|
|
|
|
module = imp.new_module('mod')
|
|
|
|
module.__package__ = value
|
|
|
|
self.verify(module, value)
|
|
|
|
|
2011-12-15 00:53:13 -04:00
|
|
|
def test_decorator_attrs(self):
|
|
|
|
def fxn(module): pass
|
|
|
|
wrapped = util.set_package(fxn)
|
|
|
|
self.assertEqual(wrapped.__name__, fxn.__name__)
|
|
|
|
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
|
2009-03-02 10:38:26 -04:00
|
|
|
|
2012-05-13 14:45:09 -03:00
|
|
|
|
2013-03-13 14:41:36 -03:00
|
|
|
class SetLoaderTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Tests importlib.util.set_loader()."""
|
|
|
|
|
|
|
|
class DummyLoader:
|
|
|
|
@util.set_loader
|
|
|
|
def load_module(self, module):
|
|
|
|
return self.module
|
|
|
|
|
|
|
|
def test_no_attribute(self):
|
|
|
|
loader = self.DummyLoader()
|
|
|
|
loader.module = imp.new_module('blah')
|
|
|
|
try:
|
|
|
|
del loader.module.__loader__
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
self.assertEqual(loader, loader.load_module('blah').__loader__)
|
|
|
|
|
|
|
|
def test_attribute_is_None(self):
|
|
|
|
loader = self.DummyLoader()
|
|
|
|
loader.module = imp.new_module('blah')
|
|
|
|
loader.module.__loader__ = None
|
|
|
|
self.assertEqual(loader, loader.load_module('blah').__loader__)
|
|
|
|
|
|
|
|
def test_not_reset(self):
|
|
|
|
loader = self.DummyLoader()
|
|
|
|
loader.module = imp.new_module('blah')
|
|
|
|
loader.module.__loader__ = 42
|
|
|
|
self.assertEqual(42, loader.load_module('blah').__loader__)
|
|
|
|
|
|
|
|
|
2012-05-13 14:45:09 -03:00
|
|
|
class ResolveNameTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Tests importlib.util.resolve_name()."""
|
|
|
|
|
|
|
|
def test_absolute(self):
|
|
|
|
# bacon
|
|
|
|
self.assertEqual('bacon', util.resolve_name('bacon', None))
|
|
|
|
|
|
|
|
def test_aboslute_within_package(self):
|
|
|
|
# bacon in spam
|
|
|
|
self.assertEqual('bacon', util.resolve_name('bacon', 'spam'))
|
|
|
|
|
|
|
|
def test_no_package(self):
|
|
|
|
# .bacon in ''
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
util.resolve_name('.bacon', '')
|
|
|
|
|
|
|
|
def test_in_package(self):
|
|
|
|
# .bacon in spam
|
|
|
|
self.assertEqual('spam.eggs.bacon',
|
|
|
|
util.resolve_name('.bacon', 'spam.eggs'))
|
|
|
|
|
|
|
|
def test_other_package(self):
|
|
|
|
# ..bacon in spam.bacon
|
|
|
|
self.assertEqual('spam.bacon',
|
|
|
|
util.resolve_name('..bacon', 'spam.eggs'))
|
|
|
|
|
|
|
|
def test_escape(self):
|
|
|
|
# ..bacon in spam
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
util.resolve_name('..bacon', 'spam')
|
|
|
|
|
|
|
|
|
2009-02-16 22:45:03 -04:00
|
|
|
if __name__ == '__main__':
|
2013-03-13 14:41:36 -03:00
|
|
|
unittest.main()
|