def _get_module(self):
     module = er.ModuleContext(self._evaluator, self._get_module_node(),
                               self.path)
     if self.path is not None:
         name = dotted_path_in_sys_path(self._evaluator.sys_path, self.path)
         if name is not None:
             imports.add_module(self._evaluator, name, module)
     return module
Esempio n. 2
0
 def _get_module(self):
     module = er.ModuleContext(
         self._evaluator,
         self._get_module_node(),
         self.path
     )
     imports.add_module(self._evaluator, module.name.string_name, module)
     return module
Esempio n. 3
0
 def check_python_file(path):
     try:
         parser_cache_item = parser_cache[path]
     except KeyError:
         try:
             return check_fs(path)
         except IOError:
             return None
     else:
         return er.ModuleContext(evaluator, parser_cache_item.parser.module)
Esempio n. 4
0
 def check_python_file(path):
     try:
         node_cache_item = parser_cache[path]
     except KeyError:
         try:
             return check_fs(path)
         except IOError:
             return None
     else:
         module_node = node_cache_item.node
         return er.ModuleContext(evaluator, module_node, path=path)
Esempio n. 5
0
 def check_python_file(path):
     try:
         # TODO I don't think we should use the cache here?!
         node_cache_item = parser_cache[evaluator.grammar._hashed][path]
     except KeyError:
         try:
             return check_fs(path)
         except IOError:
             return None
     else:
         module_node = node_cache_item.node
         return er.ModuleContext(evaluator, module_node, path=path)
def collections_namedtuple(evaluator, obj, arguments):
    """
    Implementation of the namedtuple function.

    This has to be done by processing the namedtuple class template and
    evaluating the result.

    .. note:: |jedi| only supports namedtuples on Python >2.6.

    """
    # Namedtuples are not supported on Python 2.6
    if not hasattr(collections, '_class_template'):
        return set()

    # Process arguments
    # TODO here we only use one of the types, we should use all.
    name = list(_follow_param(evaluator, arguments, 0))[0].obj
    _fields = list(_follow_param(evaluator, arguments, 1))[0]
    if isinstance(_fields, compiled.CompiledObject):
        fields = _fields.obj.replace(',', ' ').split()
    elif isinstance(_fields, iterable.AbstractSequence):
        fields = [
            v.obj for lazy_context in _fields.py__iter__()
            for v in lazy_context.infer() if hasattr(v, 'obj')
        ]
    else:
        return set()

    base = collections._class_template
    base += _NAMEDTUPLE_INIT
    # Build source
    source = base.format(
        typename=name,
        field_names=tuple(fields),
        num_fields=len(fields),
        arg_list=repr(tuple(fields)).replace("'", "")[1:-1],
        repr_fmt=', '.join(
            collections._repr_template.format(name=name) for name in fields),
        field_defs='\n'.join(
            collections._field_template.format(index=index, name=name)
            for index, name in enumerate(fields)))

    # Parse source
    module = evaluator.grammar.parse(source)
    generated_class = next(module.iter_classdefs())
    parent_context = er.ModuleContext(evaluator, module, '')
    return set([er.ClassContext(evaluator, generated_class, parent_context)])