Skip to content

Commit 9437230

Browse files
committed
Treat line continuation at EOF as a SyntaxError
This makes the parser consistent with the tokenize module (already the case in `pypy`). sample ------ ```python x = 5\ ``` before ------ ```console $ python3 t.py $ python3 -mtokenize t.py t.py:2:0: error: EOF in multi-line statement ``` after ----- ```console $ ./python t.py File "t.py", line 3 x = 5\ ^ SyntaxError: unexpected EOF while parsing $ ./python -m tokenize t.py t.py:2:0: error: EOF in multi-line statement ```
1 parent 27ee0f8 commit 9437230

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/test/test_eof.py

+6
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ def test_EOFS(self):
2424
else:
2525
raise support.TestFailed
2626

27+
def test_line_continuation_EOF(self):
28+
expect = 'unexpected EOF while parsing (<string>, line 1)'
29+
with self.assertRaises(SyntaxError) as excinfo:
30+
exec('x = 5\\')
31+
self.assertEqual(str(excinfo.exception), expect)
32+
2733
if __name__ == "__main__":
2834
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Treat line continuation at EOF as a ``SyntaxError`` by Anthony Sottile.

Parser/tokenizer.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ tok_nextc(struct tok_state *tok)
983983
return EOF;
984984
/* Last line does not end in \n,
985985
fake one */
986-
strcpy(tok->inp, "\n");
986+
if (tok->inp[-1] != '\n')
987+
strcpy(tok->inp, "\n");
987988
}
988989
tok->inp = strchr(tok->inp, '\0');
989990
done = tok->inp[-1] == '\n';
@@ -1674,6 +1675,14 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
16741675
tok->cur = tok->inp;
16751676
return ERRORTOKEN;
16761677
}
1678+
c = tok_nextc(tok);
1679+
if (c == EOF) {
1680+
tok->done = E_EOF;
1681+
tok->cur = tok->inp;
1682+
return ERRORTOKEN;
1683+
} else {
1684+
tok_backup(tok, c);
1685+
}
16771686
tok->cont_line = 1;
16781687
goto again; /* Read next line */
16791688
}

0 commit comments

Comments
 (0)