我可以通过一些库检查python文件或模块的错误,比如:pylint、pychecker、pyflakes等。在大多数情况下,我必须指定要检查的文件或目录。例如:pylint directory/mymodule.py
没关系,但对我来说还不够。我要分析分离的代码块,得到所有检测到的错误和警告。所以,作为程序的一部分,我必须从自己的模块调用python代码分析器。在
^{pr2}$
是否存在一些pylint或pyflakes之类的python库,这些库提供了python代码检查的功能,而无需编译代码?谢谢你的帮助。在
升级版
我将在这个简单的例子中解释我的意思。我有一个变量“codeString”,它包含python源代码。我必须分析这段代码(不需要任何文件创建和代码执行,但我可以编译代码)并检测所有关于错误代码块的警告。让我们看看pyflakes模块的内部,了解它是如何工作的。在
模块中有一个“check”功能皮弗莱克斯.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)
你可以看到,这个函数不能只使用一个参数“codeString”,我们还必须提供第二个参数“filename”。这是我最大的问题,我没有任何文件,只有字符串变量中的Python代码。在
据我所知,pylint、pychecker、pyflakes和所有库都只能处理创建的文件。所以我试图找到一些不需要链接到Python文件的解决方案。在