0

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.

eterey
  • 390
  • 1
  • 4
  • 16
  • 1
    pyflakes doesn't compile any code. – Martijn Pieters Jul 14 '14 at 15:10
  • why does it matter if the code compiles - in general the only way to detect the errors and warnings is to do a large chunk of the work of a compile anyway. – Tony Suffolk 66 Jul 14 '14 at 17:27
  • def some_magic_checker(code): store code in tmp file; run various checkers; return grabbed errors ? – Julien Palard Jul 14 '14 at 20:26
  • @JulienPalar, I don't want create temporary python files, because it's too slow for my task. – eterey Jul 15 '14 at 09:51
  • @TonySuffolk66, Compilation and execution of the python code can cause: sending query to database, writing changes to filesystem, etc. It's bad for me. – eterey Jul 15 '14 at 09:57
  • 1
    I can understand that you don't want execution, but compilation is not the same as execution. You can compile code very simply by opening the python interactive console and importing it, so long as your modules have no side-effects. – Tony Suffolk 66 Jul 15 '14 at 10:15
  • @TonySuffolk66, Comment length limitation won't allow me fully explain what I mean, so I update my question. Also I must say thanks for your help =) – eterey Jul 15 '14 at 11:37
  • @MartijnPieters, It's ok for me. But I don't know how to use pyflakes without creation a Python file. – eterey Jul 15 '14 at 11:40
  • 1
    I understand what you are trying to do - still not sure on the why though, but leaving that aside - could you not use the compile function - relatively easy to use. – Tony Suffolk 66 Jul 15 '14 at 12:05

1 Answers1

0

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)
eterey
  • 390
  • 1
  • 4
  • 16