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