@@ -264,7 +264,7 @@ def trailing_blank_lines(physical_line, lines, line_number, total_lines):
264
264
However the last line should end with a new line (warning W292).
265
265
"""
266
266
if line_number == total_lines :
267
- stripped_last_line = physical_line .rstrip ()
267
+ stripped_last_line = physical_line .rstrip (' \r \n ' )
268
268
if physical_line and not stripped_last_line :
269
269
return 0 , "W391 blank line at end of file"
270
270
if stripped_last_line == physical_line :
@@ -2125,21 +2125,30 @@ def generate_tokens(self):
2125
2125
self .report_error (1 , 0 , 'E902 %s' % self ._io_error , readlines )
2126
2126
tokengen = tokenize .generate_tokens (self .readline )
2127
2127
try :
2128
+ prev_physical = ''
2128
2129
for token in tokengen :
2129
2130
if token [2 ][0 ] > self .total_lines :
2130
2131
return
2131
2132
self .noqa = token [4 ] and noqa (token [4 ])
2132
- self .maybe_check_physical (token )
2133
+ self .maybe_check_physical (token , prev_physical )
2133
2134
yield token
2135
+ prev_physical = token [4 ]
2134
2136
except (SyntaxError , tokenize .TokenError ):
2135
2137
self .report_invalid_syntax ()
2136
2138
2137
- def maybe_check_physical (self , token ):
2139
+ def maybe_check_physical (self , token , prev_physical ):
2138
2140
"""If appropriate for token, check current physical line(s)."""
2139
2141
# Called after every token, but act only on end of line.
2142
+
2143
+ # a newline token ends a single physical line.
2140
2144
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 ])
2143
2152
elif token [0 ] == tokenize .STRING and '\n ' in token [1 ]:
2144
2153
# Less obviously, a string that contains newlines is a
2145
2154
# multiline string, either triple-quoted or with internal
0 commit comments