Exemple #1
0
def checkFlakes(codeString, filename, reporter):
    """Check the Python source given by codeString} for flakes."""
    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]
        (lineno, offset, text) = value.lineno, value.offset, value.text
        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            reporter.unexpectedError(filename, 'problem decoding source')
        else:
            reporter.syntaxError(filename, msg, lineno, offset, text)
        return
    except Exception:
        reporter.unexpectedError(filename, 'problem decoding source')
        return
    # Okay, it's syntactically valid.  Now check it.
    w = checker.Checker(tree, filename)
    w.messages.sort(key=lambda m: m.lineno)
    for warning in w.messages:
        reporter.flake(warning)
Exemple #2
0
def python_engine_pyflakes_check(filename):
    f = open(filename, 'r')
    code = ''.join(f.readlines())

    # first, compile into an AST and handle syntax errors.
    try:
        import _ast
        tree = compile(code, "", "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        line = text.splitlines()[-1]

        if offset is not None:
            offset = offset - (len(text) - len(line))

            return ['%s:%d: %s' % ("", lineno, msg)]

        return 1
    else:
        # okay, it's syntactically valid. Now check it.
        import pyflakes.checker as checker

        w = checker.Checker(tree, "")
        w.messages.sort(key=lambda m: m.lineno)
        return [warning.__str__() for warning in w.messages]
Exemple #3
0
    def pyflakes_check(self, code, filename, ignore=None):
        """Check the code with pyflakes to find errors
        """

        class FakeLoc:
            lineno = 0

        try:
            tree = compile(str(code), filename, 'exec', _ast.PyCF_ONLY_AST)
        except (SyntaxError, IndentationError) as value:
            return self._handle_syntactic_error(code, filename, value)
        except ValueError as error:
            return [PythonError(filename, FakeLoc(), error.args[0])]
        else:
            # the file is syntactically valid, check it now
            if ignore is not None:
                _magic_globals = pyflakes._MAGIC_GLOBALS
                pyflakes._MAGIC_GLOBALS += ignore

            w = pyflakes.Checker(tree, filename)

            if ignore is not None:
                pyflakes._MAGIC_GLOBALS = _magic_globals

            return w.messages
Exemple #4
0
    def flakes(self, input, *expectedOutputs, **kw):
        tree = ast.parse(textwrap.dedent(input))
        file_tokens = checker.make_tokens(textwrap.dedent(input))
        if kw.get("is_segment"):
            tree = tree.body[0]
            kw.pop("is_segment")
        w = checker.Checker(
            tree, file_tokens=file_tokens, withDoctest=self.withDoctest, **kw
        )
        outputs = [type(o) for o in w.messages]
        expectedOutputs = list(expectedOutputs)
        outputs.sort(key=lambda t: t.__name__)
        expectedOutputs.sort(key=lambda t: t.__name__)
        self.assertEqual(
            outputs,
            expectedOutputs,
            """\
for input:
%s
expected outputs:
%r
but got:
%s"""
            % (input, expectedOutputs, "\n".join([str(o) for o in w.messages])),
        )
        return w
Exemple #5
0
def pyflakes(filename):
    codeString = file(filename, 'U').read() + '\n'
    errors = []
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError as e:
        errors.append(dict(
            lnum=e.lineno or 0,
            col=e.offset or 0,
            text=getattr(e, 'msg', None) or str(e),
            type='E'
        ))
    else:
        w = checker.Checker(tree, filename)
        w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
        for w in w.messages:
            errors.append(dict(
                lnum=w.lineno,
                col=0,
                text=u'{0} {1}'.format(
                    flake_class_mapping.get(w.__class__, ''),
                    w.message % w.message_args),
                type='E'
            ))
    return errors
Exemple #6
0
def checkCode(codeString):
    filename = "code.py"

    warning = []
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        if text is None:
            error = "unexpectedError: Problema de decodificación de la fuente"
        else:
            error = "Code.py: " + str(lineno) + ": " + msg + " -> " + text
        return True, False, error.capitalize()
    except Exception:
        #reporter.unexpectedError(filename, 'problem decoding source')
        error = "unexpectedError: Problema de decodificación de la fuente"
        return False, error
    # Okay, it's syntactically valid.  Now check it.
    w = checker.Checker(tree, filename)
    w.messages.sort(key=lambda m: m.lineno)
    warns = False
    for war in w.messages:
        warns = True
        warning.append(str(war).capitalize())
    return False, warns, warning
Exemple #7
0
def check(codeString, filename, outfile=None):
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except (SyntaxError, IndentationError):
        value = sys.exc_info()[1]
        try:
            (lineno, offset, line) = value[1][1:]
        except IndexError:
            print >> sys.stderr, 'could not compile %r' % (filename, )
            return 1
        if line.endswith("\n"):
            line = line[:-1]
        print >> sys.stderr, '%s:%d: could not compile' % (filename, lineno)
        print >> sys.stderr, line
        print >> sys.stderr, " " * (offset - 2), "^"
        return 1
    else:
        w = checker.Checker(tree, filename)
        w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
        for warning in w.messages:
            if outfile:
                outfile.write(str(warning) + '\n')
            else:
                print warning
        return len(w.messages)
Exemple #8
0
def check(codeString, filename, reporter=None):
    """
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: The number of warnings emitted.
    @rtype: C{int}
    """
    if reporter is None:
        reporter = modReporter._makeDefaultReporter()
    # First, compile into an AST and handle syntax errors.
    try:
        tree = ast.parse(codeString, filename=filename)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        if checker.PYPY:
            if text is None:
                lines = codeString.splitlines()
                if len(lines) >= lineno:
                    text = lines[lineno - 1]
                    if sys.version_info >= (3, ) and isinstance(text, bytes):
                        try:
                            text = text.decode('ascii')
                        except UnicodeDecodeError:
                            text = None
            offset -= 1

        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            reporter.unexpectedError(filename, 'problem decoding source')
        else:
            reporter.syntaxError(filename, msg, lineno, offset, text)
        return 1
    except Exception:
        reporter.unexpectedError(filename, 'problem decoding source')
        return 1
    # Okay, it's syntactically valid.  Now check it.
    file_tokens = checker.make_tokens(codeString)
    w = checker.Checker(tree, file_tokens=file_tokens, filename=filename)
    w.messages.sort(key=lambda m: m.lineno)
    for warning in w.messages:
        reporter.flake(warning)
    return len(w.messages)
Exemple #9
0
def check(buffer):
    filename = buffer.name
    contents = buffer[:]

    # shebang usually found at the top of the file, followed by source code encoding marker.
    # assume everything else that follows is encoded in the encoding.
    for n, line in enumerate(contents):
        if n >= 2:
            break
        elif re.match(r'#.*coding[:=]\s*([-\w.]+)', line):
            contents = [''] * (n + 1) + contents[n + 1:]
            break

    contents = '\n'.join(contents) + '\n'

    vimenc = vim.eval('&encoding')
    if vimenc:
        contents = contents.decode(vimenc)

    builtins = set(['__file__'])
    try:
        builtins.update(set(eval(vim.eval('string(g:pyflakes_builtins)'))))
    except Exception:
        pass

    try:
        # TODO: use warnings filters instead of ignoring stderr
        old_stderr, sys.stderr = sys.stderr, blackhole()
        try:
            tree = ast.parse(contents, filename or '<unknown>')
        finally:
            sys.stderr = old_stderr
    except:
        try:
            value = sys.exc_info()[1]
            lineno, offset, line = value[1][1:]
        except IndexError:
            lineno, offset, line = 1, 0, ''
        if line and line.endswith("\n"):
            line = line[:-1]

        return [SyntaxError(filename, lineno, offset, str(value))]
    else:
        # pyflakes looks to _MAGIC_GLOBALS in checker.py to see which
        # UndefinedNames to ignore
        old_globals = getattr(checker, ' _MAGIC_GLOBALS', [])
        checker._MAGIC_GLOBALS = set(old_globals) | builtins

        filename = '(none)' if filename is None else filename
        w = checker.Checker(tree, filename)

        checker._MAGIC_GLOBALS = old_globals

        w.messages.sort(key=attrgetter('lineno'))
        return w.messages
    def _checkPySource(self, file_path):
        with open(file_path) as file_obj:
            tree = ast.parse(file_obj.read(), file_path)

        warnings = checker.Checker(tree, file_path)
        warnings.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
        # Log individual errors so we see them all instead of just the first
        for msg in warnings.messages:
            log.err(self.failureException("pyflakes: %s" % (msg, )))

        return len(warnings.messages)
Exemple #11
0
def _validate_pyflakes(fd, options={}):
    if is_python3(fd):
        # TODO: Pyflakes supports Python 3, but we would need to run it in Python3
        return True
    from pyflakes import checker
    tree = ast.parse(fd.read(), fd.name)
    w = checker.Checker(tree, fd.name)
    w.messages.sort(key=lambda x: x.lineno)
    for message in w.messages:
        error = message.message % message.message_args
        _detail(error, line=message.lineno)
    return len(w.messages) == 0
Exemple #12
0
def get_flakes(source, filename):
    try:
        tree = ast.parse(source, filename, "exec")
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]
        yield Problem('error', value.lineno, value.offset, msg)
    else:
        results = checker.Checker(tree, filename)
        for m in results.messages:
            yield Problem('warn', m.lineno, m.col,
                          m.message % m.message_args)
 def check(self, document):
     filename = document.get_short_name_for_display()
     start, end = document.get_bounds()
     text = document.get_text(start, end, True)
     try:
         with BlackHole():
             tree = ast.parse(text, filename)
     except SyntaxError as e:
         return [PySyntaxError(filename, e.lineno, e.offset, e.text)]
     else:
         w = checker.Checker(tree, filename)
         w.messages.sort(key=attrgetter('lineno'))
         return w.messages
