-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
2to3 does not preserve line endings #55803
Comments
I'm using LF-only line-endings for development of my IntelHex library. I'm working on Windows most of the time. After 2to3 tool has been ran on my library it has not only changed the Python syntax, but it also saved all files with CRLF line-endings. As result I have all changed files completelly modified and diff shows change in every line. 2to3 tool should respect my line-endings and must not use simple open(xxx, "wt") mode for writing modified files. |
Thanks for the report. Can you run “python -m test.test_lib2to3”, if possible with a Python 3.x version? I’ve seen that the tests use binary mode to compare file contents, so maybe you will get an error message that can get us started. |
@Éric Araujo: I've ran tests with python 3.2. All tests have passed: ---------------------------------------------------------------------- OK |
Thanks. Would you like to work on a unit test or full patch? |
Éric, thank you for the proposal, but I'm not familiar enough with the codebase to work on it. The short scan over the tests reveals that there is at least one test which tries to test CRLF behavior, in the file test_refactor.py, but I don't understand what it doing? def test_crlf_newlines(self):
old_sep = os.linesep
os.linesep = "\r\n"
try:
fn = os.path.join(TEST_DATA_DIR, "crlf.py")
fixes = refactor.get_fixers_from_package("lib2to3.fixes")
self.check_file_refactoring(fn, fixes)
finally:
os.linesep = old_sep So, in theory I can modify that test to check what if the file has LF-only line-endings originally, but os.linesep is CRLF, but then I don't know what the content I should create and how to run fixer over that. |
I can fix it. I just need to find time. :) |
I was surprised to see that the crlf.py file was not using CRLF in the new Mercurial repo. It is also not in the .hgeol file. I changed it locally, but it doesn’t change anything, the tests pass before and after the change. |
This issue affected me today. I'm editing a codebase that has mixed line endings in different files. I'd like to patch for Python 3 support without changing line endings. Even invoking a single fixer (print), the line endings mutate. Since I'm running on macOS, the files with CRLF get LF line endings. Answers in this question suggest the mutation can be suppressed by altering the _to_system_newlines function, but the proposed fix has no effect on the Python 3.6 version of this routine. I thought I'd encountered this issue before, but I realized after searching that I was thinking of bpo-10639, which may present a model for retaining the newlines when refactoring the code. I found I was able to work around the issue by putting lib2to3-clean.py in my current directory: import lib2to3.refactor
import runpy
import functools
if __name__ == '__main__':
lib2to3.refactor._open_with_encoding = functools.partial(open, newline='')
runpy.run_module('lib2to3') And invoking I've released this functionality in jaraco.develop 4.0 so others can readily invoke it with |
I couldn't reproduce this issue. I tried reproducing this problem by extending the TestRefactoringTool class and creating two files: one file with LF line-endings and one file with CRLF line-endings. The changes that I made can be found here: aaronang@55e8bd3. The output of the tests: Before: b'print("hi")\n\nprint("Like bad Windows newlines?")\n' Before: b'print("hi")\r\n\r\nprint("Like bad Windows newlines?")\r\n' Maybe this problem has been resolved? |
I do still see the issue on Python 3.7b3: $ python ~/Dropbox/bin/scripts/which-line-ending onefile.py
Line ending is '\n'
$ python ~/Dropbox/bin/scripts/which-line-ending otherfile.py
Line ending is '\r\n'
$ python -V
Python 3.7.0b3
$ python -m lib2to3 . -w
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored ./onefile.py
--- ./onefile.py (original)
+++ ./onefile.py (refactored)
@@ -1 +1 @@
-print 'hello world'
+print('hello world')
RefactoringTool: Refactored ./otherfile.py
--- ./otherfile.py (original)
+++ ./otherfile.py (refactored)
@@ -1 +1 @@
-print 'hello world'
+print('hello world')
RefactoringTool: Files that were modified:
RefactoringTool: ./onefile.py
RefactoringTool: ./otherfile.py
$ $ python ~/Dropbox/bin/scripts/which-line-ending onefile.py
Line ending is '\n'
$ python ~/Dropbox/bin/scripts/which-line-ending otherfile.py
Line ending is '\n' |
@jason R. Coombs Also, I figured out that the refactoring reads in the file using |
I've just tried 2to3 in 3.7.0, and it still breaks line endings for me... Seems like one also needs to fix line 517 in refactor.py |
Slawomir, you're probably looking for bpo-34108. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: