Exemple #1
0
 def _handle_conditional_node(self, node):
     self.conditional = True
     try:
         for child in ast.get_child_nodes(node):
             ast.walk(child, self)
     finally:
         self.conditional = False
Exemple #2
0
 def get_children(self):
     if self._children is None:
         walker = _SuiteWalker(self)
         for child in self.child_nodes:
             ast.walk(child, walker)
         self._children = walker.suites
     return self._children
Exemple #3
0
 def find_reads_for_one_liners(code):
     if code.strip() == '':
         return set(), set()
     node = _parse_text(code)
     visitor = _VariableReadsAndWritesFinder()
     ast.walk(node, visitor)
     return visitor.read
Exemple #4
0
 def loop_encountered(self, node):
     self.loop_count += 1
     for child in node.body:
         ast.walk(child, self)
     self.loop_count -= 1
     if node.orelse:
         ast.walk(node.orelse, self)
Exemple #5
0
 def _With(self, node):
     for item in pycompat.get_ast_with_items(node):
         if item.optional_vars:
             self._update_evaluated(item.optional_vars,
                                    item.context_expr, '.__enter__()')
     for child in node.body:
         ast.walk(child, self)
Exemple #6
0
 def has_errors(code):
     if code.strip() == '':
         return False
     node = _parse_text(code)
     visitor = _UnmatchedBreakOrContinueFinder()
     ast.walk(node, visitor)
     return visitor.error
Exemple #7
0
 def _create_structural_attributes(self):
     if self.visitor_class is None:
         return {}
     new_visitor = self.visitor_class(self.pycore, self)
     for child in ast.get_child_nodes(self.ast_node):
         ast.walk(child, new_visitor)
     self.defineds = new_visitor.defineds
     return new_visitor.names
 def _excepthandler(self, node):
     if node.name is not None and isinstance(node.name, str):
         type_node = node.type
         if isinstance(node.type, ast.Tuple) and type_node.elts:
             type_node = type_node.elts[0]
         self._update_evaluated(node.name, type_node, eval_type=True)
     for child in node.body:
         ast.walk(child, self)
 def _FunctionDef(self, node):
     _ScopeVisitor._FunctionDef(self, node)
     if len(node.args.args) > 0:
         first = node.args.args[0]
         if isinstance(first, ast.arg):
             new_visitor = _ClassInitVisitor(self, first.arg)
             for child in ast.get_child_nodes(node):
                 ast.walk(child, new_visitor)
Exemple #10
0
def find_errors(project, resource):
    """Find possible bad name and attribute accesses

    It returns a list of `Error`\s.
    """
    pymodule = project.get_pymodule(resource)
    finder = _BadAccessFinder(pymodule)
    ast.walk(pymodule.get_ast(), finder)
    return finder.errors
Exemple #11
0
 def _Attribute(self, node):
     if not isinstance(node.ctx, ast.Store):
         scope = self.scope.get_inner_scope_for_line(node.lineno)
         pyname = evaluate.eval_node(scope, node.value)
         if pyname is not None and \
            pyname.get_object() != pyobjects.get_unknown():
             if node.attr not in pyname.get_object():
                 self._add_error(node, 'Unresolved attribute')
     ast.walk(node.value, self)
Exemple #12
0
 def find_reads_and_writes(code):
     if code.strip() == '':
         return set(), set()
     if isinstance(code, unicode):
         code = code.encode('utf-8')
     node = _parse_text(code)
     visitor = _VariableReadsAndWritesFinder()
     ast.walk(node, visitor)
     return visitor.read, visitor.written
Exemple #13
0
 def _visit_function(self):
     if self.names is None:
         new_visitor = self.visitor(self.pycore, self.pyobject)
         for n in ast.get_child_nodes(self.pyobject.get_ast()):
             ast.walk(n, new_visitor)
         self.names = new_visitor.names
         self.names.update(self.pyobject.get_parameters())
         self.returned_asts = new_visitor.returned_asts
         self.is_generator = new_visitor.generator
         self.defineds = new_visitor.defineds
Exemple #14
0
 def _Tuple(self, node):
     new_levels = []
     if self.levels is not None:
         new_levels = list(self.levels)
         new_levels.append(self.index)
     self.index += 1
     visitor = _NodeNameCollector(new_levels)
     for child in ast.get_child_nodes(node):
         ast.walk(child, visitor)
     self.names.extend(visitor.names)
