bpo-11594: Ensure line-endings are respected when using 2to3 by aaronang · Pull Request #6483 · python/cpython
@aaronang I think I see where things went wrong. In adding newline='' during the read phase, that caused the newlines to be untranslated. But then in the write phase, we left newline=None (the default), which causes \n to be translated to \r\n. As a result, CRLF -> CRCRLF (ref)
I think the patch also needs something like:
cpython master $ git diff
diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index 7c4e064997..7841b99a5c 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -514,7 +514,7 @@ class RefactoringTool(object):
set.
"""
try:
- fp = io.open(filename, "w", encoding=encoding)
+ fp = io.open(filename, "w", encoding=encoding, newline='')
except OSError as err:
self.log_error("Can't create %s: %s", filename, err)
return
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index f3059a9311..9e3b8fbb90 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -300,6 +300,7 @@ from __future__ import print_function"""
old, new = self.refactor_file(fn)
self.assertIn(b"\r\n", old)
self.assertIn(b"\r\n", new)
+ self.assertNotIn(b"\r\r\n", new)
def test_refactor_docstring(self):
rt = self.rt()