Exemple #14
0
def lint(tree):
    # Wrap the whole module in a function
    # so that pyflakes thinks global variables are local variables
    # and reports when they are unused
    function_tree = ast.parse("def f(): 0")
    function_tree.body[0].body = tree.body

    ch = checker.Checker(function_tree, builtins=["assert_equal"])
    ch.messages.sort(key=lambda m: m.lineno)
    for message in ch.messages:
        if type(message) in MESSAGES:
            message_format = MESSAGES[type(message)]
            yield message_format.format(*message.message_args)
Exemple #15
0
def run_pyflakes(request_data):
    """
    Worker that run a frosted (the fork of pyflakes) code analysis on the
    current editor text.
    """
    global prev_results
    from pyflakes import checker
    import _ast
    WARNING = 1
    ERROR = 2
    ret_val = []
    code = request_data['code']
    path = request_data['path']
    encoding = request_data['encoding']
    if not encoding:
        encoding = 'utf-8'
    if not path:
        path = os.path.join(tempfile.gettempdir(), 'temp.py')
    if not code:
        return []
    else:
        # First, compile into an AST and handle syntax errors.
        try:
            tree = compile(code.encode(encoding), path, "exec",
                           _ast.PyCF_ONLY_AST)
        except SyntaxError as value:
            msg = '[pyFlakes] %s' % value.args[0]
            (lineno, offset, text) = value.lineno - 1, value.offset, value.text
            # If there's an encoding problem with the file, the text is None
            if text is None:
                # Avoid using msg, since for the only known case, it
                # contains a bogus message that claims the encoding the
                # file declared was unknown.s
                _logger().warning("[SyntaxError] %s: problem decoding source",
                                  path)
            else:
                ret_val.append((msg, ERROR, lineno))
        else:
            # Okay, it's syntactically valid.  Now check it.
            w = checker.Checker(tree, os.path.split(path)[1])
            w.messages.sort(key=lambda m: m.lineno)
            for message in w.messages:
                msg = "[pyFlakes] %s" % str(message).split(':')[-1].strip()
                line = message.lineno - 1
                status = WARNING \
                    if message.__class__ not in PYFLAKES_ERROR_MESSAGES \
                    else ERROR
                ret_val.append((msg, status, line))
    prev_results = ret_val
    return ret_val
