diff options
author | Anthony Sottile <asottile@umich.edu> | 2020-09-11 13:07:38 -0700 |
---|---|---|
committer | Anthony Sottile <asottile@umich.edu> | 2020-09-11 13:07:38 -0700 |
commit | 2ed2baa404d9a4711b9bcc3105683cbd8679d0f8 (patch) | |
tree | 82a58310ab85dae929a5f1264b3208616169fd50 /pycodestyle.py | |
parent | 6a56d22bfa7502ea1b391a6266cbd8d2963a3a1e (diff) | |
download | pep8-2ed2baa404d9a4711b9bcc3105683cbd8679d0f8.tar.gz |
fix skipping of physical checks when file does not end in newline
Diffstat (limited to 'pycodestyle.py')
-rwxr-xr-x | pycodestyle.py | 19 |
1 files changed, 14 insertions, 5 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 |