1 """test script for a few new invalid token catches"""
2
3 import sys
4 from test import support
5 from test.support import os_helper
6 from test.support import script_helper
7 import unittest
8
9 class ESC[4;38;5;81mEOFTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
10 def test_EOF_single_quote(self):
11 expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
12 for quote in ("'", "\""):
13 try:
14 eval(f"""{quote}this is a test\
15 """)
16 except SyntaxError as msg:
17 self.assertEqual(str(msg), expect)
18 self.assertEqual(msg.offset, 1)
19 else:
20 raise support.TestFailed
21
22 def test_EOFS(self):
23 expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)")
24 try:
25 eval("""'''this is a test""")
26 except SyntaxError as msg:
27 self.assertEqual(str(msg), expect)
28 self.assertEqual(msg.offset, 1)
29 else:
30 raise support.TestFailed
31
32 def test_EOFS_with_file(self):
33 expect = ("(<string>, line 1)")
34 with os_helper.temp_dir() as temp_dir:
35 file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""")
36 rc, out, err = script_helper.assert_python_failure(file_name)
37 self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err)
38
39 def test_eof_with_line_continuation(self):
40 expect = "unexpected EOF while parsing (<string>, line 1)"
41 try:
42 compile('"\\xhh" \\', '<string>', 'exec', dont_inherit=True)
43 except SyntaxError as msg:
44 self.assertEqual(str(msg), expect)
45 else:
46 raise support.TestFailed
47
48 def test_line_continuation_EOF(self):
49 """A continuation at the end of input must be an error; bpo2180."""
50 expect = 'unexpected EOF while parsing (<string>, line 1)'
51 with self.assertRaises(SyntaxError) as excinfo:
52 exec('x = 5\\')
53 self.assertEqual(str(excinfo.exception), expect)
54 with self.assertRaises(SyntaxError) as excinfo:
55 exec('\\')
56 self.assertEqual(str(excinfo.exception), expect)
57
58 @unittest.skipIf(not sys.executable, "sys.executable required")
59 def test_line_continuation_EOF_from_file_bpo2180(self):
60 """Ensure tok_nextc() does not add too many ending newlines."""
61 with os_helper.temp_dir() as temp_dir:
62 file_name = script_helper.make_script(temp_dir, 'foo', '\\')
63 rc, out, err = script_helper.assert_python_failure(file_name)
64 self.assertIn(b'unexpected EOF while parsing', err)
65 self.assertIn(b'line 1', err)
66 self.assertIn(b'\\', err)
67
68 file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
69 rc, out, err = script_helper.assert_python_failure(file_name)
70 self.assertIn(b'unexpected EOF while parsing', err)
71 self.assertIn(b'line 1', err)
72 self.assertIn(b'y = 6\\', err)
73
74 if __name__ == "__main__":
75 unittest.main()