| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 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 ("'", "\""): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 eval(f"""{quote}this is a test\
 | 
					
						
							|  |  |  |                 """)
 | 
					
						
							|  |  |  |             except SyntaxError as msg: | 
					
						
							|  |  |  |                 self.assertEqual(str(msg), expect) | 
					
						
							|  |  |  |                 self.assertEqual(msg.offset, 1) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise support.TestFailed | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_EOFS(self): | 
					
						
							| 
									
										
										
										
											2021-01-21 00:38:47 +03:00
										 |  |  |         expect = ("unterminated triple-quoted string literal (detected at line 1) (<string>, line 1)") | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             eval("""'''this is a test""") | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |         except SyntaxError as msg: | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |             self.assertEqual(str(msg), expect) | 
					
						
							| 
									
										
										
										
											2021-01-21 00:38:47 +03:00
										 |  |  |             self.assertEqual(msg.offset, 1) | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |             raise support.TestFailed | 
					
						
							| 
									
										
										
										
											2002-08-15 01:28:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', """'''this is \na \ntest""") | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure(file_name) | 
					
						
							|  |  |  |         self.assertIn(b'unterminated triple-quoted string literal (detected at line 3)', err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 03:38:44 +01:00
										 |  |  |     def test_eof_with_line_continuation(self): | 
					
						
							|  |  |  |         expect = "unexpected EOF while parsing (<string>, line 1)" | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             compile('"\\xhh" \\',  '<string>', 'exec', dont_inherit=True) | 
					
						
							|  |  |  |         except SyntaxError as msg: | 
					
						
							|  |  |  |             self.assertEqual(str(msg), expect) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise support.TestFailed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)' | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as excinfo: | 
					
						
							|  |  |  |             exec('x = 5\\') | 
					
						
							|  |  |  |         self.assertEqual(str(excinfo.exception), expect) | 
					
						
							|  |  |  |         with self.assertRaises(SyntaxError) as excinfo: | 
					
						
							|  |  |  |             exec('\\') | 
					
						
							|  |  |  |         self.assertEqual(str(excinfo.exception), expect) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @unittest.skipIf(not sys.executable, "sys.executable required") | 
					
						
							|  |  |  |     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', '\\') | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure(file_name) | 
					
						
							|  |  |  |             self.assertIn(b'unexpected EOF while parsing', err) | 
					
						
							| 
									
										
										
										
											2021-03-28 23:48:05 +01:00
										 |  |  |             self.assertIn(b'line 1', err) | 
					
						
							| 
									
										
										
										
											2020-06-16 03:27:33 +03:00
										 |  |  |             self.assertIn(b'\\', err) | 
					
						
							| 
									
										
										
										
											2019-05-18 11:27:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\') | 
					
						
							|  |  |  |             rc, out, err = script_helper.assert_python_failure(file_name) | 
					
						
							|  |  |  |             self.assertIn(b'unexpected EOF while parsing', err) | 
					
						
							| 
									
										
										
										
											2021-03-28 23:48:05 +01:00
										 |  |  |             self.assertIn(b'line 1', err) | 
					
						
							| 
									
										
										
										
											2020-06-16 03:27:33 +03:00
										 |  |  |             self.assertIn(b'y = 6\\', 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() |