Skip to content

Commit 659e40f

Browse files
author
exarkun
committed
Merge pyflakes-syntaxerrors-2885
Author: exarkun Reviewer: jonathanj Fixes: #2885 Use the builtin compile function to check for syntax errors before trying to construct an ast with the compiler package. The compiler package is less well able to report such errors (in particular, on Python 2.4 and older, it entirely misses some syntax errors).
1 parent 5946f55 commit 659e40f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

pyflakes/scripts/pyflakes.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99
checker = __import__('pyflakes.checker').checker
1010

1111
def check(codeString, filename):
12+
# Since compiler.parse does not reliably report syntax errors, use the
13+
# built in compiler first to detect those.
1214
try:
13-
tree = compiler.parse(codeString)
15+
compile(codeString, filename, "exec")
1416
except (SyntaxError, IndentationError), value:
1517
msg = value.args[0]
1618

17-
if not value.lineno:
18-
try:
19-
compile(codeString, filename, 'exec')
20-
except SyntaxError, value:
21-
pass
22-
2319
(lineno, offset, text) = value.lineno, value.offset, value.text
2420

2521
line = text.splitlines()[-1]
@@ -35,6 +31,9 @@ def check(codeString, filename):
3531

3632
return 1
3733
else:
34+
# Okay, it's syntactically valid. Now parse it into an ast and check
35+
# it.
36+
tree = compiler.parse(codeString)
3837
w = checker.Checker(tree, filename)
3938
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
4039
for warning in w.messages:

0 commit comments

Comments
 (0)