Exemple #16
0
    def flakes(self, input, *expectedOutputs, **kw):
        w = checker.Checker(compiler.parse(textwrap.dedent(input)), **kw)
        outputs = [type(o) for o in w.messages]
        expectedOutputs = list(expectedOutputs)
        outputs.sort()
        expectedOutputs.sort()
        self.assert_(outputs == expectedOutputs, '''\
for input:
%s
expected outputs:
%s
but got:
%s''' % (input, repr(expectedOutputs), '\n'.join([str(o) for o in w.messages])))
        return w
Exemple #17
0
    def flakes(self, input, *expectedOutputs, **kw):
        tree = compile(textwrap.dedent(input), "<test>", "exec", PyCF_ONLY_AST)
        w = checker.Checker(tree, **kw)
        outputs = [type(o) for o in w.messages]
        expectedOutputs = list(expectedOutputs)
        outputs.sort(key=lambda t: t.__name__)
        expectedOutputs.sort(key=lambda t: t.__name__)
        self.assertEqual(outputs, expectedOutputs, '''\
for input:
%s
expected outputs:
%r
but got:
%s''' % (input, expectedOutputs, '\n'.join([str(o) for o in w.messages])))
        return w
Exemple #18
0
def validate(code: str, pb: str) -> bool:
    try:
        tree = ast.parse(code)
        results = checker.Checker(tree)

        for error in results.messages:
            if isinstance(error, UNDEFINED_ERRORS):
                for arg in error.message_args:
                    if arg not in UNDEFINED_ERRORS_TO_IGNORE[pb]:
                        return False
            elif not isinstance(error, ERROR_TYPES_TO_IGNORE):
                return False
        return True
    except SyntaxError:
        return False
