bpo-40985: Show correct SyntaxError text when last line has a LINECONT (GH-20888)

When a file ends with a line that contains a line continuation character
the text of the emitted SyntaxError is empty, contrary to the old
parser, where the error text contained the text of the last line.
This commit is contained in:
Lysandros Nikolaou 2020-06-16 03:27:33 +03:00 committed by GitHub
parent 8666356280
commit 113e2b0a07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -52,10 +52,14 @@ class EOFTestCase(unittest.TestCase):
file_name = script_helper.make_script(temp_dir, 'foo', '\\') file_name = script_helper.make_script(temp_dir, 'foo', '\\')
rc, out, err = script_helper.assert_python_failure(file_name) rc, out, err = script_helper.assert_python_failure(file_name)
self.assertIn(b'unexpected EOF while parsing', err) self.assertIn(b'unexpected EOF while parsing', err)
self.assertIn(b'line 2', err)
self.assertIn(b'\\', err)
file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\') file_name = script_helper.make_script(temp_dir, 'foo', 'y = 6\\')
rc, out, err = script_helper.assert_python_failure(file_name) rc, out, err = script_helper.assert_python_failure(file_name)
self.assertIn(b'unexpected EOF while parsing', err) self.assertIn(b'unexpected EOF while parsing', err)
self.assertIn(b'line 2', err)
self.assertIn(b'y = 6\\', err)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1 @@
Fix a bug that caused the :exc:`SyntaxError` text to be empty when a file ends with a line ending in a line continuation character (i.e. backslash). The error text should contain the text of the last line.

View File

@ -1646,16 +1646,18 @@ err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
{ {
int i; int i;
char linebuf[1000]; char linebuf[1000];
if (fp == NULL) {
if (fp == NULL)
return NULL; return NULL;
}
for (i = 0; i < lineno; i++) { for (i = 0; i < lineno; i++) {
char *pLastChar = &linebuf[sizeof(linebuf) - 2]; char *pLastChar = &linebuf[sizeof(linebuf) - 2];
do { do {
*pLastChar = '\0'; *pLastChar = '\0';
if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
fp, NULL) == NULL) fp, NULL) == NULL) {
break; goto after_loop;
}
/* fgets read *something*; if it didn't get as /* fgets read *something*; if it didn't get as
far as pLastChar, it must have found a newline far as pLastChar, it must have found a newline
or hit the end of the file; if pLastChar is \n, or hit the end of the file; if pLastChar is \n,
@ -1663,6 +1665,8 @@ err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
yet seen a newline, so must continue */ yet seen a newline, so must continue */
} while (*pLastChar != '\0' && *pLastChar != '\n'); } while (*pLastChar != '\0' && *pLastChar != '\n');
} }
after_loop:
fclose(fp); fclose(fp);
if (i == lineno) { if (i == lineno) {
PyObject *res; PyObject *res;