Skip to content

Commit 99b8b4d

Browse files
committed
Check physical lines after tokenizing them.
This will make it possible to treat physical lines in multiline strings distinctly, e.g. for issue #224 and #242 (not addressed yet; this is just a prerequisite). Details: - readline_check_physical() is gone: now we only need readline() - but now setting self.indent_char in check_physical() comes too late, so move that side effect up to readline() - add maybe_check_physical() to decide after every token if it's time to check physical lines and, if so, do it
1 parent 6fa11a1 commit 99b8b4d

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

pep8.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,16 +1235,9 @@ def readline(self):
12351235
self.line_number += 1
12361236
if self.line_number > len(self.lines):
12371237
return ''
1238-
return self.lines[self.line_number - 1]
1239-
1240-
def readline_check_physical(self):
1241-
"""
1242-
Check and return the next physical line. This method can be
1243-
used to feed tokenize.generate_tokens.
1244-
"""
1245-
line = self.readline()
1246-
if line:
1247-
self.check_physical(line)
1238+
line = self.lines[self.line_number - 1]
1239+
if self.indent_char is None and line[:1] in WHITESPACE:
1240+
self.indent_char = line[0]
12481241
return line
12491242

12501243
def run_check(self, check, argument_names):
@@ -1256,18 +1249,16 @@ def run_check(self, check, argument_names):
12561249
arguments.append(getattr(self, name))
12571250
return check(*arguments)
12581251

1259-
def check_physical(self, line):
1252+
def check_physical(self, line_number, line):
12601253
"""
12611254
Run all physical checks on a raw input line.
12621255
"""
12631256
self.physical_line = line
1264-
if self.indent_char is None and line[:1] in WHITESPACE:
1265-
self.indent_char = line[0]
12661257
for name, check, argument_names in self._physical_checks:
12671258
result = self.run_check(check, argument_names)
12681259
if result is not None:
12691260
offset, text = result
1270-
self.report_error(self.line_number, offset, text, check)
1261+
self.report_error(line_number, offset, text, check)
12711262

12721263
def build_tokens_line(self):
12731264
"""
@@ -1350,13 +1341,30 @@ def check_ast(self):
13501341
def generate_tokens(self):
13511342
if self._io_error:
13521343
self.report_error(1, 0, 'E902 %s' % self._io_error, readlines)
1353-
tokengen = tokenize.generate_tokens(self.readline_check_physical)
1344+
tokengen = tokenize.generate_tokens(self.readline)
13541345
try:
13551346
for token in tokengen:
13561347
yield token
1348+
self.maybe_check_physical(token)
13571349
except (SyntaxError, tokenize.TokenError):
13581350
self.report_invalid_syntax()
13591351

1352+
def maybe_check_physical(self, token):
1353+
"""
1354+
If token calls for it, check current physical line(s).
1355+
"""
1356+
if token[0] == tokenize.STRING and token[1].count('\n'):
1357+
# Check the physical lines that make up a multiline string. Do
1358+
# *not* check the last line: its newline is outside of the
1359+
# multiline string, so we consider it a regular physical line
1360+
# (it will be checked when we see the newline token).
1361+
line_number = token[2][0]
1362+
for line in token[1].split('\n')[:-1]:
1363+
self.check_physical(line_number, line + '\n')
1364+
line_number += 1
1365+
elif token[0] in (tokenize.NEWLINE, tokenize.NL):
1366+
self.check_physical(self.line_number, token[4])
1367+
13601368
def check_all(self, expected=None, line_offset=0):
13611369
"""
13621370
Run all checks on the input file.

0 commit comments

Comments
 (0)