@@ -264,7 +264,7 @@ def trailing_blank_lines(physical_line, lines, line_number, total_lines):
264264 However the last line should end with a new line (warning W292).
265265 """
266266 if line_number == total_lines :
267- stripped_last_line = physical_line .rstrip ()
267+ stripped_last_line = physical_line .rstrip (' \r \n ' )
268268 if physical_line and not stripped_last_line :
269269 return 0 , "W391 blank line at end of file"
270270 if stripped_last_line == physical_line :
@@ -2125,21 +2125,30 @@ def generate_tokens(self):
21252125 self .report_error (1 , 0 , 'E902 %s' % self ._io_error , readlines )
21262126 tokengen = tokenize .generate_tokens (self .readline )
21272127 try :
2128+ prev_physical = ''
21282129 for token in tokengen :
21292130 if token [2 ][0 ] > self .total_lines :
21302131 return
21312132 self .noqa = token [4 ] and noqa (token [4 ])
2132- self .maybe_check_physical (token )
2133+ self .maybe_check_physical (token , prev_physical )
21332134 yield token
2135+ prev_physical = token [4 ]
21342136 except (SyntaxError , tokenize .TokenError ):
21352137 self .report_invalid_syntax ()
21362138
2137- def maybe_check_physical (self , token ):
2139+ def maybe_check_physical (self , token , prev_physical ):
21382140 """If appropriate for token, check current physical line(s)."""
21392141 # Called after every token, but act only on end of line.
2142+
2143+ # a newline token ends a single physical line.
21402144 if _is_eol_token (token ):
2141- # Obviously, a newline token ends a single physical line.
2142- self .check_physical (token [4 ])
2145+ # if the file does not end with a newline, the NEWLINE
2146+ # token is inserted by the parser, but it does not contain
2147+ # the previous physical line in `token[4]`
2148+ if token [4 ] == '' :
2149+ self .check_physical (prev_physical )
2150+ else :
2151+ self .check_physical (token [4 ])
21432152 elif token [0 ] == tokenize .STRING and '\n ' in token [1 ]:
21442153 # Less obviously, a string that contains newlines is a
21452154 # multiline string, either triple-quoted or with internal
0 commit comments