Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implicit-str-concat-in-sequence: Handling lines with multi-bytes characters - fix #2610 #2611

Merged
merged 1 commit into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pylint/checkers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,20 @@ def process_module(self, module):
self._unicode_literals = "unicode_literals" in module.future_imports

def process_tokens(self, tokens):
for i, (tok_type, token, start, _, _) in enumerate(tokens):
if tok_type == tokenize.STRING:
encoding = "ascii"
for i, (tok_type, token, start, _, line) in enumerate(tokens):
if tok_type == tokenize.ENCODING:
# this is always the first token processed
encoding = token
elif tok_type == tokenize.STRING:
# 'token' is the whole un-parsed token; we can look at the start
# of it to see whether it's a raw or unicode string etc.
self.process_string_token(token, start[0])
next_token = tokens[i + 1] if i + 1 < len(tokens) else None
if encoding != "ascii":
# We convert `tokenize` character count into a byte count,
# to match with astroid `.col_offset`
start = (start[0], len(line[: start[1]].encode(encoding)))
self.string_tokens[start] = (str_eval(token), next_token)

@check_messages(*(MSGS.keys()))
Expand All @@ -618,6 +626,10 @@ def check_for_concatenated_strings(self, iterable_node, iterable_type):
if elt.col_offset < 0:
# This can happen in case of escaped newlines
continue
if (elt.lineno, elt.col_offset) not in self.string_tokens:
# This may happen with Latin1 encoding
# cf. https://github.com/PyCQA/pylint/issues/2610
continue
matching_token, next_token = self.string_tokens[
(elt.lineno, elt.col_offset)
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding: latin_1
#pylint: disable=bad-continuation,invalid-name,missing-docstring

TOTO = ('Caf�', 'Caf�', 'Caf�')
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pylint: disable=bad-continuation,invalid-name,missing-docstring

TOTO = ('Café', 'Café', 'Café')
Empty file.
5 changes: 3 additions & 2 deletions pylint/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,9 @@ def _open_expected_file(self):
def _open_source_file(self):
if self._test_file.base == "invalid_encoded_data":
return open(self._test_file.source)
else:
return io.open(self._test_file.source, encoding="utf8")
if "latin1" in self._test_file.base:
return io.open(self._test_file.source, encoding="latin1")
return io.open(self._test_file.source, encoding="utf8")

def _get_expected(self):
with self._open_source_file() as fobj:
Expand Down