Exemple #19
0
def pyflakes(filename):
    from pyflakes import checker
    import _ast

    codeString = file(filename, 'U').read() + '\n'
    errors = []
    tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    w = checker.Checker(tree, filename)
    w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
    for w in w.messages:
        errors.append(
            dict(lnum=w.lineno,
                 col=w.col,
                 text=w.message % w.message_args,
                 type='E'))
    return errors
Exemple #20
0
    def _check_pyflakes(self, path):
        if not install_flg['pyflakes']:
            return [{'line': 0, 'msg': 'no install pyflakes'}]

        arr = []
        with open(path, 'r') as f:
            for line in f:
                arr.append(line)

        res = []
        tree = ast.parse(''.join(arr), path)
        w = checker.Checker(tree, path)
        for m in w.messages:
            res.append({'line': m.lineno, 'msg': m.message % m.message_args})

        return res
Exemple #21
0
def check(codeString, filename, reporter=None):
    """
    Check the Python source given by C{codeString} for flakes.

    @param codeString: The Python source to check.
    @type codeString: C{str}

    @param filename: The name of the file the source came from, used to report
        errors.
    @type filename: C{str}

    @param reporter: A L{Reporter} instance, where errors and warnings will be
        reported.

    @return: The number of warnings emitted.
    @rtype: C{int}
    """
    if reporter is None:
        reporter = modReporter._makeDefaultReporter()
    # First, compile into an AST and handle syntax errors.
    try:
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
    except SyntaxError:
        value = sys.exc_info()[1]
        msg = value.args[0]

        (lineno, offset, text) = value.lineno, value.offset, value.text

        # If there's an encoding problem with the file, the text is None.
        if text is None:
            # Avoid using msg, since for the only known case, it contains a
            # bogus message that claims the encoding the file declared was
            # unknown.
            reporter.unexpectedError(filename, 'problem decoding source')
        else:
            reporter.syntaxError(filename, msg, lineno, offset, text)
        return 1
    except Exception:
        reporter.unexpectedError(filename, 'problem decoding source')
        return 1
    else:
        # Okay, it's syntactically valid.  Now check it.
        w = checker.Checker(tree, filename)
        w.messages.sort(key=lambda m: m.lineno)
        for warning in w.messages:
            reporter.flake(warning)
        return len(w.messages)
