| 1 | """test script for a few new invalid token catches""" |
| 2 | |
| 3 | import sys |
| 4 | from test import support |
| 5 | from test.support import os_helper |
| 6 | from test.support import script_helper |
| 7 | import unittest |
| 8 | |
| 9 | if sys.implementation.name == 'pypy': |
| 10 | eof_msg = "unexpected end of file (EOF)" |
| 11 | else: |
| 12 | eof_msg = "unexpected EOF" |
| 13 | |
| 14 | class EOFTestCase(unittest.TestCase): |
| 15 | def test_EOF_single_quote(self): |
| 16 | expect = "unterminated string literal (detected at line 1) (<string>, line 1)" |
| 17 | for quote in ("'", "\""): |
| 18 | try: |
| 19 | eval(f"""{quote}this is a test\ |
| 20 | """) |
| 21 | except SyntaxError as msg: |
| 22 | self.assertEqual(str(msg), expect) |
| 23 | self.assertEqual(msg.offset, 1) |
| 24 | else: |
| 25 | raise support.TestFailed |
| 26 | |
| 27 | def test_EOFS(self): |
| 28 | expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)") |
| 29 | try: |
| 30 | eval("""'''this is a test""") |
| 31 | except SyntaxError as msg: |
| 32 | self.assertEqual(str(msg), expect) |
| 33 | self.assertEqual(msg.offset, 1) |
| 34 | else: |
| 35 | raise support.TestFailed |
| 36 | |
| 37 | def test_EOFS_with_file(self): |
| 38 | expect = ("(<string>, line 1)") |
| 39 | with os_helper.temp_dir() as temp_dir: |
| 40 | file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""") |
| 41 | rc, out, err = script_helper.assert_python_failure(file_name) |
| 42 | self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err) |
| 43 | |
| 44 | def test_eof_with_line_continuation(self): |
| 45 | if sys.implementation.name == 'pypy': |
| 46 | expect = f"{eof_msg} in multi-line statement (<string>, line 1)" |
| 47 | else: |
| 48 | expect = f"{eof_msg} while parsing (<string>, line 1)" |
| 49 | try: |
| 50 | compile('"\\xhh" \\', '<string>', 'exec', dont_inherit=True) |
| 51 | except SyntaxError as msg: |
| 52 | self.assertEqual(str(msg), expect) |
| 53 | else: |
| 54 | raise support.TestFailed |
| 55 | |
| 56 | def test_line_continuation_EOF(self): |
| 57 | """A continuation at the end of input must be an error; bpo2180.""" |
| 58 | if sys.implementation.name == 'pypy': |
| 59 | expect = f"{eof_msg} in multi-line statement (<string>, line 1)" |
| 60 | else: |
| 61 | expect = f"{eof_msg} while parsing (<string>, line 1)" |
| 62 | with self.assertRaises(SyntaxError) as excinfo: |
| 63 | exec('x = 5\\') |
| 64 | self.assertEqual(str(excinfo.exception), expect) |
| 65 | with self.assertRaises(SyntaxError) as excinfo: |
| 66 | exec('\\') |
| 67 | self.assertEqual(str(excinfo.exception), expect) |
| 68 | |
| 69 | @unittest.skipIf(not sys.executable, "sys.executable required") |
| 70 | def test_line_continuation_EOF_from_file_bpo2180(self): |
| 71 | """Ensure tok_nextc() does not add too many ending newlines.""" |
| 72 | if sys.implementation.name == 'pypy': |
| 73 | expect = f'{eof_msg} in multi-line statement'.encode() |
| 74 | else: |
| 75 | expect = f'{eof_msg} while parsing'.encode() |
| 76 | with os_helper.temp_dir() as temp_dir: |
| 77 | file_name = script_helper.make_script(temp_dir, 'foo', '\\') |
| 78 | rc, out, err = script_helper.assert_python_failure(file_name) |
| 79 | self.assertIn(expect, err) |
| 80 | self.assertIn(b'line 1', err) |
| 81 | self.assertIn(b'\\', err) |
| 82 | |
| 83 | file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\') |
| 84 | rc, out, err = script_helper.assert_python_failure(file_name) |
| 85 | self.assertIn(expect, err) |
| 86 | self.assertIn(b'line 1', err) |
| 87 | self.assertIn(b'y = 6\\', err) |
| 88 | |
| 89 | if __name__ == "__main__": |
| 90 | unittest.main() |