Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 1 | """test script for a few new invalid token catches""" |
| 2 | |
Anthony Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 3 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Hai Shi | deb0162 | 2020-07-06 20:29:49 +0800 | [diff] [blame] | 5 | from test.support import os_helper |
Anthony Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 6 | from test.support import script_helper |
| 7 | import unittest |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 8 | |
| 9 | class EOFTestCase(unittest.TestCase): |
| 10 | def test_EOFC(self): |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 11 | expect = "EOL while scanning string literal (<string>, line 1)" |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 12 | try: |
| 13 | eval("""'this is a test\ |
| 14 | """) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 15 | except SyntaxError as msg: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 16 | self.assertEqual(str(msg), expect) |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 17 | else: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | raise support.TestFailed |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 19 | |
| 20 | def test_EOFS(self): |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 21 | expect = ("EOF while scanning triple-quoted string literal " |
| 22 | "(<string>, line 1)") |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 23 | try: |
| 24 | eval("""'''this is a test""") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 25 | except SyntaxError as msg: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 26 | self.assertEqual(str(msg), expect) |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 27 | else: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 28 | raise support.TestFailed |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 29 | |
Pablo Galindo | db9163c | 2020-05-08 03:38:44 +0100 | [diff] [blame] | 30 | 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 Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 39 | def test_line_continuation_EOF(self): |
Daniel Hahler | 2f65aa4 | 2020-01-09 18:07:32 +0100 | [diff] [blame] | 40 | """A continuation at the end of input must be an error; bpo2180.""" |
Anthony Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 41 | 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 Shi | deb0162 | 2020-07-06 20:29:49 +0800 | [diff] [blame] | 52 | with os_helper.temp_dir() as temp_dir: |
Anthony Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 53 | 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 Nikolaou | 113e2b0 | 2020-06-16 03:27:33 +0300 | [diff] [blame] | 56 | self.assertIn(b'line 2', err) |
| 57 | self.assertIn(b'\\', err) |
Anthony Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 58 | |
| 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 Nikolaou | 113e2b0 | 2020-06-16 03:27:33 +0300 | [diff] [blame] | 62 | self.assertIn(b'line 2', err) |
| 63 | self.assertIn(b'y = 6\\', err) |
Anthony Sottile | abea73b | 2019-05-18 11:27:17 -0700 | [diff] [blame] | 64 | |
Skip Montanaro | 8c91337 | 2002-08-15 01:28:54 +0000 | [diff] [blame] | 65 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 66 | unittest.main() |