mirror of
https://github.com/python/cpython.git
synced 2025-11-03 11:23:31 +00:00
bpo-20844: open script file with "rb" mode (GH-12616)
This commit is contained in:
parent
62f9588663
commit
10654c19b5
4 changed files with 24 additions and 1 deletions
|
|
@ -109,6 +109,10 @@ the same library that the Python runtime is using.
|
||||||
(:func:`sys.getfilesystemencoding`). If *closeit* is true, the file is
|
(:func:`sys.getfilesystemencoding`). If *closeit* is true, the file is
|
||||||
closed before PyRun_SimpleFileExFlags returns.
|
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)
|
.. c:function:: int PyRun_InteractiveOne(FILE *fp, const char *filename)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -409,6 +409,23 @@ class CmdLineTest(unittest.TestCase):
|
||||||
script_name, script_name, script_dir, '',
|
script_name, script_name, script_dir, '',
|
||||||
importlib.machinery.SourceFileLoader)
|
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
|
@contextlib.contextmanager
|
||||||
def setup_test_pkg(self, *args):
|
def setup_test_pkg(self, *args):
|
||||||
with support.temp_dir() as script_dir, \
|
with support.temp_dir() as script_dir, \
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix running script with encoding cookie and LF line ending
|
||||||
|
may fail on Windows.
|
||||||
|
|
@ -283,7 +283,7 @@ static int
|
||||||
pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf)
|
pymain_run_file(_PyCoreConfig *config, PyCompilerFlags *cf)
|
||||||
{
|
{
|
||||||
const wchar_t *filename = config->run_filename;
|
const wchar_t *filename = config->run_filename;
|
||||||
FILE *fp = _Py_wfopen(filename, L"r");
|
FILE *fp = _Py_wfopen(filename, L"rb");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
char *cfilename_buffer;
|
char *cfilename_buffer;
|
||||||
const char *cfilename;
|
const char *cfilename;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue