Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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, C_EXTENSION}:
            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:
            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)