Skip to content

Commit 45c4a3b

Browse files
committed
Fix wrong offset computation when error is on the last char of a physical line; issue #268
1 parent 1d3279b commit 45c4a3b

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Changelog
55
1.5.x (unreleased)
66
------------------
77

8+
Bug fixes:
9+
10+
* Fix wrong offset computation when error is on the last char
11+
of a physical line. (Issue #268)
812

913

1014
1.5.2 (2014-04-04)

pep8.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,13 +1326,13 @@ def build_tokens_line(self):
13261326
fill = self.lines[end_row - 1][end:start]
13271327
logical.append(fill)
13281328
length += len(fill)
1329+
length += len(text)
13291330
mapping.append((length, token))
13301331
logical.append(text)
1331-
length += len(text)
13321332
previous = token
13331333
self.logical_line = ''.join(logical)
13341334
self.noqa = comments and noqa(''.join(comments))
1335-
return mapping or [(0, self.tokens[0])]
1335+
return mapping or [(len(self.tokens[0][1]), self.tokens[0])]
13361336

13371337
def check_logical(self):
13381338
"""Build a line from tokens and run all logical checks on it."""
@@ -1354,10 +1354,10 @@ def check_logical(self):
13541354
(li_number, li_offset) = offset
13551355
else:
13561356
for (token_offset, token) in mapping:
1357-
if offset < token_offset:
1357+
if offset <= token_offset:
13581358
break
1359-
li_number = token[2][0]
1360-
li_offset = (token[2][1] + offset - token_offset)
1359+
li_number = token[3][0]
1360+
li_offset = (token[3][1] + offset - token_offset)
13611361
self.report_error(li_number, li_offset, text, check)
13621362
if self.logical_line:
13631363
self.previous_indent_level = self.indent_level
@@ -1460,8 +1460,10 @@ def check_all(self, expected=None, line_offset=0):
14601460
elif COMMENT_WITH_NL and token_type == tokenize.COMMENT:
14611461
if len(self.tokens) == 1:
14621462
# The comment also ends a physical line
1463-
text = text.rstrip('\r\n')
1464-
self.tokens = [(token_type, text) + token[2:]]
1463+
token = list(token)
1464+
token[1] = text.rstrip('\r\n')
1465+
token[3] = (token[2][0], token[2][1] + len(token[1]))
1466+
self.tokens = [tuple(token)]
14651467
self.check_logical()
14661468
return self.report.get_file_results()
14671469

testsuite/E12.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,10 @@ def example_issue254():
367367
# more stuff
368368
)
369369
)
370+
#: E701:1:8 E122:2:1 E203:4:8 E128:5:1
371+
if True:\
372+
print(True)
373+
374+
print(a
375+
, end=' ')
370376
#:

0 commit comments

Comments
 (0)