예제 #1
0
def load_source(name, pathname, file=None):
    loader = _LoadSourceCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    if name in sys.modules:
        module = _exec(spec, sys.modules[name])
    else:
        module = _load(spec)
    module.__loader__ = machinery.SourceFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #2
0
def load_source(name, pathname, file=None):
    loader = _LoadSourceCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    module = _exec(spec,
                   sys.modules[name]) if name in sys.modules else _load(spec)
    # To allow reloading to potentially work, use a non-hacked loader which
    # won't rely on a now-closed file object.
    module.__loader__ = machinery.SourceFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #3
0
def load_compiled(name, pathname, file=None):
    """**DEPRECATED**"""
    loader = _LoadCompiledCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    if name in sys.modules:
        module = _exec(spec, sys.modules[name])
    else:
        module = _load(spec)
    module.__loader__ = SourcelessFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #4
0
def load_compiled(name, pathname, file=None):
    """**DEPRECATED**"""
    loader = _LoadCompiledCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    module = _exec(spec,
                   sys.modules[name]) if name in sys.modules else _load(spec)
    # To allow reloading to potentially work, use a non-hacked loader which
    # won't rely on a now-closed file object.
    module.__loader__ = SourcelessFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #5
0
파일: imp.py 프로젝트: Martiusweb/cpython
def load_source(name, pathname, file=None):
    loader = _LoadSourceCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    if name in sys.modules:
        module = _exec(spec, sys.modules[name])
    else:
        module = _load(spec)
    # To allow reloading to potentially work, use a non-hacked loader which
    # won't rely on a now-closed file object.
    module.__loader__ = machinery.SourceFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #6
0
파일: imp.py 프로젝트: Martiusweb/cpython
def load_compiled(name, pathname, file=None):
    """**DEPRECATED**"""
    loader = _LoadCompiledCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    if name in sys.modules:
        module = _exec(spec, sys.modules[name])
    else:
        module = _load(spec)
    # To allow reloading to potentially work, use a non-hacked loader which
    # won't rely on a now-closed file object.
    module.__loader__ = SourcelessFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #7
0
def load_py_source(file_path, module_name=None):
    if os.path.isfile(file_path):
        if module_name is None:
            module_name = os.path.basename(file_path).split('.', 1)[0]
        spec = util.spec_from_file_location(module_name, file_path)
        if module_name in sys.modules:
            module = _exec(spec, sys.modules[module_name])
        else:
            module = _load(spec)
        # To allow reloading to potentially work, use a non-hacked loader which
        # won't rely on a now-closed file object.
        module.__loader__ = machinery.SourceFileLoader(module_name, file_path)
        module.__spec__.loader = module.__loader__
        return module
    return None
예제 #8
0
def load_package(name, path):
    """**DEPRECATED**"""
    if os.path.isdir(path):
        extensions = machinery.SOURCE_SUFFIXES[:] + machinery.BYTECODE_SUFFIXES[:]
        for extension in extensions:
            path = os.path.join(path, "__init__" + extension)
            if os.path.exists(path):
                break
        else:
            raise ValueError("{!r} is not a package".format(path))
    spec = util.spec_from_file_location(name, path, submodule_search_locations=[])
    if name in sys.modules:
        return _exec(spec, sys.modules[name])
    else:
        return _load(spec)
예제 #9
0
파일: imp.py 프로젝트: Martiusweb/cpython
def load_package(name, path):
    """**DEPRECATED**"""
    if os.path.isdir(path):
        extensions = (machinery.SOURCE_SUFFIXES[:] +
                      machinery.BYTECODE_SUFFIXES[:])
        for extension in extensions:
            path = os.path.join(path, '__init__'+extension)
            if os.path.exists(path):
                break
        else:
            raise ValueError('{!r} is not a package'.format(path))
    spec = util.spec_from_file_location(name, path,
                                        submodule_search_locations=[])
    if name in sys.modules:
        return _exec(spec, sys.modules[name])
    else:
        return _load(spec)
예제 #10
0
파일: imp.py 프로젝트: zarecor60/OctoPrint
def load_package(name, path):
    if os.path.isdir(path):
        extensions = (machinery.SOURCE_SUFFIXES[:] +
                      machinery.BYTECODE_SUFFIXES[:])
        for extension in extensions:
            init_path = os.path.join(path, '__init__' + extension)
            if os.path.exists(init_path):
                path = init_path
                break
        else:
            raise ValueError('{!r} is not a package'.format(path))
    spec = util.spec_from_file_location(name, path,
                                        submodule_search_locations=[])
    if name in sys.modules:
        return _exec(spec, sys.modules[name])
    else:
        return _load(spec)
예제 #11
0
파일: imp.py 프로젝트: SeanKeltsch/nerfball
def load_source(name, pathname, file=None):
    print(("imp - load_source name={} path={} file={}").format(
        name, pathname, file))

    if os.getenv("NERF_IMP_LOAD_SOURCE", "1") == "1":
        raise Exception("IMP_LOAD_SOURCE - NERFED")

    loader = _LoadSourceCompatibility(name, pathname, file)
    spec = util.spec_from_file_location(name, pathname, loader=loader)
    if name in sys.modules:
        module = _exec(spec, sys.modules[name])
    else:
        module = _load(spec)
    # To allow reloading to potentially work, use a non-hacked loader which
    # won't rely on a now-closed file object.
    module.__loader__ = machinery.SourceFileLoader(name, pathname)
    module.__spec__.loader = module.__loader__
    return module
예제 #12
0
def reload(module: ModuleType,
           additionalSearchDict: Dict[str, ModuleType] = None):
    """
	Reload the module and return it.

	The module must have been successfully imported before.

	:param module: The module to reload
	:param additionalSearchDict: An additional search path to use alongside sys.modules
	:return: The module
	"""
    if not module or not isinstance(module, ModuleType):
        raise TypeError("module argument must be a ModuleType instance")
    try:
        name = module.__spec__.name
    except AttributeError:
        name = module.__name__

    if additionalSearchDict is None:
        additionalSearchDict = {}

    useCustom = module in additionalSearchDict.values()

    if useCustom:
        if additionalSearchDict.get(name) is not module:
            msg = "module {} not found in sys.modules or in additional search dict"
            raise ImportError(msg.format(name), name=name)
    else:
        if sys.modules.get(name) is not module:
            msg = "module {} not found in sys.modules or in additional search dict"
            raise ImportError(msg.format(name), name=name)

    # remove all events listeners of this module
    EventSystem.INSTANCE.removeListeners(module.__name__)

    if name in _RELOADING:
        return _RELOADING[name]
    _RELOADING[name] = module
    try:
        parent_name = name.rpartition('.')[0]
        if parent_name:
            try:
                parent = additionalSearchDict[parent_name]
            except KeyError:
                try:
                    parent = sys.modules[parent_name]
                except KeyError:
                    msg = "parent {!r} not in sys.modules nor in additional search dict"
                    raise ImportError(msg.format(parent_name),
                                      name=parent_name) from None
                else:
                    pkgpath = parent.__path__
            else:
                pkgpath = parent.__path__
        else:
            pkgpath = None
        target = module
        spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
        if spec is None:
            raise ModuleNotFoundError(
                f"spec not found for the module {name!r}", name=name)
        _bootstrap._exec(spec, module)
        # The module may have replaced itself in sys.modules!
        return additionalSearchDict[name] if useCustom else sys.modules[name]
    finally:
        try:
            del _RELOADING[name]
        except KeyError:
            pass