Exemple #15
0
 def _FunctionDef(self, node):
     _ScopeVisitor._FunctionDef(self, node)
     if len(node.args.args) > 0:
         first = node.args.args[0]
         new_visitor = None
         if isinstance(first, pycompat.ast_arg_type):
             new_visitor = _ClassInitVisitor(self, pycompat.get_ast_arg_arg(first))
         if new_visitor is not None:
             for child in ast.get_child_nodes(node):
                 ast.walk(child, new_visitor)
Exemple #16
0
 def _create_info_collector(self):
     zero = self.info.scope.get_start() - 1
     start_line = self.info.region_lines[0] - zero
     end_line = self.info.region_lines[1] - zero
     info_collector = _FunctionInformationCollector(start_line, end_line,
                                                    self.info.global_)
     body = self.info.source[self.info.scope_region[0]:
                             self.info.scope_region[1]]
     node = _parse_text(body)
     ast.walk(node, info_collector)
     return info_collector
Exemple #17
0
 def _Attribute(self, node):
     result = []
     while isinstance(node, ast.Attribute):
         result.append(node.attr)
         node = node.value
     if isinstance(node, ast.Name):
         result.append(node.id)
         primary = ".".join(reversed(result))
         if self._get_root()._is_node_interesting(node) and not self.is_bound(primary):
             self.add_unbound(primary)
     else:
         ast.walk(node, self)
Exemple #18
0
 def _FunctionDef(self, node):
     _ScopeVisitor._FunctionDef(self, node)
     if len(node.args.args) > 0:
         first = node.args.args[0]
         new_visitor = None
         if hasattr(ast, 'arg'):  # Py3
             if isinstance(first, ast.arg):
                 new_visitor = _ClassInitVisitor(self, first.arg)
         else:  # Py2
             if isinstance(first, ast.Name):
                 new_visitor = _ClassInitVisitor(self, first.id)
         if new_visitor is not None:
             for child in ast.get_child_nodes(node):
                 ast.walk(child, new_visitor)
Exemple #19
0
 def _FunctionDef(self, node):
     if not self.is_global and self.host_function:
         self.host_function = False
         for name in _get_argnames(node.args):
             self._written_variable(name, node.lineno)
         for child in node.body:
             ast.walk(child, self)
     else:
         self._written_variable(node.name, node.lineno)
         visitor = _VariableReadsAndWritesFinder()
         for child in node.body:
             ast.walk(child, visitor)
         for name in visitor.read - visitor.written:
             self._read_variable(name, node.lineno)
Exemple #20
0
def get_name_levels(node):
    """Return a list of ``(name, level)`` tuples for assigned names

    The `level` is `None` for simple assignments and is a list of
    numbers for tuple assignments for example in::

      a, (b, c) = x

    The levels for for `a` is ``[0]``, for `b` is ``[1, 0]`` and for
    `c` is ``[1, 1]``.

    """
    visitor = _NodeNameCollector()
    ast.walk(node, visitor)
    return visitor.names
 def _With(self, node):
     if (sys.version_info[1] < 3):
         if node.optional_vars:
             self._update_evaluated(node.optional_vars,
                                    node.context_expr, '.__enter__()')
     elif len(node.items) > 0:
         #TODO Handle all items?
         if node.items[0].optional_vars:
             self._update_evaluated(
                 node.items[0].optional_vars,
                 node.items[0].context_expr, 
                 '.__enter__()'
             )
                             
     for child in node.body:
         ast.walk(child, self)
Exemple #22
0
    def _For(self, node):
        self.conditional = True
        try:
            # iter has to be checked before the target variables
            ast.walk(node.iter, self)
            ast.walk(node.target, self)

            for child in node.body:
                ast.walk(child, self)
            for child in node.orelse:
                ast.walk(child, self)
        finally:
            self.conditional = False
Exemple #23
0
 def loop_encountered(self, node):
     self.loop_count += 1
     for child in node.body:
         ast.walk(child, self)
     self.loop_count -= 1
     if node.orelse:
         if isinstance(node.orelse,(list,tuple)):
             for node_ in node.orelse:
                 ast.walk(node_, self)
         else:
             ast.walk(node.orelse, self)
Exemple #24
0
 def _visit_child_scope(self, node):
     pyobject = self.pyobject.get_module().get_scope().\
         get_inner_scope_for_line(node.lineno).pyobject
     visitor = _LocalUnboundNameFinder(pyobject, self)
     for child in ast.get_child_nodes(node):
         ast.walk(child, visitor)
