Example #1
0
def create(evaluator, obj, parent_context=None, *args):
    tree_node, path = find_syntax_node_name(evaluator, obj)

    compiled_object = compiled.create(
        evaluator, obj, parent_context=parent_context.compiled_object)
    if tree_node is None:
        return compiled_object

    module_node = tree_node.get_root_node()
    if parent_context.tree_node.get_root_node() == module_node:
        module_context = parent_context.get_root_context()
    else:
        from jedi.evaluate.representation import ModuleContext
        module_context = ModuleContext(evaluator, module_node, path=path)
        name = compiled_object.get_root_context().py__name__()
        imports.add_module(evaluator, name, module_context)

    tree_context = module_context.create_context(
        tree_node,
        node_is_context=True,
        node_is_object=True
    )

    return MixedObject(
        evaluator,
        parent_context,
        compiled_object,
        tree_context=tree_context
    )
Example #2
0
def create(evaluator, obj, parent_context=None, *args):
    tree_node, path = find_syntax_node_name(evaluator, obj)

    compiled_object = compiled.create(
        evaluator, obj, parent_context=parent_context.compiled_object)
    if tree_node is None:
        return compiled_object

    module_node = tree_node.get_root_node()
    if parent_context.tree_node.get_root_node() == module_node:
        module_context = parent_context.get_root_context()
    else:
        from jedi.evaluate.representation import ModuleContext
        module_context = ModuleContext(evaluator, module_node, path=path)
        name = compiled_object.get_root_context().py__name__()
        imports.add_module(evaluator, name, module_context)

    tree_context = module_context.create_context(tree_node,
                                                 node_is_context=True,
                                                 node_is_object=True)

    return MixedObject(evaluator,
                       parent_context,
                       compiled_object,
                       tree_context=tree_context)
Example #3
0
def _create(evaluator, obj, parent_context=None, *args):
    tree_node, path = find_syntax_node_name(evaluator, obj)

    compiled_object = compiled.create(
        evaluator, obj, parent_context=parent_context.compiled_object)
    if tree_node is None:
        return compiled_object

    module_node = tree_node.get_root_node()
    if parent_context.tree_node.get_root_node() == module_node:
        module_context = parent_context.get_root_context()
    else:
        from jedi.evaluate.representation import ModuleContext
        module_context = ModuleContext(evaluator, module_node, path=path)
        # TODO this __name__ is probably wrong.
        name = compiled_object.get_root_context().py__name__()
        imports.add_module(evaluator, name, module_context)

    tree_context = module_context.create_context(tree_node,
                                                 node_is_context=True,
                                                 node_is_object=True)
    if tree_node.type == 'classdef':
        if not inspect.isclass(obj):
            # Is an instance, not a class.
            tree_context, = tree_context.execute_evaluated()

    return MixedObject(evaluator,
                       parent_context,
                       compiled_object,
                       tree_context=tree_context)
Example #4
0
    def __init__(self, evaluator, tree_module, namespaces, path):
        self.evaluator = evaluator
        self._namespaces = namespaces

        self._namespace_objects = [NamespaceObject(n) for n in namespaces]
        self._module_context = ModuleContext(evaluator, tree_module, path=path)
        self.tree_node = tree_module
Example #5
0
def _create(evaluator, obj, parent_context=None, *args):
    tree_node, path = find_syntax_node_name(evaluator, obj)

    compiled_object = compiled.create(
        evaluator, obj, parent_context=parent_context.compiled_object)
    if tree_node is None:
        return compiled_object

    module_node = tree_node.get_root_node()
    if parent_context.tree_node.get_root_node() == module_node:
        module_context = parent_context.get_root_context()
    else:
        from jedi.evaluate.representation import ModuleContext
        module_context = ModuleContext(evaluator, module_node, path=path)
        # TODO this __name__ is probably wrong.
        name = compiled_object.get_root_context().py__name__()
        imports.add_module(evaluator, name, module_context)

    tree_context = module_context.create_context(
        tree_node,
        node_is_context=True,
        node_is_object=True
    )
    if tree_node.type == 'classdef':
        if not inspect.isclass(obj):
            # Is an instance, not a class.
            tree_context, = tree_context.execute_evaluated()

    return MixedObject(
        evaluator,
        parent_context,
        compiled_object,
        tree_context=tree_context
    )
