Skip to content

bpo-11594: Ensure line-endings are respected when using 2to3 #6483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 17, 2018

Conversation

aaronang
Copy link
Contributor

@aaronang aaronang commented Apr 16, 2018

@jaraco
Copy link
Member

jaraco commented Apr 16, 2018

Looks good to me!

@jaraco
Copy link
Member

jaraco commented Apr 16, 2018

Can you add a news file also?

@aaronang
Copy link
Contributor Author

@jaraco Done 👍

return fp.read()

old_contents = read_file()
rt = self.rt(fixers=fixers)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nicer if you extracted lines 210-221 to a separate function that both check_file_refactoring() and refactor_file() would reuse. Now there's copypasta.

print "Like bad Windows newlines?"
print "hi"
print "Like bad Windows newlines?"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's hilarious. We've had a \r\n file but something along the way wiped the newlines O_O

@aaronang
Copy link
Contributor Author

@ambv Thank you for taking the time for reviewing this patch. I didn't refactor it exactly the way you told me to because I felt it was too much "extracting the similarities just for the sake of keeping the code DRY". Instead, I extracted certain functionality, "initializing the test file" and "reading the test file", into separate functions init_test_file and read_file which made more sense to me. Looking forward to your feedback 🙂

@ambv
Copy link
Contributor

ambv commented Apr 17, 2018

Much better. Thanks!

@ambv ambv merged commit c127a86 into python:master Apr 17, 2018
@miss-islington
Copy link
Contributor

Thanks @aaronang for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 17, 2018
@bedevere-bot
Copy link

GH-6515 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Sorry, @aaronang and @ambv, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker c127a86e1862df88ec6f9d15b79c627fc616766e 3.6

miss-islington added a commit that referenced this pull request Apr 17, 2018
@aaronang aaronang deleted the bpo-11594 branch April 17, 2018 22:49
@blah238
Copy link

blah238 commented Jul 2, 2018

@aaronang Did this not make the 3.7.0 release? I'm seeing my line endings being changed from CRLF to CRCRLF (yikes!) on Windows. Or perhaps that's a different issue?

@jaraco
Copy link
Member

jaraco commented Jul 4, 2018

According to the news file, this PR was included in 3.7.0b4. Sounds like you may have identified a new issue.

@blah238
Copy link

blah238 commented Jul 6, 2018

@jaraco Good to know. All tests passed on my Windows PC. Could you add a test case for CRLF files on Windows? I don't know if the automated tests are run against Windows but I could run it manually if needed.

@jaraco
Copy link
Member

jaraco commented Jul 6, 2018

@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()

@aaronang
Copy link
Contributor Author

I am sorry for the late reply. I am looking into it now.

@aaronang
Copy link
Contributor Author

@blah238 Could you apply this patch to the master:

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()

And run:

$ ./python.exe -m unittest -v lib2to3.tests.test_refactor.TestRefactoringTool.test_crlf_unchanged

For me this doesn't fail locally.

@jaraco
Copy link
Member

jaraco commented Jul 13, 2018

@aaronang to be clear, when the test didn't fail, did you run it on Windows? I didn't run the test suite, but I did verify the behavior on a Windows machine.

@aaronang
Copy link
Contributor Author

@jaraco I am sorry for being unclear. I don't have a Windows machine so I couldn't test whether the test with the patch (that you proposed) fails or not on Windows.

@aaronang
Copy link
Contributor Author

@jaraco I am sorry for asking this but could you test out the patch that you proposed to see if it fixes the problem? I am not really capable of fixing the problem because I don't have a Windows machine available. I am a bit embarrassed for asking this 😓

@jaraco
Copy link
Member

jaraco commented Jul 13, 2018

I’m pretty sure this is the right fix. I’ll submit a PR with just the test update to demonstrate the failure then apply the fix.

@aaronang
Copy link
Contributor Author

@jaraco I really wanted to help out but don't have a Windows machine available. Thanks a lot!

@jaraco
Copy link
Member

jaraco commented Jul 13, 2018

@blah238 - The fix should come in 3.7.1, but feel free to grab the latest refactor.py from the Python 3.7 branch and replace it in your installation.

@blah238
Copy link

blah238 commented Jul 13, 2018

Great to hear, thanks @jaraco and @aaronang! Much appreciated.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants