]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-25083: Python can sometimes create incorrect .pyc files (GH-8449)
authortzickel <tzickel@users.noreply.github.com>
Mon, 10 Sep 2018 18:46:14 +0000 (21:46 +0300)
committerPetr Viktorin <encukou@gmail.com>
Mon, 10 Sep 2018 18:46:14 +0000 (11:46 -0700)
Python 2 never checked for I/O error when reading .py files and
thus could mistake an I/O error for EOF and create incorrect .pyc
files.
This adds an check for this and aborts on an error.

Include/errcode.h
Misc/NEWS.d/next/Core and Builtins/2018-07-25-22-47-19.bpo-25083.HT_hXh.rst [new file with mode: 0644]
Parser/tokenizer.c
Python/pythonrun.c

index becec80c8acfea6ff5c721c164a52d1b7eeb7f26..5c5a0f7fa34666f6d55ad157e34d67bb78f70b29 100644 (file)
@@ -29,6 +29,7 @@ extern "C" {
 #define E_EOFS         23      /* EOF in triple-quoted string */
 #define E_EOLS         24      /* EOL in single-quoted string */
 #define E_LINECONT     25      /* Unexpected characters after a line continuation */
+#define E_IO           26      /* I/O error */
 
 #ifdef __cplusplus
 }
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-25-22-47-19.bpo-25083.HT_hXh.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-25-22-47-19.bpo-25083.HT_hXh.rst
new file mode 100644 (file)
index 0000000..0dc44c4
--- /dev/null
@@ -0,0 +1,2 @@
+Adding I/O error checking when reading .py files and aborting importing on
+error.
index 61bfb4e1b7afb3914b47a47d07ad3bd1bc0b6b13..c6e61df533e248c6ab98d7b3f64e15e3174e2ce4 100644 (file)
@@ -1681,6 +1681,11 @@ int
 PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
 {
     int result = tok_get(tok, p_start, p_end);
+    if (tok->fp && ferror(tok->fp)) {
+        clearerr(tok->fp);
+        result = ERRORTOKEN;
+        tok->done = E_IO;
+    }
     if (tok->decoding_erred) {
         result = ERRORTOKEN;
         tok->done = E_DECODE;
index 5707c9f5245289a8bddba9b4a09215866c29ba72..2c9f55fbd1dff32059f2443ed33d259d41956f14 100644 (file)
@@ -1654,6 +1654,9 @@ err_input(perrdetail *err)
         Py_XDECREF(tb);
         break;
     }
+    case E_IO:
+        msg = "I/O error while reading";
+        break;
     case E_LINECONT:
         msg = "unexpected character after line continuation character";
         break;