]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-20844: open script file with "rb" mode (GH-12616)
authorInada Naoki <songofacandy@gmail.com>
Mon, 1 Apr 2019 12:02:51 +0000 (21:02 +0900)
committerGitHub <noreply@github.com>
Mon, 1 Apr 2019 12:02:51 +0000 (21:02 +0900)
(cherry picked from commit 10654c19b5e6efdf3c529ff9bf7bcab89bdca1c1)

Doc/c-api/veryhigh.rst
Lib/test/test_cmd_line_script.py
Misc/NEWS.d/next/Core and Builtins/2019-03-29-18-47-50.bpo-20844.ge-7SM.rst [new file with mode: 0644]
Modules/main.c

index c891f6320f944f39f4d67e0a104ea5fef648c804..317093e95615e3af462a833811318820322f417c 100644 (file)
@@ -109,6 +109,10 @@ the same library that the Python runtime is using.
    (:func:`sys.getfilesystemencoding`).  If *closeit* is true, the file is
    closed before PyRun_SimpleFileExFlags returns.
 
+   .. note::
+      On Windows, *fp* should be opened as binary mode (e.g. ``fopen(filename, "rb")``.
+      Otherwise, Python may not handle script file with LF line ending correctly.
+
 
 .. c:function:: int PyRun_InteractiveOne(FILE *fp, const char *filename)
 
index 5ec9bbbb1230e65634ea8e1339324f6ffe1d1291..b2632602d454b94e996be5c207c2615aa4b6d6c9 100644 (file)
@@ -387,6 +387,23 @@ class CmdLineTest(unittest.TestCase):
                                       script_name, script_name, script_dir, '',
                                       importlib.machinery.SourceFileLoader)
 
+    def test_issue20884(self):
+        # On Windows, script with encoding cookie and LF line ending
+        # will be failed.
+        with support.temp_dir() as script_dir:
+            script_name = os.path.join(script_dir, "issue20884.py")
+            with open(script_name, "w", newline='\n') as f:
+                f.write("#coding: iso-8859-1\n")
+                f.write('"""\n')
+                for _ in range(30):
+                    f.write('x'*80 + '\n')
+                f.write('"""\n')
+
+            with support.change_cwd(path=script_dir):
+                rc, out, err = assert_python_ok(script_name)
+            self.assertEqual(b"", out)
+            self.assertEqual(b"", err)
+
     @contextlib.contextmanager
     def setup_test_pkg(self, *args):
         with support.temp_dir() as script_dir, \
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-03-29-18-47-50.bpo-20844.ge-7SM.rst b/Misc/NEWS.d/next/Core and Builtins/2019-03-29-18-47-50.bpo-20844.ge-7SM.rst
new file mode 100644 (file)
index 0000000..22a400a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix running script with encoding cookie and LF line ending
+may fail on Windows.
index 9011bd1f69cb05b763f7811c0ff9285fa4871ac2..e3683b9417545bd2cc4fbdf7d8fe06e7bc5740d0 100644 (file)
@@ -1534,7 +1534,7 @@ pymain_open_filename(_PyMain *pymain)
     const _PyCoreConfig *config = &_PyGILState_GetInterpreterStateUnsafe()->core_config;
     FILE* fp;
 
-    fp = _Py_wfopen(pymain->filename, L"r");
+    fp = _Py_wfopen(pymain->filename, L"rb");
     if (fp == NULL) {
         char *cfilename_buffer;
         const char *cfilename;