def _check_lazy_loading(modname): names = modname.split(".") _check_not_loaded(modname) basename = lazy_import.module_basename(modname) mod = lazy_import.lazy_module(modname, level="leaf") assert sys.modules[modname] is mod assert str(mod) == "Lazily-loaded module " + modname base = lazy_import.lazy_module(modname, level="base") assert sys.modules[basename] is base assert str(base) == "Lazily-loaded module " + basename # A subtle bug only rears its head if an actual import statement is done. # Can only test that for the module names we know of # (meaning, no random ones) if modname in NAMES_EXISTING: if modname == 'sched': import sched as newmod elif modname == 'distutils.core': import distutils.core as newmod assert str(newmod) == "Lazily-loaded module " + modname # Check that all submodules are in and that submodule access works curr_name = basename curr_mod = base for submod in names[1:]: curr_name += "." + submod curr_mod = getattr(curr_mod, submod) assert curr_name in sys.modules assert str(curr_mod) == "Lazily-loaded module " + curr_name assert isinstance(curr_mod, lazy_import.LazyModule) _check_reuse(curr_name) # Check that missing modules raise errors if modname in _GENERATED_NAMES: _check_module_missing(curr_mod)
def test_opts(nsub, lazy_opts): modname = random_modname(nsub) level, modclass, errors = lazy_opts mod = lazy_import.lazy_module(modname, error_strings=errors, lazy_mod_class=modclass, level=level) names = modname.split(".") basename = lazy_import.module_basename(modname) if level == "leaf": assert sys.modules[modname] is mod err_modname = modname elif level == "base": assert sys.modules[basename] is mod err_modname = basename else: raise ValueError("Unexpected value {} for 'level'".format(level)) # Test the exception err msg assert isinstance(mod, modclass) if errors is None: expected_err = lazy_import._MSG.format(module=err_modname, caller=__name__, install_name=basename) else: expected_err = errors["msg"].format(**errors) _check_module_missing(mod, msg=expected_err)
def test_callable_missing_module(nsub, errors, cnames, fn): modname = random_modname(nsub) basename = lazy_import.module_basename(modname) if isinstance(cnames, six.string_types): lazys = (fn(modname+"."+cnames, error_strings=errors),) cnames = (cnames, ) else: lazys = fn(modname, *cnames, error_strings=errors) for lazy in lazys: if errors is None: expected_err = lazy_import._MSG.format(module=modname, caller=__name__, install_name=basename) else: expected_err = errors["msg"].format(**errors) _check_module_missing(lazy, msg=expected_err, call=True)
def test_callable_missing(modname, errors, cnames, fn): _check_not_loaded(modname) basename = lazy_import.module_basename(modname) if isinstance(cnames, six.string_types): lazys = (fn(modname+"."+cnames, error_strings=errors),) cnames = (cnames, ) else: lazys = fn(modname, *cnames, error_strings=errors) for lazy, cname in zip(lazys, cnames): if errors is None: expected_err = lazy_import._MSG_CALLABLE.format(module=modname, caller=__name__, install_name=basename, callable=cname) else: errors['callable'] = cname expected_err = errors["msg_callable"].format(**errors) _check_callable_missing(lazy, msg=expected_err)