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

Unified Diff: Lib/test/test_source_encoding.py

Issue 26581: Double coding cookie
Patch Set: Created 4 years, 2 months ago
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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_source_encoding.py Fri Mar 18 03:03:10 2016 +0000
+++ b/Lib/test/test_source_encoding.py Thu Mar 17 13:49:41 2016 +0200
@@ -1,11 +1,12 @@
# -*- coding: koi8-r -*-
import unittest
-from test.support import TESTFN, unlink, unload, rmtree
+from test.support import TESTFN, unlink, unload, rmtree, script_helper, captured_stdout
import importlib
import os
import sys
import subprocess
+import tempfile
class SourceEncodingTest(unittest.TestCase):
@@ -142,5 +143,64 @@ class SourceEncodingTest(unittest.TestCa
msg=c.exception.args[0])
+class AbstractSourceEncodingTest:
+
+ def test_first_coding_line(self):
+ src = (b'#coding:iso8859-15\n'
+ b'print(ascii("\xc3\xa4"))\n')
+ out = self.run_script(src)
+ self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
+
+ def test_second_coding_line(self):
+ src = (b'#\n'
+ b'#coding:iso8859-15\n'
+ b'print(ascii("\xc3\xa4"))\n')
+ out = self.run_script(src)
+ self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
+
+ def test_double_coding_same_line(self):
+ src = (b'#coding:iso8859-15 coding:latin1\n'
+ b'print(ascii("\xc3\xa4"))\n')
+ out = self.run_script(src)
+ self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
+
+ def test_double_coding_line(self):
+ src = (b'#coding:iso8859-15\n'
+ b'#coding:latin1\n'
+ b'print(ascii("\xc3\xa4"))\n')
+ out = self.run_script(src)
+ self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
+
+ def test_first_non_utf8_coding_line(self):
+ src = (b'#coding:iso-8859-15 \xa4\n'
+ b'print(ascii("\xc3\xa4"))\n')
+ out = self.run_script(src)
+ self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
+
+ def test_second_non_utf8_coding_line(self):
+ src = (b'\n'
+ b'#coding:iso-8859-15 \xa4\n'
+ b'print(ascii("\xc3\xa4"))\n')
+ out = self.run_script(src)
+ self.assertEqual(out.rstrip(), br"'\xc3\u20ac'")
+
+class StringSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
+
+ def run_script(self, src):
+ with captured_stdout() as stdout:
+ exec(src)
+ return stdout.getvalue().encode()
+
+class FileSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):
+
+ def run_script(self, src):
+ with tempfile.TemporaryDirectory() as tmpd:
+ fn = os.path.join(tmpd, "test.py")
+ with open(fn, "wb") as fp:
+ fp.write(src)
+ res = script_helper.assert_python_ok(fn)
+ return res.out
+
+
if __name__ == "__main__":
unittest.main()
« 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+