pythonpylintpychecker

Check python code for errors


I can check python files or modules for error via some libraries, like: pylint, pychecker, pyflakes, etc. In most case, I must specify file or directory for checking. For example:

pylint directory/mymodule.py

It's ok, but not enough for me. I want analyze separated code block and get all detected errors and warnings. So, I must call a python code analyzer from my own module as a part of program.

import some_magic_checker

code = '''import datetime; print(datime.datime.now)'''
errors = some_magic_checker(code)
if len(errors) == 0:
    exec(code)  # code has no errors, I can execute its
else:
    print(errors)  # display info about detected errors

Does exist some python library like pylint or pyflakes, which provide functionality for python code checking without code compile? Thanks for your help.


UPD

I will try to explain what i mean in the simple example. I have one variable "codeString", which contains python source code. I must analyze this code (without any file creation and code execution, but I can compile code) and detect all warnings about incorrect blocks of a code. Let's look inside the pyflakes module and understand how it works.

There is a function "check" in module "pyflakes.api".

from pyflakes import checker
from pyflakes import reporter as modReporter
import _ast
import sys

def check(codeString, filename):
    reporter = modReporter._makeDefaultReporter()
    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
    # 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)

How you can see, this function cannot work only with one parameter "codeString", we must also provide second parameter "filename". And this is my biggest problem, I don't have any file, just Python code in string variable.

pylint, pychecker, pyflakes and all libraries, what i know, works only with created files. So i try to find some solutions, which don't require link to Python file.


Solution

  • Built-in function "compile" allow to create compiled code or AST object and catch some errors without file creation and code execution. We must pass '<string>' value as a second parameter, because code wasn’t read from a file.

    >>> def execute(code_string):
    >>>    output = list()
    >>>    try:
    >>>        tree = compile(code_string, '<string>', 'exec')
    >>>    except Exception as e:
    >>>        print(e)
    >>>    else:
    >>>        exec(tree)
    >>> # Now I can check code before calling the "exec" function
    >>> code_string = 'print("Hello_World!")'
    >>> execute(code_string)  # All is ok
    Hello_World!
    >>> code_string = 'ERROR! print("Hello_World!")'
    >>> execute(code_string)  # This code will not executed
    invalid syntax (<string>, line 1)