Skip to content

Commit

Permalink
bpo-2180: Treat line continuation at EOF as a SyntaxError (pythonGH…
Browse files Browse the repository at this point in the history
…-13401)

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
```



https://bugs.python.org/issue2180
  • Loading branch information
asottile authored and miss-islington committed May 18, 2019
1 parent e917f2e commit abea73b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
26 changes: 25 additions & 1 deletion Lib/test/test_eof.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""test script for a few new invalid token catches"""

import unittest
import sys
from test import support
from test.support import script_helper
import unittest

class EOFTestCase(unittest.TestCase):
def test_EOFC(self):
Expand All @@ -24,5 +26,27 @@ def test_EOFS(self):
else:
raise support.TestFailed

def test_line_continuation_EOF(self):
"""A contination at the end of input must be an error; bpo2180."""
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."""
with support.temp_dir() as temp_dir:
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)

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)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Treat line continuation at EOF as a ``SyntaxError`` by Anthony Sottile.
11 changes: 10 additions & 1 deletion Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,8 @@ tok_nextc(struct tok_state *tok)
return EOF;
/* Last line does not end in \n,
fake one */
strcpy(tok->inp, "\n");
if (tok->inp[-1] != '\n')
strcpy(tok->inp, "\n");
}
tok->inp = strchr(tok->inp, '\0');
done = tok->inp[-1] == '\n';
Expand Down Expand Up @@ -1674,6 +1675,14 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
tok->cur = tok->inp;
return ERRORTOKEN;
}
c = tok_nextc(tok);
if (c == EOF) {
tok->done = E_EOF;
tok->cur = tok->inp;
return ERRORTOKEN;
} else {
tok_backup(tok, c);
}
tok->cont_line = 1;
goto again; /* Read next line */
}
Expand Down

0 comments on commit abea73b

Please sign in to comment.