blob: a091ceaa25bc4fc2b67a76c1fbd38951e5b5c658 [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
Anthony Sottileabea73b2019-05-18 11:27:17 -070029 def test_line_continuation_EOF(self):
30 """A contination 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
Skip Montanaro8c913372002-08-15 01:28:54 +000051if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050052 unittest.main()