blob: 2cf263d27463c4ba8999b7f5c9f519051678c3da [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
Hai Shideb01622020-07-06 20:29:49 +08005from test.support import os_helper
Anthony Sottileabea73b2019-05-18 11:27:17 -07006from test.support import script_helper
7import unittest
Skip Montanaro8c913372002-08-15 01:28:54 +00008
9class EOFTestCase(unittest.TestCase):
10 def test_EOFC(self):
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000011 expect = "EOL while scanning string literal (<string>, line 1)"
Skip Montanaro8c913372002-08-15 01:28:54 +000012 try:
13 eval("""'this is a test\
14 """)
Guido van Rossumb940e112007-01-10 16:19:56 +000015 except SyntaxError as msg:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016 self.assertEqual(str(msg), expect)
Skip Montanaro8c913372002-08-15 01:28:54 +000017 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000018 raise support.TestFailed
Skip Montanaro8c913372002-08-15 01:28:54 +000019
20 def test_EOFS(self):
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000021 expect = ("EOF while scanning triple-quoted string literal "
22 "(<string>, line 1)")
Skip Montanaro8c913372002-08-15 01:28:54 +000023 try:
24 eval("""'''this is a test""")
Guido van Rossumb940e112007-01-10 16:19:56 +000025 except SyntaxError as msg:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026 self.assertEqual(str(msg), expect)
Skip Montanaro8c913372002-08-15 01:28:54 +000027 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000028 raise support.TestFailed
Skip Montanaro8c913372002-08-15 01:28:54 +000029
Pablo Galindodb9163c2020-05-08 03:38:44 +010030 def test_eof_with_line_continuation(self):
31 expect = "unexpected EOF while parsing (<string>, line 1)"
32 try:
33 compile('"\\xhh" \\', '<string>', 'exec', dont_inherit=True)
34 except SyntaxError as msg:
35 self.assertEqual(str(msg), expect)
36 else:
37 raise support.TestFailed
38
Anthony Sottileabea73b2019-05-18 11:27:17 -070039 def test_line_continuation_EOF(self):
Daniel Hahler2f65aa42020-01-09 18:07:32 +010040 """A continuation at the end of input must be an error; bpo2180."""
Anthony Sottileabea73b2019-05-18 11:27:17 -070041 expect = 'unexpected EOF while parsing (<string>, line 1)'
42 with self.assertRaises(SyntaxError) as excinfo:
43 exec('x = 5\\')
44 self.assertEqual(str(excinfo.exception), expect)
45 with self.assertRaises(SyntaxError) as excinfo:
46 exec('\\')
47 self.assertEqual(str(excinfo.exception), expect)
48
49 @unittest.skipIf(not sys.executable, "sys.executable required")
50 def test_line_continuation_EOF_from_file_bpo2180(self):
51 """Ensure tok_nextc() does not add too many ending newlines."""
Hai Shideb01622020-07-06 20:29:49 +080052 with os_helper.temp_dir() as temp_dir:
Anthony Sottileabea73b2019-05-18 11:27:17 -070053 file_name = script_helper.make_script(temp_dir, 'foo', '\\')
54 rc, out, err = script_helper.assert_python_failure(file_name)
55 self.assertIn(b'unexpected EOF while parsing', err)
Lysandros Nikolaou113e2b02020-06-16 03:27:33 +030056 self.assertIn(b'line 2', err)
57 self.assertIn(b'\\', err)
Anthony Sottileabea73b2019-05-18 11:27:17 -070058
59 file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
60 rc, out, err = script_helper.assert_python_failure(file_name)
61 self.assertIn(b'unexpected EOF while parsing', err)
Lysandros Nikolaou113e2b02020-06-16 03:27:33 +030062 self.assertIn(b'line 2', err)
63 self.assertIn(b'y = 6\\', err)
Anthony Sottileabea73b2019-05-18 11:27:17 -070064
Skip Montanaro8c913372002-08-15 01:28:54 +000065if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050066 unittest.main()