def test_reload_loader_replaced(self): with import_helper.CleanImport('types'): import types types.__loader__ = None self.init.invalidate_caches() reloaded = self.init.reload(types) self.assertIsNot(reloaded.__loader__, None) self.assertIs(reloaded, types) self.assertIs(sys.modules['types'], types)
def test_reload_missing_loader(self): with import_helper.CleanImport('types'): import types loader = types.__loader__ del types.__loader__ reloaded = self.init.reload(types) self.assertIs(reloaded, types) self.assertIs(sys.modules['types'], types) self.assertEqual(reloaded.__loader__.path, loader.path)
def test_frozen_submodule_in_unfrozen_package(self): with import_helper.CleanImport('__phello__', '__phello__.spam'): with import_helper.frozen_modules(enabled=False): import __phello__ with import_helper.frozen_modules(enabled=True): import __phello__.spam as spam self.assertIs(spam, __phello__.spam) self.assertIsNot(__phello__.__spec__.loader, importlib.machinery.FrozenImporter) self.assertIs(spam.__spec__.loader, importlib.machinery.FrozenImporter)
def test_target_ignored(self): imported = ('__hello__', '__phello__') with import_helper.CleanImport(*imported, usefrozen=True): import __hello__ as match import __phello__ as nonmatch name = '__hello__' actual = self.find(name) for target in (None, match, nonmatch, object(), 'not-a-module-object'): with self.subTest(target): spec = self.find(name, target=target) self.assertEqual(spec, actual)
def test_load_from_source(self): # Verify that the imp module can correctly load and find .py files # XXX (ncoghlan): It would be nice to use import_helper.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't # revert that registration, the module is left in a broken # state after reversion. Reinitialising the module contents # and just reverting os.environ to its previous state is an OK # workaround with import_helper.CleanImport('os', 'os.path', OS_PATH_NAME): import os orig_path = os.path orig_getenv = os.getenv with os_helper.EnvironmentVarGuard(): x = imp.find_module("os") self.addCleanup(x[0].close) new_os = imp.load_module("os", *x) self.assertIs(os, new_os) self.assertIs(orig_path, new_os.path) self.assertIsNot(orig_getenv, new_os.getenv)
def test_builtin(self): with import_helper.CleanImport('marshal'): import marshal imp.reload(marshal)
def test_extension(self): with import_helper.CleanImport('time'): import time imp.reload(time)
def test_CleanImport(self): import importlib with import_helper.CleanImport("asyncore"): importlib.import_module("asyncore")
def test_future3(self): with import_helper.CleanImport('test_future3'): from test import test_future3
def test_future2(self): with import_helper.CleanImport('future_test2'): from test import future_test2 self.assertEqual(future_test2.result, 6)
def test_multiple_features(self): with import_helper.CleanImport("test.test_future5"): from test import test_future5
def test_CleanImport(self): import importlib with import_helper.CleanImport("pprint"): importlib.import_module("pprint")
def test_reload_modules(self): for mod in ('tokenize', 'time', 'marshal'): with self.subTest(module=mod): with import_helper.CleanImport(mod): module = self.init.import_module(mod) self.init.reload(module)