Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/c-api/veryhigh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,23 @@ def test_issue8202_dash_m_file_ignored(self):
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, \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix running script with encoding cookie and LF line ending
may fail on Windows.
2 changes: 1 addition & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down