Exemple #22
0
    def check(self, code, filename, ignore=None):
        """Check the code with pyflakes to find errors
        """
        class FakeLoc:
            lineno = 0

        try:
            code = code.encode('utf8') + b'\n'
            tree = compile(code, filename or '', 'exec', _ast.PyCF_ONLY_AST)
        except (SyntaxError, IndentationError):
            return self._handle_syntactic_error(code, filename)
        except ValueError as error:
            return [PyFlakesError(filename, FakeLoc(), error.args[0])]
        else:
            # the file is syntactically valid, check it now
            w = pyflakes.Checker(tree, filename, ignore)

            return w.messages
Exemple #23
0
    def flakes(self, input, *expectedOutputs, **kw):
        ast = compile(textwrap.dedent(input), "<test>", "exec",
                      _ast.PyCF_ONLY_AST)
        w = checker.Checker(ast, **kw)
        outputs = [type(o) for o in w.messages]
        expectedOutputs = list(expectedOutputs)
        outputs.sort()
        expectedOutputs.sort()
        self.assert_(
            outputs == expectedOutputs, '''\
for input:
%s
expected outputs:
%s
but got:
%s''' % (input, repr(expectedOutputs), '\n'.join([str(o)
                                                  for o in w.messages])))
        return w
Exemple #24
0
    def run_check(self, context: RunContext):  # noqa
        """Check code with pyflakes."""
        params = context.get_params("pyflakes")
        builtins = params.get("builtins", "")
        if builtins:
            builtins = builtins.split(",")

        check = checker.Checker(context.ast,
                                context.filename,
                                builtins=builtins)
        for msg in check.messages:
            context.push(
                lnum=msg.lineno,
                col=msg.col + 1,
                text=msg.message % msg.message_args,
                number=CODES.get(msg.message, ""),
                source="pyflakes",
            )
Exemple #25
0
    def run(path, code=None, params=None, **meta):
        """Check code with pyflakes.

        :return list: List of errors.
        """
        import _ast

        builtins = params.get("builtins", "")

        if builtins:
            builtins = builtins.split(",")

        tree = compile(code, path, "exec", _ast.PyCF_ONLY_AST)
        w = checker.Checker(tree, path, builtins=builtins)
        w.messages = sorted(w.messages, key=lambda m: m.lineno)
        return [
            {'lnum': m.lineno, 'text': m.message % m.message_args}
            for m in sorted(w.messages, key=lambda m: m.lineno)
        ]
Exemple #26
0
    def pyflakes_check(self, code, filename, ignore=None):
        try:
            tree = compile(code, filename, "exec", _ast.PyCF_ONLY_AST)
        except (SyntaxError, IndentationError) as value:
            msg = value.args[0]

            (lineno, offset, text) = value.lineno, value.offset, value.text

            # If there's an encoding problem with the file, the text is None.
            if text is None:
                # Avoid using msg, since for the only known case, it contains a
                # bogus message that claims the encoding the file declared was
                # unknown.
                if msg.startswith('duplicate argument'):
                    arg = msg.split('duplicate argument ', 1)[1].split(' ', 1)[0].strip('\'"')
                    error = pyflakes.messages.DuplicateArgument(filename, lineno, arg)
                else:
                    error = PythonError(filename, lineno, msg)
            else:
                line = text.splitlines()[-1]

                if offset is not None:
                    offset = offset - (len(text) - len(line))

                if offset is not None:
                    error = OffsetError(filename, lineno, msg, offset)
                else:
                    error = PythonError(filename, lineno, msg)
            return [error]
        except ValueError as e:
            return [PythonError(filename, 0, e.args[0])]
        else:
            # Okay, it's syntactically valid.  Now check it.
            if ignore is not None:
                old_magic_globals = pyflakes._MAGIC_GLOBALS
                pyflakes._MAGIC_GLOBALS += ignore

            w = pyflakes.Checker(tree, filename)

            if ignore is not None:
                pyflakes._MAGIC_GLOBALS = old_magic_globals

            return w.messages
    def run(path, code=None, builtins="", **meta):
        """ Pyflake code checking.

        :return list: List of errors.

        """
        import _ast
        import os

        os.environ.setdefault('PYFLAKES_BUILTINS', builtins)

        errors = []
        tree = compile(code, path, "exec", _ast.PyCF_ONLY_AST)
        w = checker.Checker(tree, path)
        w.messages = sorted(w.messages, key=lambda m: m.lineno)
        for w in w.messages:
            errors.append(
                dict(
                    lnum=w.lineno,
                    text=w.message % w.message_args,
                ))
        return errors
