blob: 51cbbd8eed664fa22b0f526c603f6adea3755e3b [file] [log] [blame]
Skip Montanaro8c913372002-08-15 01:28:54 +00001"""test script for a few new invalid token catches"""
2
Anthony Sottileabea73b2019-05-18 11:27:17 -07003import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Anthony Sottileabea73b2019-05-18 11:27:17 -07005from test.support import script_helper
6import unittest
Skip Montanaro8c913372002-08-15 01:28:54 +00007
8class EOFTestCase(unittest.TestCase):
9 def test_EOFC(self):
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000010 expect = "EOL while scanning string literal (<string>, line 1)"
Skip Montanaro8c913372002-08-15 01:28:54 +000011 try:
12 eval("""'this is a test\
13 """)
Guido van Rossumb940e112007-01-10 16:19:56 +000014 except SyntaxError as msg:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015 self.assertEqual(str(msg), expect)
Skip Montanaro8c913372002-08-15 01:28:54 +000016 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000017 raise support.TestFailed
Skip Montanaro8c913372002-08-15 01:28:54 +000018
19 def test_EOFS(self):
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000020 expect = ("EOF while scanning triple-quoted string literal "
21 "(<string>, line 1)")
Skip Montanaro8c913372002-08-15 01:28:54 +000022 try:
23 eval("""'''this is a test""")
Guido van Rossumb940e112007-01-10 16:19:56 +000024 except SyntaxError as msg:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000025 self.assertEqual(str(msg), expect)
Skip Montanaro8c913372002-08-15 01:28:54 +000026 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000027 raise support.TestFailed
Skip Montanaro8c913372002-08-15 01:28:54 +000028
Pablo Galindodb9163c2020-05-08 03:38:44 +010029 def test_eof_with_line_continuation(self):
30 expect = "unexpected EOF while parsing (<string>, line 1)"
31 try:
32 compile('"\\xhh" \\', '<string>', 'exec', dont_inherit=True)
33 except SyntaxError as msg:
34 self.assertEqual(str(msg), expect)
35 else:
36 raise support.TestFailed
37
Anthony Sottileabea73b2019-05-18 11:27:17 -070038 def test_line_continuation_EOF(self):
Daniel Hahler2f65aa42020-01-09 18:07:32 +010039 """A continuation at the end of input must be an error; bpo2180."""
Anthony Sottileabea73b2019-05-18 11:27:17 -070040 expect = 'unexpected EOF while parsing (<string>, line 1)'
41 with self.assertRaises(SyntaxError) as excinfo:
42 exec('x = 5\\')
43 self.assertEqual(str(excinfo.exception), expect)
44 with self.assertRaises(SyntaxError) as excinfo:
45 exec('\\')
46 self.assertEqual(str(excinfo.exception), expect)
47
48 @unittest.skipIf(not sys.executable, "sys.executable required")
49 def test_line_continuation_EOF_from_file_bpo2180(self):
50 """Ensure tok_nextc() does not add too many ending newlines."""
51 with support.temp_dir() as temp_dir:
52 file_name = script_helper.make_script(temp_dir, 'foo', '\\')
53 rc, out, err = script_helper.assert_python_failure(file_name)
54 self.assertIn(b'unexpected EOF while parsing', err)
Miss Islington (bot)097b8b62020-06-15 17:46:44 -070055 self.assertIn(b'line 2', err)
56 self.assertIn(b'\\', err)
Anthony Sottileabea73b2019-05-18 11:27:17 -070057
58 file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
59 rc, out, err = script_helper.assert_python_failure(file_name)
60 self.assertIn(b'unexpected EOF while parsing', err)
Miss Islington (bot)097b8b62020-06-15 17:46:44 -070061 self.assertIn(b'line 2', err)
62 self.assertIn(b'y = 6\\', err)
Anthony Sottileabea73b2019-05-18 11:27:17 -070063
Skip Montanaro8c913372002-08-15 01:28:54 +000064if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050065 unittest.main()