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

Lysandros Nikolaou webhook-mailer at python.org
Mon Jun 15 20:27:42 EDT 2020


https://github.com/python/cpython/commit/113e2b0a07c72c0d5e3489076afb14f6b3ad1049
commit: 113e2b0a07c72c0d5e3489076afb14f6b3ad1049
branch: master
author: Lysandros Nikolaou <lisandrosnik at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-06-16T01:27:33+01:00
summary:

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.

files:
A Misc/NEWS.d/next/Core and Builtins/2020-06-15-16-29-55.bpo-40985.IIN_xX.rst
M Lib/test/test_eof.py
M Python/errors.c

diff --git a/Lib/test/test_eof.py b/Lib/test/test_eof.py
index bebad3106119e..51cbbd8eed664 100644
--- a/Lib/test/test_eof.py
+++ b/Lib/test/test_eof.py
@@ -52,10 +52,14 @@ def test_line_continuation_EOF_from_file_bpo2180(self):
             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)
+            self.assertIn(b'line 2', err)
+            self.assertIn(b'\\', 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)
+            self.assertIn(b'line 2', err)
+            self.assertIn(b'y = 6\\', err)
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-06-15-16-29-55.bpo-40985.IIN_xX.rst b/Misc/NEWS.d/next/Core and Builtins/2020-06-15-16-29-55.bpo-40985.IIN_xX.rst
new file mode 100644
index 0000000000000..e07134c7166ad
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-06-15-16-29-55.bpo-40985.IIN_xX.rst	
@@ -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.
diff --git a/Python/errors.c b/Python/errors.c
index cc00ae4a5407b..720f18bc224d4 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1646,16 +1646,18 @@ err_programtext(PyThreadState *tstate, FILE *fp, int lineno)
 {
     int i;
     char linebuf[1000];
-
-    if (fp == NULL)
+    if (fp == NULL) {
         return NULL;
+    }
+
     for (i = 0; i < lineno; i++) {
         char *pLastChar = &linebuf[sizeof(linebuf) - 2];
         do {
             *pLastChar = '\0';
             if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
-                                         fp, NULL) == NULL)
-                break;
+                                         fp, NULL) == NULL) {
+                goto after_loop;
+            }
             /* fgets read *something*; if it didn't get as
                far as pLastChar, it must have found a newline
                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 */
         } while (*pLastChar != '\0' && *pLastChar != '\n');
     }
+
+after_loop:
     fclose(fp);
     if (i == lineno) {
         PyObject *res;



More information about the Python-checkins mailing list