Example #6
0
    def __init__(self, evaluator, tree_module, namespaces):
        self.evaluator = evaluator
        self._namespaces = namespaces

        self._namespace_objects = [
            type('jedi_namespace', (), n) for n in namespaces
        ]
        self._module_context = ModuleContext(evaluator, tree_module)
        self.tree_node = tree_module
Example #7
0
def py__getitem__(context, typ, node):
    if not typ.get_root_context().name.string_name == "typing":
        return None
    # we assume that any class using [] in a module called
    # "typing" with a name for which we have a replacement
    # should be replaced by that class. This is not 100%
    # airtight but I don't have a better idea to check that it's
    # actually the PEP-0484 typing module and not some other
    if node.type == "subscriptlist":
        nodes = node.children[::2]  # skip the commas
    else:
        nodes = [node]
    del node

    nodes = [_fix_forward_reference(context, node) for node in nodes]
    type_name = typ.name.string_name

    # hacked in Union and Optional, since it's hard to do nicely in parsed code
    if type_name in ("Union", '_Union'):
        # In Python 3.6 it's still called typing.Union but it's an instance
        # called _Union.
        return unite(context.eval_node(node) for node in nodes)
    if type_name in ("Optional", '_Optional'):
        # Here we have the same issue like in Union. Therefore we also need to
        # check for the instance typing._Optional (Python 3.6).
        return context.eval_node(nodes[0])

    from jedi.evaluate.representation import ModuleContext
    typing = ModuleContext(
        context.evaluator,
        module_node=_get_typing_replacement_module(context.evaluator.latest_grammar),
        path=None
    )
    factories = typing.py__getattribute__("factory")
    assert len(factories) == 1
    factory = list(factories)[0]
    assert factory
    function_body_nodes = factory.tree_node.children[4].children
    valid_classnames = set(child.name.value
                           for child in function_body_nodes
                           if isinstance(child, tree.Class))
    if type_name not in valid_classnames:
        return None
    compiled_classname = compiled.create(context.evaluator, type_name)

    from jedi.evaluate.iterable import FakeSequence
    args = FakeSequence(
        context.evaluator,
        "tuple",
        [LazyTreeContext(context, n) for n in nodes]
    )

    result = factory.execute_evaluated(compiled_classname, args)
    return result
Example #8
0
def py__getitem__(context, typ, node):
    if not typ.get_root_context().name.string_name == "typing":
        return None
    # we assume that any class using [] in a module called
    # "typing" with a name for which we have a replacement
    # should be replaced by that class. This is not 100%
    # airtight but I don't have a better idea to check that it's
    # actually the PEP-0484 typing module and not some other
    if node.type == "subscriptlist":
        nodes = node.children[::2]  # skip the commas
    else:
        nodes = [node]
    del node

    nodes = [_fix_forward_reference(context, node) for node in nodes]
    type_name = typ.name.string_name

    # hacked in Union and Optional, since it's hard to do nicely in parsed code
    if type_name in ("Union", '_Union'):
        # In Python 3.6 it's still called typing.Union but it's an instance
        # called _Union.
        return unite(context.eval_node(node) for node in nodes)
    if type_name in ("Optional", '_Optional'):
        # Here we have the same issue like in Union. Therefore we also need to
        # check for the instance typing._Optional (Python 3.6).
        return context.eval_node(nodes[0])

    from jedi.evaluate.representation import ModuleContext
    typing = ModuleContext(
        context.evaluator,
        module_node=_get_typing_replacement_module(),
        path=None
    )
    factories = typing.py__getattribute__("factory")
    assert len(factories) == 1
    factory = list(factories)[0]
    assert factory
    function_body_nodes = factory.tree_node.children[4].children
    valid_classnames = set(child.name.value
                           for child in function_body_nodes
                           if isinstance(child, tree.Class))
    if type_name not in valid_classnames:
        return None
    compiled_classname = compiled.create(context.evaluator, type_name)

    from jedi.evaluate.iterable import FakeSequence
    args = FakeSequence(
        context.evaluator,
        "tuple",
        [LazyTreeContext(context, n) for n in nodes]
    )

    result = factory.execute_evaluated(compiled_classname, args)
    return result
