1"""test script for a few new invalid token catches""" 2 3import sys 4from test import support 5from test.support import script_helper 6import unittest 7 8class EOFTestCase(unittest.TestCase): 9 def test_EOFC(self): 10 expect = "EOL while scanning string literal (<string>, line 1)" 11 try: 12 eval("""'this is a test\ 13 """) 14 except SyntaxError as msg: 15 self.assertEqual(str(msg), expect) 16 else: 17 raise support.TestFailed 18 19 def test_EOFS(self): 20 expect = ("EOF while scanning triple-quoted string literal " 21 "(<string>, line 1)") 22 try: 23 eval("""'''this is a test""") 24 except SyntaxError as msg: 25 self.assertEqual(str(msg), expect) 26 else: 27 raise support.TestFailed 28 29 def test_eof_with_line_continuation(self): 30 expect = "unexpected EOF while parsing (<string>, line 1)" 31 try: 32 compile('"\\xhh" \\', '<string>', 'exec', dont_inherit=True) 33 except SyntaxError as msg: 34 self.assertEqual(str(msg), expect) 35 else: 36 raise support.TestFailed 37 38 def test_line_continuation_EOF(self): 39 """A continuation at the end of input must be an error; bpo2180.""" 40 expect = 'unexpected EOF while parsing (<string>, line 1)' 41 with self.assertRaises(SyntaxError) as excinfo: 42 exec('x = 5\\') 43 self.assertEqual(str(excinfo.exception), expect) 44 with self.assertRaises(SyntaxError) as excinfo: 45 exec('\\') 46 self.assertEqual(str(excinfo.exception), expect) 47 48 @unittest.skipIf(not sys.executable, "sys.executable required") 49 def test_line_continuation_EOF_from_file_bpo2180(self): 50 """Ensure tok_nextc() does not add too many ending newlines.""" 51 with support.temp_dir() as temp_dir: 52 file_name = script_helper.make_script(temp_dir, 'foo', '\\') 53 rc, out, err = script_helper.assert_python_failure(file_name) 54 self.assertIn(b'unexpected EOF while parsing', err) 55 self.assertIn(b'line 2', err) 56 self.assertIn(b'\\', err) 57 58 file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\') 59 rc, out, err = script_helper.assert_python_failure(file_name) 60 self.assertIn(b'unexpected EOF while parsing', err) 61 self.assertIn(b'line 2', err) 62 self.assertIn(b'y = 6\\', err) 63 64if __name__ == "__main__": 65 unittest.main() 66