Skip to content

Commit d55133f

Browse files
authored
bpo-40334: Catch E_EOF error, when the tokenizer returns ERRORTOKEN (GH-19743)
An E_EOF error was only being caught after the parser exited before this commit. There are some cases though, where the tokenizer returns ERRORTOKEN *and* has set an E_EOF error (like when EOF directly follows a line continuation character) which weren't correctly handled before.
1 parent 5d1f32d commit d55133f

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

Lib/test/test_eof.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def test_EOFS(self):
2626
else:
2727
raise support.TestFailed
2828

29-
@support.skip_if_new_parser("TODO for PEG -- fails with new parser")
3029
def test_line_continuation_EOF(self):
3130
"""A continuation at the end of input must be an error; bpo2180."""
3231
expect = 'unexpected EOF while parsing (<string>, line 1)'
@@ -37,7 +36,6 @@ def test_line_continuation_EOF(self):
3736
exec('\\')
3837
self.assertEqual(str(excinfo.exception), expect)
3938

40-
@unittest.skip("TODO for PEG -- fails even with old parser now")
4139
@unittest.skipIf(not sys.executable, "sys.executable required")
4240
def test_line_continuation_EOF_from_file_bpo2180(self):
4341
"""Ensure tok_nextc() does not add too many ending newlines."""

Parser/pegen/pegen.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,16 @@ tokenizer_error(Parser *p)
344344
break;
345345
case E_BADPREFIX:
346346
return tokenizer_error_with_col_offset(p,
347-
PyExc_SyntaxError, "invalid string prefix");
347+
errtype, "invalid string prefix");
348348
case E_EOFS:
349349
return tokenizer_error_with_col_offset(p,
350-
PyExc_SyntaxError, "EOF while scanning triple-quoted string literal");
350+
errtype, "EOF while scanning triple-quoted string literal");
351351
case E_EOLS:
352352
return tokenizer_error_with_col_offset(p,
353-
PyExc_SyntaxError, "EOL while scanning string literal");
353+
errtype, "EOL while scanning string literal");
354+
case E_EOF:
355+
return tokenizer_error_with_col_offset(p,
356+
errtype, "unexpected EOF while parsing");
354357
case E_DEDENT:
355358
return tokenizer_error_with_col_offset(p,
356359
PyExc_IndentationError, "unindent does not match any outer indentation level");

0 commit comments

Comments
 (0)