| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | """test script for a few new invalid token catches""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  | from codecs import BOM_UTF8 | 
					
						
							| 
									
										
										
										
											2025-01-22 21:09:51 +02:00
										 |  |  | from test.support import force_not_colorized | 
					
						
							| 
									
										
										
										
											2020-07-06 20:29:49 +08:00
										 |  |  | from test.support import os_helper | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  | from test.support import script_helper | 
					
						
							| 
									
										
										
										
											2023-04-19 17:18:16 +01:00
										 |  |  | from test.support import warnings_helper | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class EOFTestCase(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2021-01-21 00:38:47 +03:00
										 |  |  |     def test_EOF_single_quote(self): | 
					
						
							|  |  |  |         expect = "unterminated string literal (detected at line 1) (<string>, line 1)" | 
					
						
							|  |  |  |         for quote in ("'", "\""): | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |             with self.assertRaises(SyntaxError) as cm: | 
					
						
							| 
									
										
										
										
											2021-01-21 00:38:47 +03:00
										 |  |  |                 eval(f"""{quote}this is a test\
 | 
					
						
							|  |  |  |                 """)
 | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |             self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |             self.assertEqual(cm.exception.offset, 1) | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_EOFS(self): | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |         expect = ("unterminated triple-quoted string literal (detected at line 3) (<string>, line 1)") | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             eval("""ä = '''thîs is \na \ntest""") | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, "ä = '''thîs is ") | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             eval("""ä = '''thîs is \na \ntest""".encode()) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, "ä = '''thîs is ") | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             eval(BOM_UTF8 + """ä = '''thîs is \na \ntest""".encode()) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, "ä = '''thîs is ") | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             eval("""# coding: latin1\nä = '''thîs is \na \ntest""".encode('latin1')) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), "unterminated triple-quoted string literal (detected at line 4) (<string>, line 2)") | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, "ä = '''thîs is ") | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 5) | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-22 21:09:51 +02:00
										 |  |  |     @force_not_colorized | 
					
						
							| 
									
										
										
										
											2021-06-12 18:53:49 +01:00
										 |  |  |     def test_EOFS_with_file(self): | 
					
						
							|  |  |  |         expect = ("(<string>, line 1)") | 
					
						
							|  |  |  |         with os_helper.temp_dir() as temp_dir: | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', | 
					
						
							|  |  |  |                                                   """ä = '''thîs is \na \ntest""") | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 "    ä = '''thîs is ", | 
					
						
							|  |  |  |                 '        ^', | 
					
						
							|  |  |  |                 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', | 
					
						
							|  |  |  |                                                   """ä = '''thîs is \na \ntest""".encode()) | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 "    ä = '''thîs is ", | 
					
						
							|  |  |  |                 '        ^', | 
					
						
							|  |  |  |                 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', | 
					
						
							|  |  |  |                                                   BOM_UTF8 + """ä = '''thîs is \na \ntest""".encode()) | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 "    ä = '''thîs is ", | 
					
						
							|  |  |  |                 '        ^', | 
					
						
							|  |  |  |                 'SyntaxError: unterminated triple-quoted string literal (detected at line 3)']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', | 
					
						
							|  |  |  |                                                   """# coding: latin1\nä = '''thîs is \na \ntest""".encode('latin1')) | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 "    ä = '''thîs is ", | 
					
						
							|  |  |  |                 '        ^', | 
					
						
							|  |  |  |                 'SyntaxError: unterminated triple-quoted string literal (detected at line 4)']) | 
					
						
							| 
									
										
										
										
											2021-06-12 18:53:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-19 17:18:16 +01:00
										 |  |  |     @warnings_helper.ignore_warnings(category=SyntaxWarning) | 
					
						
							| 
									
										
										
										
											2020-05-08 03:38:44 +01:00
										 |  |  |     def test_eof_with_line_continuation(self): | 
					
						
							|  |  |  |         expect = "unexpected EOF while parsing (<string>, line 1)" | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							| 
									
										
										
										
											2023-04-19 17:18:16 +01:00
										 |  |  |             compile('"\\Xhh" \\', '<string>', 'exec') | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							| 
									
										
										
										
											2020-05-08 03:38:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  |     def test_line_continuation_EOF(self): | 
					
						
							| 
									
										
										
										
											2020-01-09 18:07:32 +01:00
										 |  |  |         """A continuation at the end of input must be an error; bpo2180.""" | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  |         expect = 'unexpected EOF while parsing (<string>, line 1)' | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             exec('ä = 5\\') | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, 'ä = 5\\\n') | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 7) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             exec('ä = 5\\'.encode()) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, 'ä = 5\\\n') | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 7) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             exec('# coding:latin1\nä = 5\\'.encode('latin1')) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), | 
					
						
							|  |  |  |                          'unexpected EOF while parsing (<string>, line 2)') | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, 'ä = 5\\\n') | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 7) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							|  |  |  |             exec(BOM_UTF8 + 'ä = 5\\'.encode()) | 
					
						
							|  |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.text, 'ä = 5\\\n') | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.offset, 7) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as cm: | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  |             exec('\\') | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |         self.assertEqual(str(cm.exception), expect) | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @unittest.skipIf(not sys.executable, "sys.executable required") | 
					
						
							| 
									
										
										
										
											2025-01-22 21:09:51 +02:00
										 |  |  |     @force_not_colorized | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  |     def test_line_continuation_EOF_from_file_bpo2180(self): | 
					
						
							|  |  |  |         """Ensure tok_nextc() does not add too many ending newlines.""" | 
					
						
							| 
									
										
										
										
											2020-07-06 20:29:49 +08:00
										 |  |  |         with os_helper.temp_dir() as temp_dir: | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', '\\') | 
					
						
							| 
									
										
										
										
											2024-10-07 23:23:49 +02:00
										 |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-2:], [ | 
					
						
							|  |  |  |                 '    \\', | 
					
						
							|  |  |  |                 'SyntaxError: unexpected EOF while parsing']) | 
					
						
							|  |  |  |             self.assertEqual(err[-3][-8:], ', line 1', err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', 'ä = 6\\') | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 '    ä = 6\\', | 
					
						
							|  |  |  |                 '          ^', | 
					
						
							|  |  |  |                 'SyntaxError: unexpected EOF while parsing']) | 
					
						
							|  |  |  |             self.assertEqual(err[-4][-8:], ', line 1', err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', | 
					
						
							|  |  |  |                                                   '# coding:latin1\n' | 
					
						
							|  |  |  |                                                   'ä = 7\\'.encode('latin1')) | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 '    ä = 7\\', | 
					
						
							|  |  |  |                 '          ^', | 
					
						
							|  |  |  |                 'SyntaxError: unexpected EOF while parsing']) | 
					
						
							|  |  |  |             self.assertEqual(err[-4][-8:], ', line 2', err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', | 
					
						
							|  |  |  |                                                   BOM_UTF8 + 'ä = 8\\'.encode()) | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure('-X', 'utf8', file_name) | 
					
						
							|  |  |  |             err = err.decode().splitlines() | 
					
						
							|  |  |  |             self.assertEqual(err[-3:], [ | 
					
						
							|  |  |  |                 '    ä = 8\\', | 
					
						
							|  |  |  |                 '          ^', | 
					
						
							|  |  |  |                 'SyntaxError: unexpected EOF while parsing']) | 
					
						
							|  |  |  |             self.assertEqual(err[-4][-8:], ', line 1', err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  |     unittest.main() |