Merge lp:~henninge/pocket-lint/bug-776389-pep-0263-encoding into lp:pocket-lint

Proposed by Henning Eggers
Status: Merged
Merged at revision: 379
Proposed branch: lp:~henninge/pocket-lint/bug-776389-pep-0263-encoding
Merge into: lp:pocket-lint
Diff against target: 94 lines (+37/-8)
2 files modified
pocketlint/formatcheck.py (+10/-5)
pocketlint/tests/test_python.py (+27/-3)
To merge this branch: bzr merge lp:~henninge/pocket-lint/bug-776389-pep-0263-encoding
Reviewer Review Type Date Requested Status
Curtis Hovey Pending
Review via email: mp+59888@code.launchpad.net

Description of the change

Fix bug 776389 by applying the regex from PEP 0263 to the first two lines of each file. Store the encoding instead of the old is_utf8 flag.

Currently, no further use is made of this extended information but I guess check_text could verify the encoding of each line now. It could also check if the encoding is valid in the first place.

To post a comment you must log in.
383. By Henning Eggers

Check text first and let it set the encoding.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'pocketlint/formatcheck.py'
--- pocketlint/formatcheck.py 2011-04-18 19:38:52 +0000
+++ pocketlint/formatcheck.py 2011-05-04 14:48:38 +0000
@@ -448,18 +448,21 @@
448class PythonChecker(BaseChecker, AnyTextMixin):448class PythonChecker(BaseChecker, AnyTextMixin):
449 """Check python source code."""449 """Check python source code."""
450450
451 # This regex is taken from PEP 0263.
452 encoding_pattern = re.compile("coding[:=]\s*([-\w.]+)")
453
451 def __init__(self, file_path, text, reporter=None):454 def __init__(self, file_path, text, reporter=None):
452 super(PythonChecker, self).__init__(455 super(PythonChecker, self).__init__(
453 file_path, text, reporter=reporter)456 file_path, text, reporter=reporter)
454 self.is_utf8 = False457 self.encoding = 'ascii'
455458
456 def check(self):459 def check(self):
457 """Check the syntax of the python code."""460 """Check the syntax of the python code."""
458 if self.text == '':461 if self.text == '':
459 return462 return
463 self.check_text()
460 self.check_flakes()464 self.check_flakes()
461 self.check_pep8()465 self.check_pep8()
462 self.check_text()
463466
464 def check_flakes(self):467 def check_flakes(self):
465 """Check compilation and syntax."""468 """Check compilation and syntax."""
@@ -500,8 +503,10 @@
500 """Call each line_method for each line in text."""503 """Call each line_method for each line in text."""
501 for line_no, line in enumerate(self.text.splitlines()):504 for line_no, line in enumerate(self.text.splitlines()):
502 line_no += 1505 line_no += 1
503 if line_no in (1, 2) and '# -*- coding: utf-8 -*-' in line:506 if line_no in (1, 2):
504 self.is_utf8 = True507 match = self.encoding_pattern.search(line)
508 if match:
509 self.encoding = match.group(1).lower()
505 self.check_pdb(line_no, line)510 self.check_pdb(line_no, line)
506 self.check_length(line_no, line)511 self.check_length(line_no, line)
507 self.check_trailing_whitespace(line_no, line)512 self.check_trailing_whitespace(line_no, line)
@@ -518,7 +523,7 @@
518523
519 def check_ascii(self, line_no, line):524 def check_ascii(self, line_no, line):
520 """Check that the line is ascii."""525 """Check that the line is ascii."""
521 if self.is_utf8:526 if self.encoding != 'ascii':
522 return527 return
523 try:528 try:
524 line.encode('ascii')529 line.encode('ascii')
525530
=== modified file 'pocketlint/tests/test_python.py'
--- pocketlint/tests/test_python.py 2011-04-19 16:33:22 +0000
+++ pocketlint/tests/test_python.py 2011-05-04 14:48:38 +0000
@@ -201,13 +201,37 @@
201 self.assertEqual(201 self.assertEqual(
202 [(1, 'Line contains a call to pdb.')], self.reporter.messages)202 [(1, 'Line contains a call to pdb.')], self.reporter.messages)
203203
204 def test_code_is_utf8(self):204 def _test_encoding(self, python, expected_encoding='foo-encoding'):
205 checker = PythonChecker(
206 'bogus', python % dict(encoding=expected_encoding), self.reporter)
207 checker.check_text()
208 self.assertEqual(expected_encoding, checker.encoding)
209
210 def test_pep0263_no_encoding(self):
211 self._test_encoding("# First line\n# Second line\n\n", 'ascii')
212
213 def test_pep0263_encoding_standard_coding(self):
214 self._test_encoding("# coding=%(encoding)s\n")
215
216 def test_pep0263_encoding_standard_encoding(self):
217 self._test_encoding("# encoding=%(encoding)s\n")
218
219 def test_pep0263_encoding_emacs(self):
220 self._test_encoding("# -*- coding: %(encoding)s -*-\n")
221
222 def test_pep0263_encoding_vim(self):
223 self._test_encoding("# vim: set fileencoding=%(encoding)s :\n")
224
225 def test_pep0263_encoding_2nd_line(self):
226 self._test_encoding("# First line\n# coding=%(encoding)s\n\n")
227
228 def test_code_utf8(self):
205 utf8_python = u"a = 'this is utf-8 [\u272a]'"229 utf8_python = u"a = 'this is utf-8 [\u272a]'"
206 checker = PythonChecker('bogus', utf8_python, self.reporter)230 checker = PythonChecker('bogus', utf8_python, self.reporter)
207 checker.is_utf8 = True231 checker.encoding = 'utf-8'
208 checker.check_text()232 checker.check_text()
209233
210 def test_code_ascii_is_not_is_utf8(self):234 def test_code_ascii_is_not_utf8(self):
211 utf8_python = u"a = 'this is utf-8 [\u272a]'"235 utf8_python = u"a = 'this is utf-8 [\u272a]'"
212 checker = PythonChecker('bogus', utf8_python, self.reporter)236 checker = PythonChecker('bogus', utf8_python, self.reporter)
213 checker.check_text()237 checker.check_text()

Subscribers

People subscribed via source and target branches

to all changes: