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