test_eof.py 3.0 KB

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