[Python-checkins] bpo-11594: Ensure line-endings are respected when using 2to3 (GH-6483)
Miss Islington (bot)
webhook-mailer at python.org
Tue Apr 17 17:58:43 EDT 2018
https://github.com/python/cpython/commit/3b3be1fe10f6c15e57360cac9d9dbc660666e655
commit: 3b3be1fe10f6c15e57360cac9d9dbc660666e655
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-04-17T14:58:34-07:00
summary:
bpo-11594: Ensure line-endings are respected when using 2to3 (GH-6483)
(cherry picked from commit c127a86e1862df88ec6f9d15b79c627fc616766e)
Co-authored-by: Aaron Ang <aaronang at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst
M Lib/lib2to3/refactor.py
M Lib/lib2to3/tests/data/crlf.py
M Lib/lib2to3/tests/test_refactor.py
diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index db2e38d22f16..7c4e0649975c 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -314,7 +314,7 @@ def _read_python_source(self, filename):
encoding = tokenize.detect_encoding(f.readline)[0]
finally:
f.close()
- with io.open(filename, "r", encoding=encoding) as f:
+ with io.open(filename, "r", encoding=encoding, newline='') as f:
return f.read(), encoding
def refactor_file(self, filename, write=False, doctests_only=False):
diff --git a/Lib/lib2to3/tests/data/crlf.py b/Lib/lib2to3/tests/data/crlf.py
index dbe2d7bb1012..a83ca8f0a206 100644
--- a/Lib/lib2to3/tests/data/crlf.py
+++ b/Lib/lib2to3/tests/data/crlf.py
@@ -1,3 +1,3 @@
-print "hi"
-
-print "Like bad Windows newlines?"
+print "hi"
+
+print "Like bad Windows newlines?"
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index e9bae5e45d01..f3059a93113b 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -180,32 +180,42 @@ def print_output(self, old_text, new_text, filename, equal):
def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS,
options=None, mock_log_debug=None,
actually_write=True):
- tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
- self.addCleanup(shutil.rmtree, tmpdir)
- # make a copy of the tested file that we can write to
- shutil.copy(test_file, tmpdir)
- test_file = os.path.join(tmpdir, os.path.basename(test_file))
- os.chmod(test_file, 0o644)
-
- def read_file():
- with open(test_file, "rb") as fp:
- return fp.read()
-
- old_contents = read_file()
+ test_file = self.init_test_file(test_file)
+ old_contents = self.read_file(test_file)
rt = self.rt(fixers=fixers, options=options)
if mock_log_debug:
rt.log_debug = mock_log_debug
rt.refactor_file(test_file)
- self.assertEqual(old_contents, read_file())
+ self.assertEqual(old_contents, self.read_file(test_file))
if not actually_write:
return
rt.refactor_file(test_file, True)
- new_contents = read_file()
+ new_contents = self.read_file(test_file)
self.assertNotEqual(old_contents, new_contents)
return new_contents
+ def init_test_file(self, test_file):
+ tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
+ self.addCleanup(shutil.rmtree, tmpdir)
+ shutil.copy(test_file, tmpdir)
+ test_file = os.path.join(tmpdir, os.path.basename(test_file))
+ os.chmod(test_file, 0o644)
+ return test_file
+
+ def read_file(self, test_file):
+ with open(test_file, "rb") as fp:
+ return fp.read()
+
+ def refactor_file(self, test_file, fixers=_2TO3_FIXERS):
+ test_file = self.init_test_file(test_file)
+ old_contents = self.read_file(test_file)
+ rt = self.rt(fixers=fixers)
+ rt.refactor_file(test_file, True)
+ new_contents = self.read_file(test_file)
+ return old_contents, new_contents
+
def test_refactor_file(self):
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
@@ -285,6 +295,12 @@ def test_crlf_newlines(self):
finally:
os.linesep = old_sep
+ def test_crlf_unchanged(self):
+ fn = os.path.join(TEST_DATA_DIR, "crlf.py")
+ old, new = self.refactor_file(fn)
+ self.assertIn(b"\r\n", old)
+ self.assertIn(b"\r\n", new)
+
def test_refactor_docstring(self):
rt = self.rt()
diff --git a/Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst b/Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst
new file mode 100644
index 000000000000..be2abf6c34dd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst
@@ -0,0 +1 @@
+Ensure line-endings are respected when using lib2to3.
More information about the Python-checkins
mailing list