Esempio n. 1
0
 def visit_FunctionDef(self, node):
     print "\t" + node.name
     if node.name == "clone":
         print "\t\t" + ast.dump(node)
         CloneVisitor().visit(node)
         cc_result = cc_visit_ast(node)[0]
         print cc_result.name + ": " + str(cc_result.classname) + " " + str(cc_result.complexity)
Esempio n. 2
0
 def visit_FunctionDef(self, node):
     if node.name != '__init__':
         self.methods.add(node.name)
     try:
         method_namespaces = self.symbol_table.lookup(node.name).get_namespaces()
         # if self.debug:
         #     print len(method_namespaces)
         method_table = method_namespaces[0]
         if len(method_namespaces) > 1:
             if node.name in self.method_namespaces.keys():
                 method_table = method_namespaces[self.method_namespaces[node.name]]
                 self.method_namespaces[node.name] += 1
             else:
                 self.method_namespaces[node.name] = 1
         method_extractor = MethodSymbolsExtractor(node, method_table, self.debug)
         method_extractor.extract()
         instance_variables_in_method = method_extractor.instance_references - self.methods
         self.instance_references |= instance_variables_in_method
         self.instance_variables_per_method[node.name] = instance_variables_in_method
         for reference in method_extractor.foreign_references.keys():
                 if reference in self.foreign_references.keys():
                     self.foreign_references[reference] |= method_extractor.foreign_references[reference]
                 else:
                     self.foreign_references[reference] = method_extractor.foreign_references[reference]
         cc_result = cc_visit_ast(node)[0]
         self.method_complexities[node.name] = cc_result.complexity
         if self.debug:
             print cc_result.name + ": " + str(cc_result.complexity)
         self.function_calls += method_extractor.function_calls
     except ValueError:
         print node.name
         print self.symbol_table.lookup(node.name).get_namespaces()[0].get_identifiers()
         print self.symbol_table.lookup(node.name).get_namespaces()[1].get_identifiers()
         raise
Esempio n. 3
0
 def visit_FunctionDef(self, node):
     print "\t" + node.name
     if node.name == "clone":
         print "\t\t" + ast.dump(node)
         CloneVisitor().visit(node)
         cc_result = cc_visit_ast(node)[0]
         print cc_result.name + ": " + str(cc_result.classname) + " " + str(
             cc_result.complexity)
Esempio n. 4
0
    def complexity(self):
        if self._complexity is None:
            comp = cc_visit_ast(self.ast_node)
            if not comp:
                raise MetricsException(
                    'Error while computing Complexity: no result')
            else:
                self._complexity = sum(c.complexity for c in comp)

        return self._complexity
Esempio n. 5
0
    def __init__(self, function: ast3.FunctionDef, module: "Module") -> None:
        """
        @cc 14
        @desc easier to use version of the ast function def
        @arg function: the AST functionDef to parse
        @arg module: the module this function resides in
        """

        # easy data
        self._function = function
        self.name = function.name
        self.line = function.lineno
        self.column = function.col_offset
        self.body = function.body
        self.module = module
        self.decorators = function.decorator_list

        # time to parse arguments
        self._args = function.args.args
        self.args = [Arg(x) for x in self._args]
        self.functions = [
            Function(x, self.module)
            for x in self.body
            if isinstance(x, ast3.FunctionDef)
        ]
        self.classes = [
            Class(x, self.module) for x in self.body if isinstance(x, ast3.ClassDef)
        ]
        self.untyped = [
            x for x in self.args if not x.typed and x not in DEFAULT_ARG_IGNORE
        ]
        self.doc = None
        self.returns = None
        self.return_typed = False
        self.missing_args: Set[str] = set()
        self.unexpected_args: Set[str] = set()
        arg_names = set(x.name for x in self.args if x.name not in DEFAULT_ARG_IGNORE)
        self.missing_args = arg_names
        if isinstance(self.body[0], ast3.Expr):
            # this is most likely a doc string
            self.doc = Doc(self.body[0], Doc.Type.FUNCTION)
            doc_arg_names = set(x for x, y in self.doc.args.items())
            self.missing_args = arg_names - doc_arg_names
            self.unexpected_args = doc_arg_names - arg_names
        if function.returns:
            self.return_typed = True
            self.returns = parse_elt(function.returns)  # type: ignore

        # complexity checks
        self._radon = cc_visit_ast(self._function)[0]
        self.complexity = self._radon.complexity
        self.is_method = self._radon.is_method
        self._halstead = h_visit_ast(self._function)
Esempio n. 6
0
 def visit_FunctionDef(self, node):
     if node.name != '__init__':
         self.methods.add(node.name)
     try:
         method_namespaces = self.symbol_table.lookup(
             node.name).get_namespaces()
         # if self.debug:
         #     print len(method_namespaces)
         method_table = method_namespaces[0]
         if len(method_namespaces) > 1:
             if node.name in self.method_namespaces.keys():
                 method_table = method_namespaces[self.method_namespaces[
                     node.name]]
                 self.method_namespaces[node.name] += 1
             else:
                 self.method_namespaces[node.name] = 1
         method_extractor = MethodSymbolsExtractor(node, method_table,
                                                   self.debug)
         method_extractor.extract()
         instance_variables_in_method = method_extractor.instance_references - self.methods
         self.instance_references |= instance_variables_in_method
         self.instance_variables_per_method[
             node.name] = instance_variables_in_method
         for reference in method_extractor.foreign_references.keys():
             if reference in self.foreign_references.keys():
                 self.foreign_references[
                     reference] |= method_extractor.foreign_references[
                         reference]
             else:
                 self.foreign_references[
                     reference] = method_extractor.foreign_references[
                         reference]
         cc_result = cc_visit_ast(node)[0]
         self.method_complexities[node.name] = cc_result.complexity
         if self.debug:
             print cc_result.name + ": " + str(cc_result.complexity)
         self.function_calls += method_extractor.function_calls
     except ValueError:
         print node.name
         print self.symbol_table.lookup(
             node.name).get_namespaces()[0].get_identifiers()
         print self.symbol_table.lookup(
             node.name).get_namespaces()[1].get_identifiers()
         raise
     except KeyError:
         #Let it go, let it gooo!
         pass
Esempio n. 7
0
def getBufferErrors(sourceCode):
    """Provides a list of warnings/errors for the given source code"""
    sourceCode += '\n'

    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(sourceCode, "<string>", "exec", PyCF_ONLY_AST)
    except SyntaxError as value:
        # If there's an encoding problem with the file, the text is None.
        if value.text is None:
            return {}, []
        return {value.lineno: [value.args[0]]}, []
    except (ValueError, TypeError) as value:
        # ValueError may happened in case of invalid \x escape character
        # E.g. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=674797
        # TypeError may happened in case of null characters in a file
        # E.g. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=674796
        msg = str(value)
        if msg == "":
            return {-1: ["Could not compile buffer: unknown error"]}, []
        return {-1: ["Could not compile buffer: " + msg]}, []

    # Okay, it's syntactically valid.  Now check it.
    check = Checker(tree, "<string>")
    results = {}
    lines = sourceCode.splitlines()
    for warning in check.messages:
        if isinstance(warning.lineno, int):
            lineno = warning.lineno
        else:
            # By some reasons I see ast NAME node here (pyflakes 0.7.3)
            lineno = warning.lineno.lineno
        if not IGNORE_REGEXP.search(lines[lineno - 1]):
            if lineno in results:
                results[lineno].append(warning.message % warning.message_args)
            else:
                results[lineno] = [warning.message % warning.message_args]

    # Radon: CC complexity as the second value
    return results, sorted_results(cc_visit_ast(tree))