blob: f8065788cec1d115e92b165c10a7e09264298200 [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
Victor Stinner1def7752020-04-23 03:03:24 +020029 @support.skip_if_new_parser("TODO for PEG -- fails with new parser")
Anthony Sottileabea73b2019-05-18 11:27:17 -070030 def test_line_continuation_EOF(self):
Daniel Hahler2f65aa42020-01-09 18:07:32 +010031 """A continuation at the end of input must be an error; bpo2180."""
Anthony Sottileabea73b2019-05-18 11:27:17 -070032 expect = 'unexpected EOF while parsing (<string>, line 1)'
33 with self.assertRaises(SyntaxError) as excinfo:
34 exec('x = 5\\')
35 self.assertEqual(str(excinfo.exception), expect)
36 with self.assertRaises(SyntaxError) as excinfo:
37 exec('\\')
38 self.assertEqual(str(excinfo.exception), expect)
39
Pablo Galindoc5fc1562020-04-22 23:29:27 +010040 @unittest.skip("TODO for PEG -- fails even with old parser now")
Anthony Sottileabea73b2019-05-18 11:27:17 -070041 @unittest.skipIf(not sys.executable, "sys.executable required")
42 def test_line_continuation_EOF_from_file_bpo2180(self):
43 """Ensure tok_nextc() does not add too many ending newlines."""
44 with support.temp_dir() as temp_dir:
45 file_name = script_helper.make_script(temp_dir, 'foo', '\\')
46 rc, out, err = script_helper.assert_python_failure(file_name)
47 self.assertIn(b'unexpected EOF while parsing', err)
48
49 file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
50 rc, out, err = script_helper.assert_python_failure(file_name)
51 self.assertIn(b'unexpected EOF while parsing', err)
52
Skip Montanaro8c913372002-08-15 01:28:54 +000053if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050054 unittest.main()