(root)/
Python-3.12.0/
Lib/
test/
test_eof.py
       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  from test.support import warnings_helper
       8  import unittest
       9  
      10  class ESC[4;38;5;81mEOFTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      11      def test_EOF_single_quote(self):
      12          expect = "unterminated string literal (detected at line 1) (<string>, line 1)"
      13          for quote in ("'", "\""):
      14              try:
      15                  eval(f"""{quote}this is a test\
      16                  """)
      17              except SyntaxError as msg:
      18                  self.assertEqual(str(msg), expect)
      19                  self.assertEqual(msg.offset, 1)
      20              else:
      21                  raise support.TestFailed
      22  
      23      def test_EOFS(self):
      24          expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)")
      25          try:
      26              eval("""'''this is a test""")
      27          except SyntaxError as msg:
      28              self.assertEqual(str(msg), expect)
      29              self.assertEqual(msg.offset, 1)
      30          else:
      31              raise support.TestFailed
      32  
      33      def test_EOFS_with_file(self):
      34          expect = ("(<string>, line 1)")
      35          with os_helper.temp_dir() as temp_dir:
      36              file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""")
      37              rc, out, err = script_helper.assert_python_failure(file_name)
      38          self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err)
      39  
      40      @warnings_helper.ignore_warnings(category=SyntaxWarning)
      41      def test_eof_with_line_continuation(self):
      42          expect = "unexpected EOF while parsing (<string>, line 1)"
      43          try:
      44              compile('"\\Xhh" \\', '<string>', 'exec')
      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()