Exemple #25
0
 def _get_unbound_names(self, defined_pyobject):
     visitor = _GlobalUnboundNameFinder(self.pymodule, defined_pyobject)
     ast.walk(self.pymodule.get_ast(), visitor)
     return visitor.unbound
Exemple #26
0
 def _comprehension(self, node):
     ast.walk(node.target, self)
     ast.walk(node.iter, self)
 def _Assign(self, node):
     self.assigned_ast = node.value
     for child_node in node.targets:
         ast.walk(child_node, self)
 def _Assign(self, node):
     ast.walk(node, _AssignVisitor(self))
Exemple #29
0
def eval_node2(scope, node):
    evaluator = StatementEvaluator(scope)
    ast.walk(node, evaluator)
    return evaluator.old_result, evaluator.result
 def _Tuple(self, node):
     if not isinstance(node.ctx, ast.Store):
         return
     for child in ast.get_child_nodes(node):
         ast.walk(child, self)
Exemple #31
0
 def _NamedExpr(self, node):
     ast.walk(node.target, _AssignVisitor(self))
     ast.walk(node.value, self)
Exemple #32
0
 def _Assign(self, node):
     ast.walk(node.value, self)
     for child in node.targets:
         ast.walk(child, self)
Exemple #33
0
def eval_node2(scope, node):
    evaluator = StatementEvaluator(scope)
    ast.walk(node, evaluator)
    return evaluator.old_result, evaluator.result
Exemple #34
0
 def start_walking(self, node):
     nodes = [node]
     if isinstance(node, ast.FunctionDef):
         nodes = ast.get_child_nodes(node)
     for child in nodes:
         ast.walk(child, self)
Exemple #35
0
 def _FunctionDef(self, node):
     self.written.add(node.name)
     visitor = _VariableReadsAndWritesFinder()
     for child in ast.get_child_nodes(node):
         ast.walk(child, visitor)
     self.read.update(visitor.read - visitor.written)
Exemple #36
0
 def _Assign(self, node):
     ast.walk(node.value, self)
     for child in node.targets:
         ast.walk(child, self)
 def _For(self, node):
     names = self._update_evaluated(node.target, node.iter,
                                    '.__iter__().next()')
     for child in node.body + node.orelse:
         ast.walk(child, self)
 def _get_unbound_names(self, defined_pyobject):
     visitor = _GlobalUnboundNameFinder(self.pymodule, defined_pyobject)
     ast.walk(self.pymodule.get_ast(), visitor)
     return visitor.unbound
Exemple #39
0
 def _Assign(self, node):
     self.assigned_ast = node.value
     for child_node in node.targets:
         ast.walk(child_node, self)
     ast.walk(node.value, _ExpressionVisitor(self.scope_visitor))
 def _With(self, node):
     if node.optional_vars:
         self._update_evaluated(node.optional_vars,
                                node.context_expr, '.__enter__()')
     for child in node.body:
         ast.walk(child, self)
Exemple #41
0
 def _FunctionDef(self, node):
     self.written.add(node.name)
     visitor = _VariableReadsAndWritesFinder()
     for child in ast.get_child_nodes(node):
         ast.walk(child, visitor)
     self.read.update(visitor.read - visitor.written)
 def _For(self, node):
     names = self._update_evaluated(node.target, node.iter,  # noqa
                                    '.__iter__().next()')
     for child in node.body + node.orelse:
         ast.walk(child, self)
Exemple #43
0
    def _AnnAssign(self, node):
        self.assigned_ast = node.value
        self.type_hint = node.annotation

        ast.walk(node.target, self)
 def _Tuple(self, node):
     if not isinstance(node.ctx, ast.Store):
         return
     for child in ast.get_child_nodes(node):
         ast.walk(child, self)
Exemple #45
0
 def _AugAssign(self, node):
     ast.walk(node.value, self)
     self._read_variable(node.target.id, node.target.lineno)
     self._written_variable(node.target.id, node.target.lineno)
 def _visit_child_scope(self, node):
     pyobject = self.pyobject.get_module().get_scope().\
                get_inner_scope_for_line(node.lineno).pyobject
     visitor = _LocalUnboundNameFinder(pyobject, self)
     for child in ast.get_child_nodes(node):
         ast.walk(child, visitor)