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

Side by Side Diff: Lib/test/test_source_encoding.py

Issue 26581: Double coding cookie
Patch Set: Created 4 years, 2 months 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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_importlib/source/test_source_encoding.py ('k') | Lib/tokenize.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: koi8-r -*- 1 # -*- coding: koi8-r -*-
2 2
3 import unittest 3 import unittest
4 from test.support import TESTFN, unlink, unload, rmtree 4 from test.support import TESTFN, unlink, unload, rmtree, script_helper, captured _stdout
5 import importlib 5 import importlib
6 import os 6 import os
7 import sys 7 import sys
8 import subprocess 8 import subprocess
9 import tempfile
9 10
10 class SourceEncodingTest(unittest.TestCase): 11 class SourceEncodingTest(unittest.TestCase):
11 12
12 def test_pep263(self): 13 def test_pep263(self):
13 self.assertEqual( 14 self.assertEqual(
14 "ðÉÔÏÎ".encode("utf-8"), 15 "ðÉÔÏÎ".encode("utf-8"),
15 b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd' 16 b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
16 ) 17 )
17 self.assertEqual( 18 self.assertEqual(
18 "\ð".encode("utf-8"), 19 "\ð".encode("utf-8"),
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 134
134 def test_error_from_string(self): 135 def test_error_from_string(self):
135 # See http://bugs.python.org/issue6289 136 # See http://bugs.python.org/issue6289
136 input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8') 137 input = "# coding: ascii\n\N{SNOWMAN}".encode('utf-8')
137 with self.assertRaises(SyntaxError) as c: 138 with self.assertRaises(SyntaxError) as c:
138 compile(input, "<string>", "exec") 139 compile(input, "<string>", "exec")
139 expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \ 140 expected = "'ascii' codec can't decode byte 0xe2 in position 16: " \
140 "ordinal not in range(128)" 141 "ordinal not in range(128)"
141 self.assertTrue(c.exception.args[0].startswith(expected), 142 self.assertTrue(c.exception.args[0].startswith(expected),
142 msg=c.exception.args[0]) 143 msg=c.exception.args[0])
144
145
146 class AbstractSourceEncodingTest:
147
148 def test_first_coding_line(self):
149 src = (b'#coding:iso8859-15\n'
150 b'print(ascii("\xc3\xa4"))\n')
151 out = self.run_script(src)
152 self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
153
154 def test_second_coding_line(self):
155 src = (b'#\n'
156 b'#coding:iso8859-15\n'
157 b'print(ascii("\xc3\xa4"))\n')
158 out = self.run_script(src)
159 self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
160
161 def test_double_coding_same_line(self):
162 src = (b'#coding:iso8859-15 coding:latin1\n'
163 b'print(ascii("\xc3\xa4"))\n')
164 out = self.run_script(src)
165 self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
166
167 def test_double_coding_line(self):
168 src = (b'#coding:iso8859-15\n'
169 b'#coding:latin1\n'
170 b'print(ascii("\xc3\xa4"))\n')
171 out = self.run_script(src)
172 self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
173
174 def test_first_non_utf8_coding_line(self):
175 src = (b'#coding:iso-8859-15 \xa4\n'
176 b'print(ascii("\xc3\xa4"))\n')
177 out = self.run_script(src)
178 self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
179
180 def test_second_non_utf8_coding_line(self):
181 src = (b'\n'
182 b'#coding:iso-8859-15 \xa4\n'
183 b'print(ascii("\xc3\xa4"))\n')
184 out = self.run_script(src)
185 self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
186
187 class StringSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
188
189 def run_script(self, src):
190 with captured_stdout() as stdout:
191 exec(src)
192 return stdout.getvalue().encode()
193
194 class FileSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
195
196 def run_script(self, src):
197 with tempfile.TemporaryDirectory() as tmpd:
198 fn = os.path.join(tmpd, "test.py")
199 with open(fn, "wb") as fp:
200 fp.write(src)
201 res = script_helper.assert_python_ok(fn)
202 return res.out
143 203
144 204
145 if __name__ == "__main__": 205 if __name__ == "__main__":
146 unittest.main() 206 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_importlib/source/test_source_encoding.py ('k') | Lib/tokenize.py » ('j') | no next file with comments »

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