[Python-checkins] bpo-34108: Fix double carriage return in 2to3 on Windows (GH-8271) (#8275)

Jason R. Coombs webhook-mailer at python.org
Fri Jul 13 15:24:22 EDT 2018


https://github.com/python/cpython/commit/cf21d0031dd84544d4108765553c2b03dfe726c5
commit: cf21d0031dd84544d4108765553c2b03dfe726c5
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Jason R. Coombs <jaraco at jaraco.com>
date: 2018-07-13T15:24:19-04:00
summary:

bpo-34108: Fix double carriage return in 2to3 on Windows (GH-8271) (#8275)

* Add test capturing failure.
* Honor newlines as present in the original file.
(cherry picked from commit cafaf0447b950fd4f59edd8cbde040c61ae528f8)

Co-authored-by: Jason R. Coombs <jaraco at jaraco.com>

files:
A Misc/NEWS.d/next/Library/2018-07-13-08-44-52.bpo-34108.RjobUC.rst
M Lib/lib2to3/refactor.py
M Lib/lib2to3/tests/test_refactor.py

diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index 7c4e0649975c..7841b99a5cd4 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -514,7 +514,7 @@ def write_file(self, new_text, filename, old_text, encoding=None):
         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 f3059a93113b..9e3b8fbb90b2 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -300,6 +300,7 @@ def test_crlf_unchanged(self):
         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()
diff --git a/Misc/NEWS.d/next/Library/2018-07-13-08-44-52.bpo-34108.RjobUC.rst b/Misc/NEWS.d/next/Library/2018-07-13-08-44-52.bpo-34108.RjobUC.rst
new file mode 100644
index 000000000000..1021f98b7f9e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-13-08-44-52.bpo-34108.RjobUC.rst
@@ -0,0 +1 @@
+Remove extraneous CR in 2to3 refactor.



More information about the Python-checkins mailing list