Skip to content

Commit

Permalink
Catch syntax errors with E901; fix E27* to behave consistently with P…
Browse files Browse the repository at this point in the history
…ython3.
  • Loading branch information
florentx committed May 23, 2012
1 parent 7f49967 commit d637af8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changelog
1.0.2 (UNRELEASED)
------------------

* Add E901 for syntax errors. (Issues #63 and #30)

* Add E271, E272, E273 and E274 for extraneous whitespace around
keywords. (Issue #57)

Expand Down
25 changes: 21 additions & 4 deletions pep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
500 line length
600 deprecation
700 statements
900 syntax error
You can add checks to this program by writing plugins. Each plugin is
a simple function that is called for each line of source code, either
Expand Down Expand Up @@ -138,8 +139,8 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number)
OPERATORS = BINARY_OPERATORS | UNARY_OPERATORS
SKIP_TOKENS = frozenset([tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
tokenize.DEDENT, tokenize.NEWLINE])
KEYWORDS = frozenset(keyword.kwlist + ['print'])
E225NOT_KEYWORDS = KEYWORDS - frozenset(['False', 'None', 'True'])
KEYWORDS = (frozenset(keyword.kwlist + ['print']) -
frozenset(['False', 'None', 'True']))
BENCHMARK_KEYS = ('directories', 'files', 'logical lines', 'physical lines')

options = None
Expand Down Expand Up @@ -538,7 +539,7 @@ def missing_whitespace_around_operator(logical_line, tokens):
if prev_text in '}])':
need_space = True
elif prev_type == tokenize.NAME:
if prev_text not in E225NOT_KEYWORDS:
if prev_text not in KEYWORDS:
need_space = True
else:
need_space = True
Expand Down Expand Up @@ -955,6 +956,22 @@ def check_logical(self):
text, check)
self.previous_logical = self.logical_line

def generate_tokens(self):
"""
Check if the syntax is valid.
"""
try:
for token in tokenize.generate_tokens(self.readline_check_physical):
yield token
except (SyntaxError, tokenize.TokenError):
exc_type, exc = sys.exc_info()[:2]
offset = exc.args[1]
if len(offset) > 2:
offset = offset[1:3]
self.report_error(offset[0], offset[1],
'E901 %s: %s' % (exc_type.__name__, exc.args[0]),
self.generate_tokens)

def check_all(self, expected=None, line_offset=0):
"""
Run all checks on the input file.
Expand All @@ -970,7 +987,7 @@ def check_all(self, expected=None, line_offset=0):
self.blank_lines_before_comment = 0
self.tokens = []
parens = 0
for token in tokenize.generate_tokens(self.readline_check_physical):
for token in self.generate_tokens():
if options.verbose >= 3:
if token[2][0] == token[3][0]:
pos = '[%s:%s]' % (token[2][1] or '', token[3][1])
Expand Down
4 changes: 4 additions & 0 deletions testsuite/E27.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
True and False
#: E274
True and False
#: E272
this and False
#: E274
this and False
10 changes: 10 additions & 0 deletions testsuite/E90.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#: E901
}
#: E901
= [x
#: E901 E101 W191
while True:
try:
pass
except:
print 'Whoops'

0 comments on commit d637af8

Please sign in to comment.