• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""test script for a few new invalid token catches"""
2
3import sys
4from test import support
5from test.support import script_helper
6import unittest
7
8class EOFTestCase(unittest.TestCase):
9    def test_EOFC(self):
10        expect = "EOL while scanning string literal (<string>, line 1)"
11        try:
12            eval("""'this is a test\
13            """)
14        except SyntaxError as msg:
15            self.assertEqual(str(msg), expect)
16        else:
17            raise support.TestFailed
18
19    def test_EOFS(self):
20        expect = ("EOF while scanning triple-quoted string literal "
21                  "(<string>, line 1)")
22        try:
23            eval("""'''this is a test""")
24        except SyntaxError as msg:
25            self.assertEqual(str(msg), expect)
26        else:
27            raise support.TestFailed
28
29    def test_line_continuation_EOF(self):
30        """A continuation at the end of input must be an error; bpo2180."""
31        expect = 'unexpected EOF while parsing (<string>, line 1)'
32        with self.assertRaises(SyntaxError) as excinfo:
33            exec('x = 5\\')
34        self.assertEqual(str(excinfo.exception), expect)
35        with self.assertRaises(SyntaxError) as excinfo:
36            exec('\\')
37        self.assertEqual(str(excinfo.exception), expect)
38
39    @unittest.skipIf(not sys.executable, "sys.executable required")
40    def test_line_continuation_EOF_from_file_bpo2180(self):
41        """Ensure tok_nextc() does not add too many ending newlines."""
42        with support.temp_dir() as temp_dir:
43            file_name = script_helper.make_script(temp_dir, 'foo', '\\')
44            rc, out, err = script_helper.assert_python_failure(file_name)
45            self.assertIn(b'unexpected EOF while parsing', err)
46
47            file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
48            rc, out, err = script_helper.assert_python_failure(file_name)
49            self.assertIn(b'unexpected EOF while parsing', err)
50
51if __name__ == "__main__":
52    unittest.main()
53