Exemple #28
0
    def to_output(self):
        """
        Produce json output

        :return: json result report
        """
        results = []
        cli = {'cli': results}
        warning_dict = {}
        try:
            tree = ast.parse(self.codeString, filename=self.filename)
            file_tokens = checker.make_tokens(self.codeString)
            w = checker.Checker(tree,
                                file_tokens=file_tokens,
                                filename=self.filename)
            w.messages.sort(key=lambda m: m.lineno)
            for warning in w.messages:
                warning_dict['message'] = str(warning).split(' ', 1)[1]
                warning = str(warning).split(' ', 1)[0]
                loc = str(warning).split(':')
                warning_dict['file'] = loc[0] if loc[0] else None
                warning_dict['line'] = loc[1] if loc[1] else None
                warning_dict['column'] = loc[2] if loc[2] else None
                results.append(warning_dict.copy())
        except SyntaxError:
            value = sys.exc_info()[1]
            msg = value.args[0]
            warning_dict['message'] = msg
            warning_dict['file'] = self.filename
            warning_dict['line'] = value.lineno if value.lineno else None
            warning_dict['column'] = value.offset if value.offset else None
            results.append(warning_dict.copy())

        flakes_json = {'results': cli}
        flakes_json = json.dumps(flakes_json,
                                 sort_keys=True,
                                 indent=2,
                                 separators=(',', ': '))
        return flakes_json + '\n'
Exemple #29
0
    def _check(self, codeString, filename, warnings):
        try:
            tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
        except (SyntaxError, IndentationError) as value:
            msg = value.args[0]

            (lineno, offset, text) = value.lineno, value.offset, value.text

            # If there's an encoding problem with the file, the text is None.
            if text is None:
                # Avoid using msg, since for the only known case, it contains a
                # bogus message that claims the encoding the file declared was
                # unknown.
                print("{}: problem decoding source".format(filename),
                      file=sys.stderr)
            else:
                line = text.splitlines()[-1]

                if offset is not None:
                    offset = offset - (len(text) - len(line))

                print("%s:%d: %s" % (filename, lineno, msg), file=sys.stderr)
                print(line, file=sys.stderr)

                if offset is not None:
                    print(" " * offset, "^", file=sys.stderr)

            return 1
        except UnicodeError as msg:
            print("encoding error at %r: %s" % (filename, msg),
                  file=sys.stderr)
            return 1
        else:
            # Okay, it's syntactically valid.
            # Now parse it into an ast and check it.
            w = checker.Checker(tree, filename)
            warnings.extend(w.messages)
            return len(warnings)
Exemple #30
0
    def run(self, code):
        """Make the analysis

        This function is inspired from the check function of the pyflakes start
        script.
        """
        print("run called in pychecker")
        self._code = code
        # Open a buffer for the output
        output = StringIO()
        # Start the check
        try:
            tree = compiler.parse(self._code)
        except (SyntaxError, IndentationError):
            value = sys.exc_info()[1]
            try:
                (lineno, offset, line) = value[1][1:]
            except IndexError:
                print >> output, _('Could not compile the code.')
            else:
                if line.endswith("\n"):
                    line = line[:-1]
                print >> output, _('line %d: could not compile') % lineno
                print >> output, line
                print >> output, " " * (offset - 2), "^"
            self._nb_errors = None
        else:
            w = checker.Checker(tree, 'line')
            w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
            for warning in w.messages:
                print >> output, warning
            self._nb_errors = len(w.messages)

        # Get the output and remove the irrelevant file path
        self._report = output.getvalue()
        # Close the buffer
        output.close()