mirror of
				https://github.com/python/cpython.git
				synced 2025-11-01 06:01:29 +00:00 
			
		
		
		
	bpo-40267: Fix message when last input character produces a SyntaxError (GH-19521)
When there is a SyntaxError after reading the last input character from the tokenizer and if no newline follows it, the error message used to be `unexpected EOF while parsing`, which is wrong.
This commit is contained in:
		
							parent
							
								
									574547a75c
								
							
						
					
					
						commit
						9a4b38f66b
					
				
					 5 changed files with 13 additions and 1 deletions
				
			
		
							
								
								
									
										4
									
								
								Include/token.h
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										4
									
								
								Include/token.h
									
										
									
										generated
									
									
									
								
							|  | @ -78,6 +78,10 @@ extern "C" { | |||
| #define ISTERMINAL(x)           ((x) < NT_OFFSET) | ||||
| #define ISNONTERMINAL(x)        ((x) >= NT_OFFSET) | ||||
| #define ISEOF(x)                ((x) == ENDMARKER) | ||||
| #define ISWHITESPACE(x)         ((x) == ENDMARKER || \ | ||||
|                                  (x) == NEWLINE   || \ | ||||
|                                  (x) == INDENT    || \ | ||||
|                                  (x) == DEDENT) | ||||
| 
 | ||||
| 
 | ||||
| PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */ | ||||
|  |  | |||
|  | @ -713,7 +713,7 @@ def test_lambda(self): | |||
| 
 | ||||
|         # lambda doesn't work without parens, because the colon | ||||
|         #  makes the parser think it's a format_spec | ||||
|         self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing', | ||||
|         self.assertAllRaise(SyntaxError, 'invalid syntax', | ||||
|                             ["f'{lambda x:x}'", | ||||
|                              ]) | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1 @@ | |||
| Fix the tokenizer to display the correct error message, when there is a SyntaxError on the last input character and no newline follows. It used to be `unexpected EOF while parsing`, while it should be `invalid syntax`. | ||||
|  | @ -332,6 +332,9 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, | |||
|              PyParser_AddToken(ps, (int)type, str, | ||||
|                                lineno, col_offset, tok->lineno, end_col_offset, | ||||
|                                &(err_ret->expected))) != E_OK) { | ||||
|             if (tok->done == E_EOF && !ISWHITESPACE(type)) { | ||||
|                 tok->done = E_SYNTAX; | ||||
|             } | ||||
|             if (err_ret->error != E_DONE) { | ||||
|                 PyObject_FREE(str); | ||||
|                 err_ret->token = type; | ||||
|  |  | |||
|  | @ -69,6 +69,10 @@ def update_file(file, content): | |||
| #define ISTERMINAL(x)           ((x) < NT_OFFSET) | ||||
| #define ISNONTERMINAL(x)        ((x) >= NT_OFFSET) | ||||
| #define ISEOF(x)                ((x) == ENDMARKER) | ||||
| #define ISWHITESPACE(x)         ((x) == ENDMARKER || \\ | ||||
|                                  (x) == NEWLINE   || \\ | ||||
|                                  (x) == INDENT    || \\ | ||||
|                                  (x) == DEDENT) | ||||
| 
 | ||||
| 
 | ||||
| PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */ | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lysandros Nikolaou
						Lysandros Nikolaou