blob: abcbf046e2cc22462a142f12809cec84f0e508ed [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):
Batuhan Taskayaa698d522021-01-21 00:38:47 +030010 def test_EOF_single_quote(self):
11 expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
12 for quote in ("'", "\""):
13 try:
14 eval(f"""{quote}this is a test\
15 """)
16 except SyntaxError as msg:
17 self.assertEqual(str(msg), expect)
18 self.assertEqual(msg.offset, 1)
19 else:
20 raise support.TestFailed
Skip Montanaro8c913372002-08-15 01:28:54 +000021
22 def test_EOFS(self):
Batuhan Taskayaa698d522021-01-21 00:38:47 +030023 expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)")
Skip Montanaro8c913372002-08-15 01:28:54 +000024 try:
25 eval("""'''this is a test""")
Guido van Rossumb940e112007-01-10 16:19:56 +000026 except SyntaxError as msg:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027 self.assertEqual(str(msg), expect)
Batuhan Taskayaa698d522021-01-21 00:38:47 +030028 self.assertEqual(msg.offset, 1)
Skip Montanaro8c913372002-08-15 01:28:54 +000029 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000030 raise support.TestFailed
Skip Montanaro8c913372002-08-15 01:28:54 +000031
Miss Islington (bot)d03f3422021-06-12 13:27:02 -070032 def test_EOFS_with_file(self):
33 expect = ("(<string>, line 1)")
34 with os_helper.temp_dir() as temp_dir:
35 file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""")
36 rc, out, err = script_helper.assert_python_failure(file_name)
37 self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err)
38
Pablo Galindodb9163c2020-05-08 03:38:44 +010039 def test_eof_with_line_continuation(self):
40 expect = "unexpected EOF while parsing (<string>, line 1)"
41 try:
42 compile('"\\xhh" \\', '<string>', 'exec', dont_inherit=True)
43 except SyntaxError as msg:
44 self.assertEqual(str(msg), expect)
45 else:
46 raise support.TestFailed
47
Anthony Sottileabea73b2019-05-18 11:27:17 -070048 def test_line_continuation_EOF(self):
Daniel Hahler2f65aa42020-01-09 18:07:32 +010049 """A continuation at the end of input must be an error; bpo2180."""
Anthony Sottileabea73b2019-05-18 11:27:17 -070050 expect = 'unexpected EOF while parsing (<string>, line 1)'
51 with self.assertRaises(SyntaxError) as excinfo:
52 exec('x = 5\\')
53 self.assertEqual(str(excinfo.exception), expect)
54 with self.assertRaises(SyntaxError) as excinfo:
55 exec('\\')
56 self.assertEqual(str(excinfo.exception), expect)
57
58 @unittest.skipIf(not sys.executable, "sys.executable required")
59 def test_line_continuation_EOF_from_file_bpo2180(self):
60 """Ensure tok_nextc() does not add too many ending newlines."""
Hai Shideb01622020-07-06 20:29:49 +080061 with os_helper.temp_dir() as temp_dir:
Anthony Sottileabea73b2019-05-18 11:27:17 -070062 file_name = script_helper.make_script(temp_dir, 'foo', '\\')
63 rc, out, err = script_helper.assert_python_failure(file_name)
64 self.assertIn(b'unexpected EOF while parsing', err)
Pablo Galindo261a4522021-03-28 23:48:05 +010065 self.assertIn(b'line 1', err)
Lysandros Nikolaou113e2b02020-06-16 03:27:33 +030066 self.assertIn(b'\\', err)
Anthony Sottileabea73b2019-05-18 11:27:17 -070067
68 file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
69 rc, out, err = script_helper.assert_python_failure(file_name)
70 self.assertIn(b'unexpected EOF while parsing', err)
Pablo Galindo261a4522021-03-28 23:48:05 +010071 self.assertIn(b'line 1', err)
Lysandros Nikolaou113e2b02020-06-16 03:27:33 +030072 self.assertIn(b'y = 6\\', err)
Anthony Sottileabea73b2019-05-18 11:27:17 -070073
Skip Montanaro8c913372002-08-15 01:28:54 +000074if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050075 unittest.main()