Example #9
0
class MixedModuleContext(Context):
    resets_positions = True
    type = 'mixed_module'

    def __init__(self, evaluator, tree_module, namespaces, path):
        self.evaluator = evaluator
        self._namespaces = namespaces

        self._namespace_objects = [NamespaceObject(n) for n in namespaces]
        self._module_context = ModuleContext(evaluator, tree_module, path=path)
        self.tree_node = tree_module

    def get_node(self):
        return self.tree_node

    def get_filters(self, *args, **kwargs):
        for filter in self._module_context.get_filters(*args, **kwargs):
            yield filter

        for namespace_obj in self._namespace_objects:
            compiled_object = compiled.create(self.evaluator, namespace_obj)
            mixed_object = mixed.MixedObject(
                self.evaluator,
                parent_context=self,
                compiled_object=compiled_object,
                tree_context=self._module_context
            )
            for filter in mixed_object.get_filters(*args, **kwargs):
                yield filter

    def __getattr__(self, name):
        return getattr(self._module_context, name)
Example #10
0
    def __init__(self, evaluator, tree_module, namespaces, path):
        self.evaluator = evaluator
        self._namespaces = namespaces

        self._namespace_objects = [NamespaceObject(n) for n in namespaces]
        self._module_context = ModuleContext(evaluator, tree_module, path=path)
        self.tree_node = tree_module
Example #11
0
class MixedModuleContext(Context):
    resets_positions = True
    type = 'mixed_module'

    def __init__(self, evaluator, tree_module, namespaces, path):
        self.evaluator = evaluator
        self._namespaces = namespaces

        self._namespace_objects = [NamespaceObject(n) for n in namespaces]
        self._module_context = ModuleContext(evaluator, tree_module, path=path)
        self.tree_node = tree_module

    def get_node(self):
        return self.tree_node

    def get_filters(self, *args, **kwargs):
        for filter in self._module_context.get_filters(*args, **kwargs):
            yield filter

        for namespace_obj in self._namespace_objects:
            compiled_object = compiled.create(self.evaluator, namespace_obj)
            mixed_object = mixed.MixedObject(self.evaluator,
                                             parent_context=self,
                                             compiled_object=compiled_object,
                                             tree_context=self._module_context)
            for filter in mixed_object.get_filters(*args, **kwargs):
                yield filter

    def __getattr__(self, name):
        return getattr(self._module_context, name)
Example #12
0
def _load_module(evaluator, path=None, source=None, sys_path=None, parent_module=None):
    def load(source):
        dotted_path = path and compiled.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:
            if source is None:
                with open(path, 'rb') as f:
                    source = f.read()
        else:
            return compiled.load_module(evaluator, path)
        p = path
        p = FastParser(evaluator.grammar, source_to_unicode(source), p)
        save_parser(path, p)
        return p.module

    if sys_path is None:
        sys_path = evaluator.sys_path

    cached = load_parser(path)
    module_node = load(source) if cached is None else cached.module
    if isinstance(module_node, compiled.CompiledObject):
        return module_node

    from jedi.evaluate.representation import ModuleContext
    return ModuleContext(evaluator, module_node)
