Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(36976)

Delta Between Two Patch Sets: Lib/test/test_importlib/source/test_source_encoding.py

Issue 18873: "Encoding" detected in non-comment lines
Left Patch Set: Created 6 years ago
Right Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/lib2to3/tests/test_refactor.py ('k') | Lib/test/test_tokenize.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 from . import util as source_util 1 from . import util as source_util
2 2
3 from importlib import _bootstrap 3 from importlib import _bootstrap
4 import codecs 4 import codecs
5 import re 5 import re
6 import sys 6 import sys
7 # Because sys.path gets essentially blanked, need to have unicodedata already 7 # Because sys.path gets essentially blanked, need to have unicodedata already
8 # imported for the parser to use. 8 # imported for the parser to use.
9 import unicodedata 9 import unicodedata
10 import unittest 10 import unittest
11 11
12 12
13 CODING_RE = re.compile(r'coding[:=]\s*([-\w.]+)') 13 CODING_RE = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
14 14
15 15
16 class EncodingTest(unittest.TestCase): 16 class EncodingTest(unittest.TestCase):
17 17
18 """PEP 3120 makes UTF-8 the default encoding for source code 18 """PEP 3120 makes UTF-8 the default encoding for source code
19 [default encoding]. 19 [default encoding].
20 20
21 PEP 263 specifies how that can change on a per-file basis. Either the first 21 PEP 263 specifies how that can change on a per-file basis. Either the first
22 or second line can contain the encoding line [encoding first line] 22 or second line can contain the encoding line [encoding first line]
23 encoding second line]. If the file has the BOM marker it is considered UTF-8 23 encoding second line]. If the file has the BOM marker it is considered UTF-8
(...skipping 10 matching lines...) Expand all
34 def run_test(self, source): 34 def run_test(self, source):
35 with source_util.create_modules(self.module_name) as mapping: 35 with source_util.create_modules(self.module_name) as mapping:
36 with open(mapping[self.module_name], 'wb') as file: 36 with open(mapping[self.module_name], 'wb') as file:
37 file.write(source) 37 file.write(source)
38 loader = _bootstrap.SourceFileLoader(self.module_name, 38 loader = _bootstrap.SourceFileLoader(self.module_name,
39 mapping[self.module_name]) 39 mapping[self.module_name])
40 return loader.load_module(self.module_name) 40 return loader.load_module(self.module_name)
41 41
42 def create_source(self, encoding): 42 def create_source(self, encoding):
43 encoding_line = "# coding={0}".format(encoding) 43 encoding_line = "# coding={0}".format(encoding)
44 assert CODING_RE.search(encoding_line) 44 assert CODING_RE.match(encoding_line)
45 source_lines = [encoding_line.encode('utf-8')] 45 source_lines = [encoding_line.encode('utf-8')]
46 source_lines.append(self.source_line.encode(encoding)) 46 source_lines.append(self.source_line.encode(encoding))
47 return b'\n'.join(source_lines) 47 return b'\n'.join(source_lines)
48 48
49 def test_non_obvious_encoding(self): 49 def test_non_obvious_encoding(self):
50 # Make sure that an encoding that has never been a standard one for 50 # Make sure that an encoding that has never been a standard one for
51 # Python works. 51 # Python works.
52 encoding_line = "# coding=koi8-r" 52 encoding_line = "# coding=koi8-r"
53 assert CODING_RE.search(encoding_line) 53 assert CODING_RE.match(encoding_line)
54 source = "{0}\na=42\n".format(encoding_line).encode("koi8-r") 54 source = "{0}\na=42\n".format(encoding_line).encode("koi8-r")
55 self.run_test(source) 55 self.run_test(source)
56 56
57 # [default encoding] 57 # [default encoding]
58 def test_default_encoding(self): 58 def test_default_encoding(self):
59 self.run_test(self.source_line.encode('utf-8')) 59 self.run_test(self.source_line.encode('utf-8'))
60 60
61 # [encoding first line] 61 # [encoding first line]
62 def test_encoding_on_first_line(self): 62 def test_encoding_on_first_line(self):
63 encoding = 'Latin-1' 63 encoding = 'Latin-1'
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 self.run_test(b'\n') 114 self.run_test(b'\n')
115 115
116 116
117 def test_main(): 117 def test_main():
118 from test.support import run_unittest 118 from test.support import run_unittest
119 run_unittest(EncodingTest, LineEndingTest) 119 run_unittest(EncodingTest, LineEndingTest)
120 120
121 121
122 if __name__ == '__main__': 122 if __name__ == '__main__':
123 test_main() 123 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+