Exemplo n.º 1
0
 def _get_nested_import_name(self):
     """
     Generates an Import statement, that can be used to fake nested imports.
     """
     i = self._nested_import
     # This is not an existing Import statement. Therefore, set position to
     # 0 (0 is not a valid line number).
     zero = (0, 0)
     names = [unicode(name) for name in i.namespace_names[1:]]
     name = helpers.FakeName(names, self._nested_import)
     new = tree.Import(i._sub_module, zero, zero, name)
     new.parent = self._module
     debug.dbg('Generated a nested import: %s', new)
     return helpers.FakeName(str(i.namespace_names[1]), new)
Exemplo n.º 2
0
    def _get_names_dict(self, names_dict):
        builtin_methods = {}
        for cls in reversed(type(self).mro()):
            try:
                builtin_methods.update(cls.builtin_methods)
            except AttributeError:
                pass

        if not builtin_methods:
            return names_dict

        dct = {}
        for names in names_dict.values():
            for name in names:
                name_str = name.value
                try:
                    method = builtin_methods[name_str, self.type]
                except KeyError:
                    dct[name_str] = [name]
                else:
                    parent = BuiltinMethod(self, method, name.parent)
                    dct[name_str] = [
                        helpers.FakeName(name_str, parent, is_definition=True)
                    ]
        return dct
Exemplo n.º 3
0
 def _module_attributes(self):
     names = [
         '__file__', '__package__', '__doc__', '__name__', '__version__'
     ]
     # All the additional module attributes are strings.
     parent = Instance(self._evaluator,
                       compiled.create(self._evaluator, str))
     return [helpers.FakeName(n, parent) for n in names]
Exemplo n.º 4
0
 def _get_slots(self, evaluator, names_dict):
     slots = names_dict['__slots__'][0]
     statement = slots.parent
     node = self._evaluator.eval_statement(statement)[0]
     for v in node:
         pos = v.start_pos
         name = self._evaluator.eval_element(v)[0].obj
         yield name, [helpers.FakeName(name, self, pos, True)]
Exemplo n.º 5
0
    def parent(self):
        obj = self._value
        parser_path = []
        if inspect.ismodule(obj):
            module = obj
        else:
            class FakeParent(pr.Base):
                parent = None  # To avoid having no parent for NamePart.
                path = None

            names = []
            try:
                o = obj.__objclass__
                names.append(obj.__name__)
                obj = o
            except AttributeError:
                pass

            try:
                module_name = obj.__module__
                names.insert(0, obj.__name__)
            except AttributeError:
                # Unfortunately in some cases like `int` there's no __module__
                module = builtins
            else:
                module = __import__(module_name)
            fake_name = helpers.FakeName(names, FakeParent())
            parser_path = fake_name.names
        raw_module = get_module(self._value)

        try:
            path = module.__file__
        except AttributeError:
            pass
        else:
            path = re.sub('c$', '', path)
            if path.endswith('.py'):
                # cut the `c` from `.pyc`
                with open(path) as f:
                    source = source_to_unicode(f.read())
                mod = FastParser(source, path[:-1]).module
                if not parser_path:
                    return mod
                found = self._evaluator.eval_call_path(iter(parser_path), mod, None)
                if found:
                    return found[0]
                debug.warning('Interpreter lookup for Python code failed %s',
                              mod)

        module = compiled.CompiledObject(raw_module)
        if raw_module == builtins:
            # The builtins module is special and always cached.
            module = compiled.builtin
        return compiled.create(self._evaluator, self._value, module, module)
Exemplo n.º 6
0
 def get_defined_names(self):
     """
     Returns a list of names that define a generator, which can return the
     content of a generator.
     """
     executes_generator = '__next__', 'send', 'next'
     for name in compiled.generator_obj.get_defined_names():
         if name.name in executes_generator:
             parent = GeneratorMethod(self, name.parent)
             yield helpers.FakeName(name.name, parent)
         else:
             yield name
Exemplo n.º 7
0
 def names_dicts(self, search_global=False):  # is always False
     dct = {}
     executes_generator = '__next__', 'send', 'next'
     for names in compiled.generator_obj.names_dict.values():
         for name in names:
             if name.value in executes_generator:
                 parent = GeneratorMethod(self, name.parent)
                 dct[name.value] = [
                     helpers.FakeName(name.name, parent, is_definition=True)
                 ]
             else:
                 dct[name.value] = [name]
     yield dct
Exemplo n.º 8
0
 def _sub_modules(self):
     """
     Lists modules in the directory of this module (if this module is a
     package).
     """
     path = self._module.path
     names = []
     if path is not None and path.endswith(os.path.sep + '__init__.py'):
         mods = pkgutil.iter_modules([os.path.dirname(path)])
         for module_loader, name, is_pkg in mods:
             name = helpers.FakeName(name)
             # It's obviously a relative import to the current module.
             imp = helpers.FakeImport(name, self, level=1)
             name.parent = imp
             names.append(name)
     return names
    def _sub_modules_dict(self):
        """
        Lists modules in the directory of this module (if this module is a
        package).
        """
        path = self._module.path
        names = {}
        if path is not None and path.endswith(os.path.sep + '__init__.py'):
            mods = pkgutil.iter_modules([os.path.dirname(path)])
            for module_loader, name, is_pkg in mods:
                fake_n = helpers.FakeName(name)
                # It's obviously a relative import to the current module.
                imp = helpers.FakeImport(fake_n, self, level=1)
                fake_n.parent = imp
                names[name] = [fake_n]

        # TODO add something like this in the future, its cleaner than the
        #   import hacks.
        # ``os.path`` is a hardcoded exception, because it's a
        # ``sys.modules`` modification.
        #if str(self.name) == 'os':
        #    names.append(helpers.FakeName('path', parent=self))

        return names
Exemplo n.º 10
0
 def _generate_name(self, name):
     # Create a pseudo import to be able to follow them.
     name = helpers.FakeName(name)
     imp = helpers.FakeImport(name, parent=self.module)
     name.parent = imp
     return name
 def name(self):
     return helpers.FakeName(unicode(self.base.name), self, (1, 0))
 def name(self):
     name = self.base.name
     return helpers.FakeName(unicode(name), self, name.start_pos)
Exemplo n.º 13
0
 def _generate_name(self, name):
     return helpers.FakeName(name, parent=self.module)
Exemplo n.º 14
0
 def name(self):
     return helpers.FakeName(self.type, parent=self)
Exemplo n.º 15
0
 def _generate_name(self, name):
     return helpers.FakeName(name, parent=self.import_stmt)