Debian Codegraph @

Lib/test/test_eof.py

raw
1 """test script for a few new invalid token catches"""
2
3 import sys
4 from codecs import BOM_UTF8
5 from test.support import force_not_colorized
6 from test.support import os_helper
7 from test.support import script_helper
8 from test.support import warnings_helper
9 import unittest
10
11 class EOFTestCase(unittest.TestCase):
12 def test_EOF_single_quote(self):
13 expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
14 for quote in ("'", "\""):
15 with self.assertRaises(SyntaxError) as cm:
16 eval(f"""{quote}this is a test\
17 """)
18 self.assertEqual(str(cm.exception), expect)
19 self.assertEqual(cm.exception.offset, 1)
20
21 def test_EOFS(self):
22 expect = ("unterminated triple-quoted string literal (detected at line 3) (<string>, line 1)")
23 with self.assertRaises(SyntaxError) as cm:
24 eval("""ä = '''thîs is \na \ntest""")
25 self.assertEqual(str(cm.exception), expect)
26 self.assertEqual(cm.exception.text, "ä = '''thîs is ")
27 self.assertEqual(cm.exception.offset, 5)
28
29 with self.assertRaises(SyntaxError) as cm:
30 eval("""ä = '''thîs is \na \ntest""".encode())
31 self.assertEqual(str(cm.exception), expect)
32 self.assertEqual(cm.exception.text, "ä = '''thîs is ")
33 self.assertEqual(cm.exception.offset, 5)
34
35 with self.assertRaises(SyntaxError) as cm:
36 eval(BOM_UTF8 + """ä = '''thîs is \na \ntest""".encode())
37 self.assertEqual(str(cm.exception), expect)
38 self.assertEqual(cm.exception.text, "ä = '''thîs is ")
39 self.assertEqual(cm.exception.offset, 5)
40
41 with self.assertRaises(SyntaxError) as cm:
42 eval("""# coding: latin1\nä = '''thîs is \na \ntest""".encode('latin1'))
43 self.assertEqual(str(cm.exception), "unterminated triple-quoted string literal (detected at line 4) (<string>, line 2)")
44 self.assertEqual(cm.exception.text, "ä = '''thîs is ")
45 self.assertEqual(cm.exception.offset, 5)
46
47 @force_not_colorized
48 def test_EOFS_with_file(self):
49 expect = ("(<string>, line 1)")
50 with os_helper.temp_dir() as temp_dir:
51 file_name = script_helper.make_script(temp_dir, 'foo',
52 """ä = '''thîs is \na \ntest""")
53 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
54 err = err.decode().splitlines()
55 self.assertEqual(err[-3:], [
56 " ä = '''thîs is ",
57 ' ^',
58 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)'])
59
60 file_name = script_helper.make_script(temp_dir, 'foo',
61 """ä = '''thîs is \na \ntest""".encode())
62 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
63 err = err.decode().splitlines()
64 self.assertEqual(err[-3:], [
65 " ä = '''thîs is ",
66 ' ^',
67 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)'])
68
69 file_name = script_helper.make_script(temp_dir, 'foo',
70 BOM_UTF8 + """ä = '''thîs is \na \ntest""".encode())
71 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
72 err = err.decode().splitlines()
73 self.assertEqual(err[-3:], [
74 " ä = '''thîs is ",
75 ' ^',
76 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)'])
77
78 file_name = script_helper.make_script(temp_dir, 'foo',
79 """# coding: latin1\nä = '''thîs is \na \ntest""".encode('latin1'))
80 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
81 err = err.decode().splitlines()
82 self.assertEqual(err[-3:], [
83 " ä = '''thîs is ",
84 ' ^',
85 'SyntaxError: unterminated triple-quoted string literal (detected at line 4)'])
86
87 @warnings_helper.ignore_warnings(category=SyntaxWarning)
88 def test_eof_with_line_continuation(self):
89 expect = "unexpected EOF while parsing (<string>, line 1)"
90 with self.assertRaises(SyntaxError) as cm:
91 compile('"\\Xhh" \\', '<string>', 'exec')
92 self.assertEqual(str(cm.exception), expect)
93
94 def test_line_continuation_EOF(self):
95 """A continuation at the end of input must be an error; bpo2180."""
96 expect = 'unexpected EOF while parsing (<string>, line 1)'
97 with self.assertRaises(SyntaxError) as cm:
98 exec('ä = 5\\')
99 self.assertEqual(str(cm.exception), expect)
100 self.assertEqual(cm.exception.text, 'ä = 5\\\n')
101 self.assertEqual(cm.exception.offset, 7)
102
103 with self.assertRaises(SyntaxError) as cm:
104 exec('ä = 5\\'.encode())
105 self.assertEqual(str(cm.exception), expect)
106 self.assertEqual(cm.exception.text, 'ä = 5\\\n')
107 self.assertEqual(cm.exception.offset, 7)
108
109 with self.assertRaises(SyntaxError) as cm:
110 exec('# coding:latin1\nä = 5\\'.encode('latin1'))
111 self.assertEqual(str(cm.exception),
112 'unexpected EOF while parsing (<string>, line 2)')
113 self.assertEqual(cm.exception.text, 'ä = 5\\\n')
114 self.assertEqual(cm.exception.offset, 7)
115
116 with self.assertRaises(SyntaxError) as cm:
117 exec(BOM_UTF8 + 'ä = 5\\'.encode())
118 self.assertEqual(str(cm.exception), expect)
119 self.assertEqual(cm.exception.text, 'ä = 5\\\n')
120 self.assertEqual(cm.exception.offset, 7)
121
122 with self.assertRaises(SyntaxError) as cm:
123 exec('\\')
124 self.assertEqual(str(cm.exception), expect)
125
126 @unittest.skipIf(not sys.executable, "sys.executable required")
127 @force_not_colorized
128 def test_line_continuation_EOF_from_file_bpo2180(self):
129 """Ensure tok_nextc() does not add too many ending newlines."""
130 with os_helper.temp_dir() as temp_dir:
131 file_name = script_helper.make_script(temp_dir, 'foo', '\\')
132 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
133 err = err.decode().splitlines()
134 self.assertEqual(err[-2:], [
135 ' \\',
136 'SyntaxError: unexpected EOF while parsing'])
137 self.assertEqual(err[-3][-8:], ', line 1', err)
138
139 file_name = script_helper.make_script(temp_dir, 'foo', 'ä = 6\\')
140 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
141 err = err.decode().splitlines()
142 self.assertEqual(err[-3:], [
143 ' ä = 6\\',
144 ' ^',
145 'SyntaxError: unexpected EOF while parsing'])
146 self.assertEqual(err[-4][-8:], ', line 1', err)
147
148 file_name = script_helper.make_script(temp_dir, 'foo',
149 '# coding:latin1\n'
150 'ä = 7\\'.encode('latin1'))
151 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
152 err = err.decode().splitlines()
153 self.assertEqual(err[-3:], [
154 ' ä = 7\\',
155 ' ^',
156 'SyntaxError: unexpected EOF while parsing'])
157 self.assertEqual(err[-4][-8:], ', line 2', err)
158
159 file_name = script_helper.make_script(temp_dir, 'foo',
160 BOM_UTF8 + 'ä = 8\\'.encode())
161 rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name)
162 err = err.decode().splitlines()
163 self.assertEqual(err[-3:], [
164 ' ä = 8\\',
165 ' ^',
166 'SyntaxError: unexpected EOF while parsing'])
167 self.assertEqual(err[-4][-8:], ', line 1', err)
168
169
170 if __name__ == "__main__":
171 unittest.main()