summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2020-09-11 13:07:38 -0700
committerAnthony Sottile <asottile@umich.edu>2020-09-11 13:07:38 -0700
commit2ed2baa404d9a4711b9bcc3105683cbd8679d0f8 (patch)
tree82a58310ab85dae929a5f1264b3208616169fd50
parent6a56d22bfa7502ea1b391a6266cbd8d2963a3a1e (diff)
downloadpep8-2ed2baa404d9a4711b9bcc3105683cbd8679d0f8.tar.gz
fix skipping of physical checks when file does not end in newline
-rwxr-xr-xpycodestyle.py19
-rw-r--r--testsuite/W29.py5
-rw-r--r--testsuite/W39.py2
3 files changed, 20 insertions, 6 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index deb4539..88eb4d7 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -264,7 +264,7 @@ def trailing_blank_lines(physical_line, lines, line_number, total_lines):
However the last line should end with a new line (warning W292).
"""
if line_number == total_lines:
- stripped_last_line = physical_line.rstrip()
+ stripped_last_line = physical_line.rstrip('\r\n')
if physical_line and not stripped_last_line:
return 0, "W391 blank line at end of file"
if stripped_last_line == physical_line:
@@ -2125,21 +2125,30 @@ class Checker(object):
self.report_error(1, 0, 'E902 %s' % self._io_error, readlines)
tokengen = tokenize.generate_tokens(self.readline)
try:
+ prev_physical = ''
for token in tokengen:
if token[2][0] > self.total_lines:
return
self.noqa = token[4] and noqa(token[4])
- self.maybe_check_physical(token)
+ self.maybe_check_physical(token, prev_physical)
yield token
+ prev_physical = token[4]
except (SyntaxError, tokenize.TokenError):
self.report_invalid_syntax()
- def maybe_check_physical(self, token):
+ def maybe_check_physical(self, token, prev_physical):
"""If appropriate for token, check current physical line(s)."""
# Called after every token, but act only on end of line.
+
+ # a newline token ends a single physical line.
if _is_eol_token(token):
- # Obviously, a newline token ends a single physical line.
- self.check_physical(token[4])
+ # if the file does not end with a newline, the NEWLINE
+ # token is inserted by the parser, but it does not contain
+ # the previous physical line in `token[4]`
+ if token[4] == '':
+ self.check_physical(prev_physical)
+ else:
+ self.check_physical(token[4])
elif token[0] == tokenize.STRING and '\n' in token[1]:
# Less obviously, a string that contains newlines is a
# multiline string, either triple-quoted or with internal
diff --git a/testsuite/W29.py b/testsuite/W29.py
index 4050c2f..e9ad580 100644
--- a/testsuite/W29.py
+++ b/testsuite/W29.py
@@ -9,6 +9,11 @@ class Foo(object):
#: W291:2:35
'''multiline
string with trailing whitespace'''
+#: W291 W292 noeol
+x = 1
+#: W191 W292 noeol
+if False:
+ pass # indented with tabs
#: W292:1:36 noeol
# This line doesn't have a linefeed
#: W292:1:5 E225:1:2 noeol
diff --git a/testsuite/W39.py b/testsuite/W39.py
index 68f886b..25e05b0 100644
--- a/testsuite/W39.py
+++ b/testsuite/W39.py
@@ -5,7 +5,7 @@
# Two additional empty lines
-#: W391:4:1 W293:3:1 W293:4:1 noeol
+#: W292:4:5 W293:3:1 W293:4:1 noeol
# The last lines contain space