def test_continuing(self): # [continuing] mod_name = 'for_real' with util.mock_spec('nonexistent') as first, \ util.mock_spec(mod_name) as second: first.find_spec = lambda self, fullname, path=None, parent=None: None with util.import_state(meta_path=[first, second]): self.assertIs(self.__import__(mod_name), second.modules[mod_name])
def test_star_in_all(self): with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]): importer['pkg'].__all__ = ['*'] module = self.__import__('pkg', fromlist=['*']) self.assertEqual(module.__name__, 'pkg') self.assertFalse(hasattr(module, '*'))
def test_nonexistent_object(self): # [bad object] with util.mock_spec('module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('module', fromlist=['non_existent']) self.assertEqual(module.__name__, 'module') self.assertFalse(hasattr(module, 'non_existent'))
def test_empty_list(self): # An empty list should not count as asking for sys.path. module = 'module' path = '<test path>' importer = util.mock_spec(module) with util.import_state(path_importer_cache={path: importer}, path=[path]): self.assertIsNone(self.find('module', []))
def test_invalid_type_in_all(self): with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]), \ warnings.catch_warnings(): warnings.simplefilter('error', BytesWarning) importer['pkg'].__all__ = [b'attr'] with self.assertRaisesRegex(TypeError, r'\bpkg\.__all__\b'): self.__import__('pkg', fromlist=['*'])
def test_module_from_package(self): # [module] with util.mock_spec('pkg.__init__', 'pkg.module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg', fromlist=['module']) self.assertEqual(module.__name__, 'pkg') self.assertTrue(hasattr(module, 'module')) self.assertEqual(module.module.__name__, 'pkg.module')
def test_deep_relative_package_import(self): modules = ['a.__init__', 'a.b.__init__', 'a.c'] with test_util.mock_spec(*modules) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module('a') self.init.import_module('a.b') module = self.init.import_module('..c', 'a.b') self.assertEqual(module.__name__, 'a.c')
def test_path(self): # Test that 'path' is used when set. # Implicitly tests that sys.path_importer_cache is used. module = '<test module>' path = '<test path>' importer = util.mock_spec(module) with util.import_state(path_importer_cache={path: importer}): found = self.find(module, [path]) self.check_found(found, importer)
def basic_star_test(self, fromlist=['*']): # [using *] with util.mock_spec('pkg.__init__', 'pkg.module') as mock: with util.import_state(meta_path=[mock]): mock['pkg'].__all__ = ['module'] module = self.__import__('pkg', fromlist=fromlist) self.assertEqual(module.__name__, 'pkg') self.assertTrue(hasattr(module, 'module')) self.assertEqual(module.module.__name__, 'pkg.module')
def test_absolute_package_import(self): # Test importing a module from a package with an absolute name. pkg_name = 'pkg' pkg_long_name = '{0}.__init__'.format(pkg_name) name = '{0}.mod'.format(pkg_name) with test_util.mock_spec(pkg_long_name, name) as mock: with test_util.import_state(meta_path=[mock]): module = self.init.import_module(name) self.assertEqual(module.__name__, name)
def test_path_importer_cache_empty_string(self): # The empty string should create a finder using the cwd. path = '' module = '<test module>' importer = util.mock_spec(module) hook = util.mock_path_hook(os.getcwd(), importer=importer) with util.import_state(path=[path], path_hooks=[hook]): found = self.find(module) self.check_found(found, importer) self.assertIn(os.getcwd(), sys.path_importer_cache)
def test_star_with_others(self): # [using * with others] context = util.mock_spec('pkg.__init__', 'pkg.module1', 'pkg.module2') with context as mock: with util.import_state(meta_path=[mock]): mock['pkg'].__all__ = ['module1'] module = self.__import__('pkg', fromlist=['module2', '*']) self.assertEqual(module.__name__, 'pkg') self.assertTrue(hasattr(module, 'module1')) self.assertTrue(hasattr(module, 'module2')) self.assertEqual(module.module1.__name__, 'pkg.module1') self.assertEqual(module.module2.__name__, 'pkg.module2')
def test_path_hooks(self): # Test that sys.path_hooks is used. # Test that sys.path_importer_cache is set. module = '<test module>' path = '<test path>' importer = util.mock_spec(module) hook = util.mock_path_hook(path, importer=importer) with util.import_state(path_hooks=[hook]): found = self.find(module, [path]) self.check_found(found, importer) self.assertIn(path, sys.path_importer_cache) self.assertIs(sys.path_importer_cache[path], importer)
def test_shallow_relative_package_import(self): # Test importing a module from a package through a relative import. pkg_name = 'pkg' pkg_long_name = '{0}.__init__'.format(pkg_name) module_name = 'mod' absolute_name = '{0}.{1}'.format(pkg_name, module_name) relative_name = '.{0}'.format(module_name) with test_util.mock_spec(pkg_long_name, absolute_name) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module(pkg_name) module = self.init.import_module(relative_name, pkg_name) self.assertEqual(module.__name__, absolute_name)
def test_module_from_package_triggers_ModuleNotFoundError(self): # If a submodule causes an ModuleNotFoundError because it tries # to import a module which doesn't exist, that should let the # ModuleNotFoundError propagate. def module_code(): import i_do_not_exist with util.mock_spec('pkg.__init__', 'pkg.mod', module_code={'pkg.mod': module_code}) as importer: with util.import_state(meta_path=[importer]): with self.assertRaises(ModuleNotFoundError) as exc: self.__import__('pkg', fromlist=['mod']) self.assertEqual('i_do_not_exist', exc.exception.name)
def relative_import_test(self, create, globals_, callback): """Abstract out boilerplace for setting up for an import test.""" uncache_names = [] for name in create: if not name.endswith('.__init__'): uncache_names.append(name) else: uncache_names.append(name[:-len('.__init__')]) with util.mock_spec(*create) as importer: with util.import_state(meta_path=[importer]): with warnings.catch_warnings(): warnings.simplefilter("ignore") for global_ in globals_: with util.uncache(*uncache_names): callback(global_)
def test_module_replaced(self): def code(): import sys module = type(sys)('top_level') module.spam = 3 sys.modules['top_level'] = module mock = test_util.mock_spec('top_level', module_code={'top_level': code}) with mock: with test_util.import_state(meta_path=[mock]): module = self.init.import_module('top_level') reloaded = self.init.reload(module) actual = sys.modules['top_level'] self.assertEqual(actual.spam, 3) self.assertEqual(reloaded.spam, 3)
def test_loaded_once(self): # Issue #13591: Modules should only be loaded once when # initializing the parent package attempts to import the # module currently being imported. b_load_count = 0 def load_a(): self.init.import_module('a.b') def load_b(): nonlocal b_load_count b_load_count += 1 code = {'a': load_a, 'a.b': load_b} modules = ['a.__init__', 'a.b'] with test_util.mock_spec(*modules, module_code=code) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module('a.b') self.assertEqual(b_load_count, 1)
def test_module_not_package_but_side_effects(self): # If a module injects something into sys.modules as a side-effect, then # pick up on that fact. name = 'mod' subname = name + '.b' def module_injection(): sys.modules[subname] = 'total bunk' mock_spec = util.mock_spec('mod', module_code={'mod': module_injection}) with mock_spec as mock: with util.import_state(meta_path=[mock]): try: submodule = self.__import__(subname) finally: import_helper.unload(subname)
def test_raising_parent_after_importing_child(self): def __init__(): import pkg.module 1 / 0 mock = util.mock_spec('pkg.__init__', 'pkg.module', module_code={'pkg': __init__}) with mock: with util.import_state(meta_path=[mock]): with self.assertRaises(ZeroDivisionError): self.__import__('pkg') self.assertNotIn('pkg', sys.modules) self.assertIn('pkg.module', sys.modules) with self.assertRaises(ZeroDivisionError): self.__import__('pkg.module') self.assertNotIn('pkg', sys.modules) self.assertIn('pkg.module', sys.modules)
def test_raising_parent_after_relative_importing_child(self): def __init__(): from . import module 1 / 0 mock = util.mock_spec('pkg.__init__', 'pkg.module', module_code={'pkg': __init__}) with mock: with util.import_state(meta_path=[mock]): with self.assertRaises((ZeroDivisionError, ImportError)): # This raises ImportError on the "from . import module" # line, not sure why. self.__import__('pkg') self.assertNotIn('pkg', sys.modules) with self.assertRaises((ZeroDivisionError, ImportError)): self.__import__('pkg.module') self.assertNotIn('pkg', sys.modules)
def test_first_called(self): # [first called] mod = 'top_level' with util.mock_spec(mod) as first, util.mock_spec(mod) as second: with util.import_state(meta_path=[first, second]): self.assertIs(self.__import__(mod), first.modules[mod])
def test_module_import(self): # Test importing a top-level module. with test_util.mock_spec('top_level') as mock: with test_util.import_state(meta_path=[mock]): module = self.init.import_module('top_level') self.assertEqual(module.__name__, 'top_level')
def test_empty_string(self): with util.mock_spec('pkg.__init__', 'pkg.mod') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg.mod', fromlist=['']) self.assertEqual(module.__name__, 'pkg.mod')
def test_nonexistent_from_package(self): with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg', fromlist=['non_existent']) self.assertEqual(module.__name__, 'pkg') self.assertFalse(hasattr(module, 'non_existent'))
def test_import_parent(self): with util.mock_spec('pkg.__init__', 'pkg.module') as mock: with util.import_state(meta_path=[mock]): module = self.__import__('pkg.module') self.assertIn('pkg', sys.modules)
def test_bad_parent(self): with util.mock_spec('pkg.module') as mock: with util.import_state(meta_path=[mock]): with self.assertRaises(ImportError) as cm: self.__import__('pkg.module') self.assertEqual(cm.exception.name, 'pkg')
def test_object(self): # [object case] with util.mock_spec('module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('module', fromlist=['attr']) self.assertEqual(module.__name__, 'module')
def test_return_from_from_import(self): # [from return] with util.mock_spec('pkg.__init__', 'pkg.module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg.module', fromlist=['attr']) self.assertEqual(module.__name__, 'pkg.module')