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
1=== modified file 'pocketlint/formatcheck.py'
2--- pocketlint/formatcheck.py 2011-04-18 19:38:52 +0000
3+++ pocketlint/formatcheck.py 2011-05-04 14:48:38 +0000
4@@ -448,18 +448,21 @@
5 class PythonChecker(BaseChecker, AnyTextMixin):
6 """Check python source code."""
7
8+ # This regex is taken from PEP 0263.
9+ encoding_pattern = re.compile("coding[:=]\s*([-\w.]+)")
10+
11 def __init__(self, file_path, text, reporter=None):
12 super(PythonChecker, self).__init__(
13 file_path, text, reporter=reporter)
14- self.is_utf8 = False
15+ self.encoding = 'ascii'
16
17 def check(self):
18 """Check the syntax of the python code."""
19 if self.text == '':
20 return
21+ self.check_text()
22 self.check_flakes()
23 self.check_pep8()
24- self.check_text()
25
26 def check_flakes(self):
27 """Check compilation and syntax."""
28@@ -500,8 +503,10 @@
29 """Call each line_method for each line in text."""
30 for line_no, line in enumerate(self.text.splitlines()):
31 line_no += 1
32- if line_no in (1, 2) and '# -*- coding: utf-8 -*-' in line:
33- self.is_utf8 = True
34+ if line_no in (1, 2):
35+ match = self.encoding_pattern.search(line)
36+ if match:
37+ self.encoding = match.group(1).lower()
38 self.check_pdb(line_no, line)
39 self.check_length(line_no, line)
40 self.check_trailing_whitespace(line_no, line)
41@@ -518,7 +523,7 @@
42
43 def check_ascii(self, line_no, line):
44 """Check that the line is ascii."""
45- if self.is_utf8:
46+ if self.encoding != 'ascii':
47 return
48 try:
49 line.encode('ascii')
50
51=== modified file 'pocketlint/tests/test_python.py'
52--- pocketlint/tests/test_python.py 2011-04-19 16:33:22 +0000
53+++ pocketlint/tests/test_python.py 2011-05-04 14:48:38 +0000
54@@ -201,13 +201,37 @@
55 self.assertEqual(
56 [(1, 'Line contains a call to pdb.')], self.reporter.messages)
57
58- def test_code_is_utf8(self):
59+ def _test_encoding(self, python, expected_encoding='foo-encoding'):
60+ checker = PythonChecker(
61+ 'bogus', python % dict(encoding=expected_encoding), self.reporter)
62+ checker.check_text()
63+ self.assertEqual(expected_encoding, checker.encoding)
64+
65+ def test_pep0263_no_encoding(self):
66+ self._test_encoding("# First line\n# Second line\n\n", 'ascii')
67+
68+ def test_pep0263_encoding_standard_coding(self):
69+ self._test_encoding("# coding=%(encoding)s\n")
70+
71+ def test_pep0263_encoding_standard_encoding(self):
72+ self._test_encoding("# encoding=%(encoding)s\n")
73+
74+ def test_pep0263_encoding_emacs(self):
75+ self._test_encoding("# -*- coding: %(encoding)s -*-\n")
76+
77+ def test_pep0263_encoding_vim(self):
78+ self._test_encoding("# vim: set fileencoding=%(encoding)s :\n")
79+
80+ def test_pep0263_encoding_2nd_line(self):
81+ self._test_encoding("# First line\n# coding=%(encoding)s\n\n")
82+
83+ def test_code_utf8(self):
84 utf8_python = u"a = 'this is utf-8 [\u272a]'"
85 checker = PythonChecker('bogus', utf8_python, self.reporter)
86- checker.is_utf8 = True
87+ checker.encoding = 'utf-8'
88 checker.check_text()
89
90- def test_code_ascii_is_not_is_utf8(self):
91+ def test_code_ascii_is_not_utf8(self):
92 utf8_python = u"a = 'this is utf-8 [\u272a]'"
93 checker = PythonChecker('bogus', utf8_python, self.reporter)
94 checker.check_text()

Subscribers

People subscribed via source and target branches

to all changes: