Beispiel #1
0
def load_module(evaluator, path=None, name=None, sys_path=None):
    if sys_path is None:
        sys_path = list(evaluator.get_sys_path())
    if path is not None:
        dotted_path = dotted_from_fs_path(path, sys_path=sys_path)
    else:
        dotted_path = name

    temp, sys.path = sys.path, sys_path
    try:
        __import__(dotted_path)
    except ImportError:
        # If a module is "corrupt" or not really a Python module or whatever.
        debug.warning('Module %s not importable in path %s.', dotted_path, path)
        return None
    except Exception:
        # Since __import__ pretty much makes code execution possible, just
        # catch any error here and print it.
        import traceback
        print_to_stderr("Cannot import:\n%s" % traceback.format_exc())
        return None
    finally:
        sys.path = temp

    # Just access the cache after import, because of #59 as well as the very
    # complicated import structure of Python.
    module = sys.modules[dotted_path]
    return create_access_path(evaluator, module)
Beispiel #2
0
def load_module(evaluator, path=None, name=None, sys_path=None):
    if sys_path is None:
        sys_path = list(evaluator.get_sys_path())
    if path is not None:
        dotted_path = dotted_from_fs_path(path, sys_path=sys_path)
    else:
        dotted_path = name

    temp, sys.path = sys.path, sys_path
    try:
        __import__(dotted_path)
    except ImportError:
        # If a module is "corrupt" or not really a Python module or whatever.
        debug.warning('Module %s not importable in path %s.', dotted_path,
                      path)
        return None
    except Exception:
        # Since __import__ pretty much makes code execution possible, just
        # catch any error here and print it.
        import traceback
        print_to_stderr("Cannot import:\n%s" % traceback.format_exc())
        return None
    finally:
        sys.path = temp

    # Just access the cache after import, because of #59 as well as the very
    # complicated import structure of Python.
    module = sys.modules[dotted_path]
    return create_access_path(evaluator, module)
Beispiel #3
0
def _load_module(evaluator,
                 path=None,
                 code=None,
                 sys_path=None,
                 module_name=None,
                 safe_module_name=False):
    try:
        return evaluator.module_cache.get(module_name)
    except KeyError:
        pass
    try:
        return evaluator.module_cache.get_from_path(path)
    except KeyError:
        pass

    if isinstance(path, ImplicitNSInfo):
        from jedi.evaluate.context.namespace import ImplicitNamespaceContext
        module = ImplicitNamespaceContext(
            evaluator,
            fullname=path.name,
            paths=path.paths,
        )
    else:
        if sys_path is None:
            sys_path = evaluator.get_sys_path()

        dotted_path = path and dotted_from_fs_path(path, sys_path)
        if path is not None and path.endswith(('.py', '.zip', '.egg')) \
                and dotted_path not in settings.auto_import_modules:

            module_node = evaluator.parse(code=code,
                                          path=path,
                                          cache=True,
                                          diff_cache=True,
                                          cache_path=settings.cache_directory)

            from jedi.evaluate.context import ModuleContext
            module = ModuleContext(
                evaluator,
                module_node,
                path=path,
                code_lines=get_cached_code_lines(evaluator.grammar, path),
            )
        else:
            module = compiled.load_module(evaluator,
                                          path=path,
                                          sys_path=sys_path)

    if module is not None and module_name is not None:
        add_module_to_cache(evaluator,
                            module_name,
                            module,
                            safe=safe_module_name)

    return module
Beispiel #4
0
def _load_module(evaluator, path=None, code=None, sys_path=None,
                 module_name=None, safe_module_name=False):
    try:
        return evaluator.module_cache.get(module_name)
    except KeyError:
        pass
    try:
        return evaluator.module_cache.get_from_path(path)
    except KeyError:
        pass

    if isinstance(path, ImplicitNSInfo):
        from jedi.evaluate.context.namespace import ImplicitNamespaceContext
        module = ImplicitNamespaceContext(
            evaluator,
            fullname=path.name,
            paths=path.paths,
        )
    else:
        if sys_path is None:
            sys_path = evaluator.get_sys_path()

        dotted_path = path and dotted_from_fs_path(path, sys_path)
        if path is not None and path.endswith(('.py', '.zip', '.egg')) \
                and dotted_path not in settings.auto_import_modules:

            module_node = evaluator.parse(
                code=code, path=path, cache=True, diff_cache=True,
                cache_path=settings.cache_directory)

            from jedi.evaluate.context import ModuleContext
            module = ModuleContext(
                evaluator, module_node,
                path=path,
                code_lines=get_cached_code_lines(evaluator.grammar, path),
            )
        else:
            module = compiled.load_module(evaluator, path=path, sys_path=sys_path)

    if module is not None and module_name is not None:
        add_module_to_cache(evaluator, module_name, module, safe=safe_module_name)

    return module
Beispiel #5
0
def _load_module(evaluator,
                 path=None,
                 code=None,
                 sys_path=None,
                 parent_module=None):
    if sys_path is None:
        sys_path = evaluator.get_sys_path()

    dotted_path = path and dotted_from_fs_path(path, sys_path)
    if path is not None and path.endswith(('.py', '.zip', '.egg')) \
            and dotted_path not in settings.auto_import_modules:

        module_node = evaluator.parse(code=code,
                                      path=path,
                                      cache=True,
                                      diff_cache=True,
                                      cache_path=settings.cache_directory)

        from jedi.evaluate.context import ModuleContext
        return ModuleContext(evaluator, module_node, path=path)
    else:
        return compiled.load_module(evaluator, path=path, sys_path=sys_path)