Upgrade version of pyflakes used for error checking, to support python 2.7 new syntax

This commit is contained in:
Kovid Goyal 2011-07-15 15:09:41 -06:00
parent b97dfea4e7
commit 5c9c46382f
2 changed files with 10 additions and 16 deletions

View File

@ -1,5 +1,5 @@
" Project wide builtins " Project wide builtins
let g:pyflakes_builtins += ["dynamic_property", "__", "P", "I", "lopen", "icu_lower", "icu_upper", "icu_title", "ngettext"] let g:pyflakes_builtins = ["_", "dynamic_property", "__", "P", "I", "lopen", "icu_lower", "icu_upper", "icu_title", "ngettext"]
python << EOFPY python << EOFPY
import os import os

View File

@ -25,18 +25,11 @@ class Message:
return '%s:%s: %s'%(self.filename, self.lineno, self.msg) return '%s:%s: %s'%(self.filename, self.lineno, self.msg)
def check_for_python_errors(code_string, filename): def check_for_python_errors(code_string, filename):
# Since compiler.parse does not reliably report syntax errors, use the import _ast
# built in compiler first to detect those. # First, compile into an AST and handle syntax errors.
try: try:
try: tree = compile(code_string, filename, "exec", _ast.PyCF_ONLY_AST)
compile(code_string, filename, "exec") except (SyntaxError, IndentationError) as value:
except MemoryError:
# Python 2.4 will raise MemoryError if the source can't be
# decoded.
if sys.version_info[:2] == (2, 4):
raise SyntaxError(None)
raise
except (SyntaxError, IndentationError), value:
msg = value.args[0] msg = value.args[0]
(lineno, offset, text) = value.lineno, value.offset, value.text (lineno, offset, text) = value.lineno, value.offset, value.text
@ -47,15 +40,16 @@ def check_for_python_errors(code_string, filename):
# bogus message that claims the encoding the file declared was # bogus message that claims the encoding the file declared was
# unknown. # unknown.
msg = "%s: problem decoding source" % filename msg = "%s: problem decoding source" % filename
return [Message(filename, lineno, msg)] return [Message(filename, lineno, msg)]
else: else:
# Okay, it's syntactically valid. Now parse it into an ast and check
# it.
import compiler
checker = __import__('pyflakes.checker').checker checker = __import__('pyflakes.checker').checker
tree = compiler.parse(code_string) # Okay, it's syntactically valid. Now check it.
w = checker.Checker(tree, filename) w = checker.Checker(tree, filename)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno)) w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
for warning in w.messages:
print warning
print (dir(warning))
return [Message(x.filename, x.lineno, x.message%x.message_args) for x in return [Message(x.filename, x.lineno, x.message%x.message_args) for x in
w.messages] w.messages]