Example #13
0
    def __init__(self, evaluator, parent_context, compiled_object, tree_name):
        self.evaluator = evaluator
        self.compiled_object = compiled_object
        self.obj = compiled_object.obj
        self._tree_name = tree_name
        name_module = tree_name.get_root_node()
        if parent_context.tree_node.get_root_node() != name_module:
            from jedi.evaluate.representation import ModuleContext
            module_context = ModuleContext(evaluator, name_module)
            name = compiled_object.get_root_context().py__name__()
            imports.add_module(evaluator, name, module_context)
        else:
            module_context = parent_context.get_root_context()

        self._context = module_context.create_context(tree_name.parent,
                                                      node_is_context=True,
                                                      node_is_object=True)
Example #14
0
    def __init__(self, evaluator, parent_context, compiled_object, tree_name):
        self.evaluator = evaluator
        self.compiled_object = compiled_object
        self.obj = compiled_object.obj
        self._tree_name = tree_name
        name_module = tree_name.get_root_node()
        if parent_context.tree_node.get_root_node() != name_module:
            from jedi.evaluate.representation import ModuleContext
            module_context = ModuleContext(evaluator, name_module)
            name = compiled_object.get_root_context().py__name__()
            imports.add_module(evaluator, name, module_context)
        else:
            module_context = parent_context.get_root_context()

        self._context = module_context.create_context(
            tree_name.parent,
            node_is_context=True,
            node_is_object=True
        )
Example #15
0
def test_sys_path_with_modifications():
    code = dedent("""
        import os
    """)

    path = os.path.abspath(os.path.join(os.curdir, 'module_name.py'))
    grammar = load_grammar()
    module_node = parse(code, path=path)
    module_context = ModuleContext(Evaluator(grammar), module_node, path=path)
    paths = sys_path_with_modifications(module_context.evaluator,
                                        module_context)
    assert '/tmp/.buildout/eggs/important_package.egg' in paths
Example #16
0
def _load_module(evaluator, path=None, code=None, sys_path=None, parent_module=None):
    if sys_path is None:
        sys_path = evaluator.sys_path

    dotted_path = path and compiled.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 = parse(code=code, path=path, cache=True, diff_cache=True)

        from jedi.evaluate.representation import ModuleContext
        return ModuleContext(evaluator, module_node, path=path)
    else:
        return compiled.load_module(evaluator, path)
Example #17
0
def _get_paths_from_buildout_script(evaluator, buildout_script_path):
    try:
        module_node = parse(
            path=buildout_script_path,
            grammar=evaluator.grammar,
            cache=True
        )
    except IOError:
        debug.warning('Error trying to read buildout_script: %s', buildout_script_path)
        return

    from jedi.evaluate.representation import ModuleContext
    for path in _check_module(ModuleContext(evaluator, module_node, buildout_script_path)):
        yield path
Example #18
0
def _get_paths_from_buildout_script(evaluator, buildout_script):
    def load(buildout_script):
        try:
            with open(buildout_script, 'rb') as f:
                source = common.source_to_unicode(f.read())
        except IOError:
            debug.dbg('Error trying to read buildout_script: %s',
                      buildout_script)
            return

        p = ParserWithRecovery(evaluator.grammar, source, buildout_script)
        save_parser(buildout_script, p)
        return p.module

    cached = load_parser(buildout_script)
    module_node = cached and cached.module or load(buildout_script)
    if module_node is None:
        return

    from jedi.evaluate.representation import ModuleContext
    for path in _check_module(ModuleContext(evaluator, module_node)):
        yield path
Example #19
0
def check_module_test(code):
    grammar = load_grammar()
    module_context = ModuleContext(Evaluator(grammar), parse(code), path=None)
    return _check_module(module_context)
Example #20
0
def check_module_test(code):
    grammar = load_grammar()
    p = ParserWithRecovery(grammar, code)
    module_context = ModuleContext(Evaluator(grammar), p.module)
    return _check_module(module_context)