def load_module(name, file, filename, details): """**DEPRECATED** Load a module, given information returned by find_module(). The module name must include the full package name, if any. """ suffix, mode, type_ = details if mode and (not mode.startswith(('r', 'U')) or '+' in mode): raise ValueError('invalid file open mode {!r}'.format(mode)) elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: msg = 'file object required for import (type code {})'.format(type_) raise ValueError(msg) elif type_ == PY_SOURCE: return load_source(name, filename, file) elif type_ == PY_COMPILED: return load_compiled(name, filename, file) elif type_ == C_EXTENSION and load_dynamic is not None: if file is None: with open(filename, 'rb') as opened_file: return load_dynamic(name, filename, opened_file) else: return load_dynamic(name, filename, file) elif type_ == PKG_DIRECTORY: return load_package(name, filename) elif type_ == C_BUILTIN: return init_builtin(name) elif type_ == PY_FROZEN: return init_frozen(name) else: msg = "Don't know how to import {} (type code {})".format(name, type_) raise ImportError(msg, name=name)
def load_module(name, file, filename, details): (suffix, mode, type_) = details with warnings.catch_warnings(): warnings.simplefilter('ignore') if mode and (not mode.startswith(('r', 'U')) or '+' in mode): raise ValueError('invalid file open mode {!r}'.format(mode)) elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: msg = 'file object required for import (type code {})'.format( type_) raise ValueError(msg) else: if type_ == PY_SOURCE: return load_source(name, filename, file) if type_ == PY_COMPILED: return load_compiled(name, filename, file) if type_ == C_EXTENSION and load_dynamic is not None: if file is None: with open(filename, 'rb') as opened_file: return load_dynamic(name, filename, opened_file) else: return load_dynamic(name, filename, file) else: if type_ == PKG_DIRECTORY: return load_package(name, filename) if type_ == C_BUILTIN: return init_builtin(name) if type_ == PY_FROZEN: return init_frozen(name) msg = "Don't know how to import {} (type code {})".format( name, type_) raise ImportError(msg, name=name)
def load_module(name, file, filename, details): """**DEPRECATED** Load a module, given information returned by find_module(). The module name must include the full package name, if any. """ suffix, mode, type_ = details with warnings.catch_warnings(): warnings.simplefilter("ignore") if mode and (not mode.startswith(("r", "U")) or "+" in mode): raise ValueError("invalid file open mode {!r}".format(mode)) elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: msg = "file object required for import (type code {})".format(type_) raise ValueError(msg) elif type_ == PY_SOURCE: return load_source(name, filename, file) elif type_ == PY_COMPILED: return load_compiled(name, filename, file) elif type_ == PKG_DIRECTORY: return load_package(name, filename) elif type_ == C_BUILTIN: return init_builtin(name) elif type_ == PY_FROZEN: return init_frozen(name) else: msg = "Don't know how to import {} (type code {}".format(name, type_) raise ImportError(msg, name=name)
import _imp import time as import_time assert _imp.is_builtin("time") == True assert _imp.is_builtin("os") == False assert _imp.is_builtin("not existing module") == False assert _imp.is_frozen("__hello__") == True assert _imp.is_frozen("os") == False class FakeSpec: def __init__(self, name): self.name = name A = FakeSpec("time") imp_time = _imp.create_builtin(A) assert imp_time.sleep == import_time.sleep B = FakeSpec("not existing module") assert _imp.create_builtin(B) == None _imp.exec_builtin(imp_time) == 0 _imp.get_frozen_object("__hello__") hello = _imp.init_frozen("__hello__") assert hello.initialized == True