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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/lib2to3/refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions Lib/lib2to3/tests/data/crlf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
print "hi"

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

44 changes: 30 additions & 14 deletions Lib/lib2to3/tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure line-endings are respected when using lib2to3.